最近在项目上遇到了问题是:411错误,出现这个错误可能是再请求POST的时候,若没有参数的情况下,需要把 HttpWebRequest的长度设置为0,req.ContentLength = 0;
public string HttpPost(string url)
{
string result = "";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
//httpWebRequest.ContentLength = 0;
req.ContentLength = 0;
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
//获取内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
return result;
}
标签:string,stream,ContentLength,req,result,HttpWebRequest,服务器返回,远程,411 From: https://blog.51cto.com/u_15902978/5965034