[問題] https post無法把值post出去

看板C_Sharp (C#)作者 (費德勒5566)時間5年前 (2019/07/25 12:04), 編輯推噓4(405)
留言9則, 5人參與, 5年前最新討論串1/1
原本使用的網頁jquery如下,可以把msg=$("#content").val() 給POST到遠端API $("button").click(function(){ $.post("API網址",{msg:$("#content").val(),}, function(e){ console.log(e); }); }); 想要改寫成C#之後多些彈性 但POST出去的內容,API一直回應說抓不到msg的值 string param = "msg=我要傳的訊息"; byte[] bs = Encoding.UTF8.GetBytes(param); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("API網址"); req.Method = "POST"; req.ContentType = "application/json"; req.ContentLength = bs.Length; using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(bs, 0, bs.Length); } 不曉得哪邊出了問題,搞了兩天還搞不定 原本想說折衷一下乾脆改用shellwindows去控制網頁傳值 但好死不死這網頁只能在chrome和edge運作,換成IE就一樣post不到 偏偏VS又只支援IE的control,快瘋了...-.- -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.249.76.232 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1564027492.A.705.html

07/25 12:43, 5年前 , 1F
建議改用HttpClient, 還有.net也有chrome核心的webkit能用
07/25 12:43, 1F

07/26 08:30, 5年前 , 2F
要看wireshark 我才是你msg沒有JSON.stringify
07/26 08:30, 2F

07/26 10:16, 5年前 , 3F
對啊你ContentType和實際送的不同啊,你的格式是formdata
07/26 10:16, 3F

07/26 10:17, 5年前 , 4F
ContentType要改成application/x-www-form-urlencoded吧
07/26 10:17, 4F

07/29 00:51, 5年前 , 5F
有https的網頁嗎
07/29 00:51, 5F

07/29 00:51, 5年前 , 6F
連接可以參考?我幫你測看看
07/29 00:51, 6F

08/01 23:00, 5年前 , 7F
應該不是json而是form喔 你要開F12看看
08/01 23:00, 7F

08/07 20:17, 5年前 , 8F
感謝上面各位,ContentType的確不是json,修正後就
08/07 20:17, 8F

08/07 20:17, 5年前 , 9F
ok了
08/07 20:17, 9F
文章代碼(AID): #1TEIfaS5 (C_Sharp)
文章代碼(AID): #1TEIfaS5 (C_Sharp)