日期:2014-05-18  浏览次数:21091 次

远程服务器返回错误: (411) 所需的长度 这是什么错误?? 在线等! 急~~~~

HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create("Http://192.168.213.99:8090");
  wreq.Method = "POST";
  wreq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8";
  HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse();



  打断点调式 执行到这句“ HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse();” 出错。

  “ 远程服务器返回错误: (411) 所需的长度”

------解决方案--------------------
如果是post的话,你的参数没有
需要将参数写入request里面
C# code
   if (method.ToUpper() == "POST")
                {
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = data.Length;
                    /*  可接收可压缩HTML*/
                    request.Headers.Add("Accept-Language", "zh-cn");
                    request.Headers.Add("Accept-Encoding", "gzip,deflate");
                    /* 柏志诚  */
                    Stream newStream = request.GetRequestStream();
                    newStream.Write(data, 0, data.Length);
                    newStream.Close();
                }

------解决方案--------------------
request.Method = "GET";
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "" );
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
}

post
string param = "";
byte[] bs = Encoding.ASCII.GetBytes(param);

HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "" );
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}

------解决方案--------------------
C# code

public String RGetPageCode(String PageURL, String Charset)
    {
        String strHtml = "";
        ASCIIEncoding encoding = new ASCIIEncoding();
        //byte[] bytes = Encoding.UTF8.GetBytes(Charset);
        byte[] bytes = encoding.GetBytes(Charset);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(PageURL);
        request.Method = "POST";
        request.ContentLength = bytes.Length;
        request.ContentType = "text/xml";
        using (Stream requestStream = request.GetRequestStream())
        {
            requestStream.Write(bytes, 0, bytes.Length);
        }

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        if (response.StatusCode == HttpStatusCode.OK)
        {
           // strHtml = String.Format("POST failed. Received HTTP {0}",response.StatusCode);
            if (response != null)
            {
                if (response.StatusCode == HttpStatusCode.OK && request.HaveResponse)
                {
                    using (StreamReader sr = new StreamReader(response.GetResponseStream(), encoding))
                    {
                        if (sr != null)
                        {
                            strHtml = sr.ReadToEnd();
                        }
                    }
                }
                response.Close();
            }
        }
        return strHtml;
    }