0
Picture of Postman POST request如何在C#中POST請求與內容類型爲X-WWW-form-urlencoded?
我想使這個POST請求接收OAuth2用戶訪問令牌,但使用C#代碼在Visual Studio 2015年
感謝。
Picture of Postman POST request如何在C#中POST請求與內容類型爲X-WWW-form-urlencoded?
我想使這個POST請求接收OAuth2用戶訪問令牌,但使用C#代碼在Visual Studio 2015年
感謝。
System.Net.HttpWebRequest
是你需要知道的類,這是一個簡單的例子。關於更多信息,請參閱MSDN:https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest(v=vs.110).aspx
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.AllowAutoRedirect = true;
webRequest.Timeout = 20 * 1000;
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36";
//write the data to post request
//Postdata : a=1&b=2&c=3
byte[] buffer = Encoding.Default.GetBytes(Postdata);
if (buffer != null)
{
webRequest.ContentLength = buffer.Length;
webRequest.GetRequestStream().Write(buffer, 0, buffer.Length);
}
WebResponse wr = webRequest.GetResponse()
對不起,目前不起作用。你將如何寫出類型爲x-www-form-url-encoded的Postdata?作爲字符串格式或字典或列表?感謝所有的幫助! – MiRo
參見注釋'Postdata'是一個字符串'Postdata:a = 1&b = 2&c = 3' –