Post 提交错误 远程服务器返回错误: (411) 所需的长度

这是由于在IIS7中站点被以POST方式请求时,必须要求传递参数,如果调用的API无须传递参数,需要标记下内容为0

httpWebRequest.ContentLength = 0;

即可解决411异常.

/// 

/// 执行post请求

/// 

/// url地址

/// 

public static string HttpPost(string url)

{

//创建http请求

HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);

//参数类型,这里是json类型

//还有别的类型如"application/x-www-form-urlencoded"

httpWebRequest.ContentType = "application/json";



//设置请求类型

httpWebRequest.Method = "POST";

//设置超时时间

httpWebRequest.Timeout = 20000;
//标记内容为0

httpWebRequest.ContentLength = 0;

//发送请求

HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

//读取返回数据

StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);

string responseContent = streamReader.ReadToEnd();

streamReader.Close();

httpWebResponse.Close();

httpWebRequest.Abort();

return responseContent;

}

你可能感兴趣的:(bug总结,json,postman,测试工具,post)