POST请求——HttpWebRequest
Posted Lulus
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POST请求——HttpWebRequest相关的知识,希望对你有一定的参考价值。
string url="";
string param="";
string result = string.Empty;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
Encoding encoding = Encoding.UTF8;
byte[] bs = Encoding.UTF8.GetBytes(param);
string responseData = String.Empty;
req.Method = "Post";
req.Accept = "application/json, text/javascript, */*; q=0.01";
req.ContentType = "application/json; charset=UTF-8";
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
reqStream.Close();
}
using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
{
responseData = reader.ReadToEnd().ToString();
}
result = responseData;
}
以上是关于POST请求——HttpWebRequest的主要内容,如果未能解决你的问题,请参考以下文章
使用 C# 和 HttpWebRequest 向端点发送 POST 请求
C# 利用HttpWebRequest进行HTTPS的post请求的示例
C#利用HttpWebRequest进行post请求的示例(HTTPS)
HttpClient替换HttpWebRequest--以GET和POST请求为例说明