这是由于在IIS7中站点被以POST方式请求时,必须要求传递参数,如果调用的API无须传递参数,需要标记下内容为0
httpWebRequest.ContentLength = 0;
即可解决411异常.
/// <summary> /// 执行post请求 /// </summary> /// <param name="url">url地址</param> /// <returns></returns> 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; }