簡體   English   中英

使用帶有兩個參數的HttpWebRequest在C#中發布

[英]Post in C# using HttpWebRequest with two parameters

我是編程新手。 我正在編寫一個使用C#和android的簡單應用程序,其中我需要向服務器發送信息,信息將URL與兩個參數合並在一起。 當我嘗試將信息發送到服務器時,它只是發送URL,而不發送參數。 您能在這方面幫助我嗎?

var postData = ("id1="+"123456");
postData += ("&id2="+"0123456789");
var request = (HttpWebRequest)WebRequest.Create("http://abc.xyz.com");
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST"
request.ContentType = "multipart/form-data";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
 {
     stream.Write(data, 0, data.Length);
     stream.Close();
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
 response.Close();

您已將內容類型設置為multipart/form-data但是要發布的數據形成為application/x-www-form-urlencoded

多部分內容應如下所示:

--12345
Content-Disposition: form-data; name="sometext"

some text sent via post...
--12345--

內容類型必須包含邊界:

request.ContentType = "multipart/form-data; boundary=12345";

但是您有這種類型的內容:

id1=123456&id2=0123456789

因此,將application/x-www-form-urlencoded用於內容類型將解決此問題:

request.ContentType = "application/x-www-form-urlencoded";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM