这是由于在IIS7中站点被以POST方式请求时,必须要求传递参数,如果调用的API无须传递参数,需要标记下内容为0
httpWebRequest.ContentLength = 0;
即可解决411异常.
public static string Post(string url)
{
//垃圾回收,回收没有正常关闭的http连接
System.GC.Collect();
try
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = 0;
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
return result;
}
}
catch (Exception ex)
{
}
}