最近在项目上遇到了问题是: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; }