使用接收到的截断的响应的HttpWebRequest(Truncated response rece

2019-09-17 02:37发布

我使用下面的代码,以使HttpWebRequests到一个网站:

public static HttpWebResponse SendGETRequest(string url, string agent, CookieContainer cookieContainer)
{
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
   request.UserAgent = agent;
   request.Method = "GET";
   request.ContentType = "text/html";
   request.CookieContainer = cookieContainer;

   return (HttpWebResponse)request.GetResponse();
}

一切从多个网页工作得很好,直到我试图用一个新的,只收到了页面的最后一部分。 这是接收到的响应:

<tr> 
    <td colspan="2" height="5"><spacer type="block" width="100%" height="5"></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

头部是正确的,并说,只有接收到的数据发送。 以下是请求和响应的报头中:

请求:

GET /Broker/Ops/FichaContratoJS.asp?nc=815044&IP=5&YY=2012&M=6&St=0&CC=FESX201206 HTTP/1.1  
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19  
Content-Type: text/html  
Host: www.xxxx.com  
Cookie: ASPSESSIONIDACBDCDBT=MGDNMNABOANDMILHBNCIDFCH;Autenticacion=Sid=230fae3d%2De0e2%2D4df1%2D8aa8%2D000fb352eaef&IdUsuarioWeb=xxxx; ASPSESSIONIDACBCCDAT=AFDJMNABAFJDDHABLOLAINDK; ASPSESSIONIDCADCBCAT=CEBJGNABLCALPJLDJFPBMLDE

响应:

HTTP/1.1 200 OK  
Date: Wed, 09 May 2012 07:25:03 GMT  
Server: Microsoft-IIS/6.0  
X-Powered-By: ASP.NET  
Pragma: no-cache  
**Content-Length: 155**  
Content-Type: text/html  
Expires: Wed, 09 May 2012 07:24:02 GMT  
Set-Cookie: Autenticacion=Sid=230fae3d%2De0e2%2D4df1%2D8aa8%2D000fb352eaef&IdUsuarioWeb=xxxx; path=/  
Cache-control: no-cache  

做同样的使用Web浏览器工作正常,返回的约4000个字节的内容长度。

有任何想法吗?

PD:万一它的事项我在做多次打电话给SendGETRequest从不同的线程在同一网站,但因为没有共享变量,我认为它不应该有所作为。

编辑:这是我用来提取从流文本扩展:

    public static string ReadTextResponse(this Stream stream)
    {
        int count;
        Encoding enconding = System.Text.Encoding.GetEncoding(1252);
        System.Text.StringBuilder stringBuilder = new StringBuilder();
        byte[] buffer = new byte[1023];

        do
        {
            count = stream.Read(buffer, 0, buffer.Length);

            if (count != 0)
            {
                string tempString = enconding.GetString(buffer, 0, count);
                stringBuilder.Append(tempString);
            }
        }
        while (count > 0);

        return stringBuilder.ToString();
    }

据我知道这是正确的。 此外, 请注意来自服务器的响应报头包含截短的数据的长度

Answer 1:

我认为你没有使用权的HttpWebResponse对象。

也许你不关闭请求或读取所有响应的sTREM。

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse.aspx

你的方法应该是:

public static string SendGETRequest(string url, string agent, CookieContainer cookieContainer)
    {
        var request = (HttpWebRequest)WebRequest.Create(url);
        request.UserAgent = agent;
        request.Method = "GET";
        request.ContentType = "text/html";
        request.CookieContainer = cookieContainer;

        string result;
        using (var myResponse = (HttpWebResponse) request.GetResponse())
        {
            using (var stream = myResponse.GetResponseStream())
            {
                result = null;
                if (stream != null)
                {
                    using (var sr = new StreamReader(stream, System.Text.Encoding.UTF8))
                    {
                        result = sr.ReadToEnd();
                        sr.Close();
                    }
                    stream.Close();
                }
            }
            myResponse.Close();
        }
        return result;
    }


Answer 2:

不可思议......我的网址发送/Broker/Ops/FichaContratoJS.asp?nc=815044&IP=5&YY=2012&M=6和浏览器被发送/Broker/Ops/FichaContratoJS.asp?nc=815044&IP=5&YY=2012&M=06& (请注意多出0的M参数(这是一个月)。把那里返回0整个页面。听起来像是一个缺陷给我



Answer 3:

我遇到了类似的情况,发现复制的响应流到MemoryStream似乎解决我的问题。

public static string SendGETRequest(string url, string agent, CookieContainer cookieContainer)
{
    var request = (HttpWebRequest)WebRequest.Create(url);
    request.UserAgent = agent;
    request.Method = "GET";
    request.ContentType = "text/html";
    request.CookieContainer = cookieContainer;

    string result;
    using (var myResponse = (HttpWebResponse) request.GetResponse())
    {
        using (var stream = myResponse.GetResponseStream())
        {
            result = null;
            if (stream != null)
            {
                MemoryStream memStream = new MemoryStream();
                stream.CopyTo(memStream);
                memStream.Flush();
                stream.Close();

                using (var sr = new StreamReader(memStream, System.Text.Encoding.UTF8))
                {
                    result = sr.ReadToEnd();
                    sr.Close();
                }

            memStream.Close();
            }
        }
        myResponse.Close();
    }
    return result;
}


文章来源: Truncated response received using HttpWebRequest