使用HttpWebRequest收到截断的响应

时间:2012-05-09 07:39:48

标签: c# .net httpwebrequest http-headers

我正在使用以下代码将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,但由于没有共享变量,我认为它不应该有所作为。

编辑:这是我用来从Stream中提取文本的扩展名:

    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();
    }

据我所知,这是正确的。此外,请注意,服务器的响应标头包含截断数据的长度

3 个答案:

答案 0 :(得分:1)

我认为您没有正确使用HttpWebResponse对象。

也许您没有关闭请求或阅读所有响应链。

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;
    }

答案 1 :(得分:0)

难以置信......我发送的网址为/Broker/Ops/FichaContratoJS.asp?nc=815044&IP=5&YY=2012&M=6,浏览器正在发送/Broker/Ops/FichaContratoJS.asp? NC = 815044&安培; IP = 5&安培; YY = 2012&安培; M = 06&安培; (注意M参数上的额外0(它是一个月)。把那个0放回整页。听起来像是我的缺陷

答案 2 :(得分:0)

我遇到了类似的情况,发现将响应流复制到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;
}