c# - 使用 HttpWebRequest 收到的截断响应

标签 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>

header 是正确的,表示只发送接收到的数据。以下是请求和响应的 header :

要求:

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  

在网络浏览器上执行相同的操作效果很好,并返回大约 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();
    }

据我所知,这是正确的。另外,请注意来自服务器的响应 header 包含截断数据的长度

最佳答案

我认为您没有正确使用 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;
    }

关于c# - 使用 HttpWebRequest 收到的截断响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10511764/

相关文章:

c# - 即时从字符串编译 Func<T1, TResult>

c# - 如何在 C# 中的类中正确隐藏帮助程序(内部)类

c# - 如何从单元测试中引用 VS 2012 可移植类库?

.net - native C++ 应用程序的 list ?

java - HTTPServlet Request.getInputStream() 总是接收空行

c# - Xamarin iOS WebException : App crashes after HttpWebRequest is complete

c# - 为什么 "DateTime.Now.ToString("hh tt", new CultureInfo ("de"))"在不同版本的 .Net 中返回不同的结果?

c# - 默认文件名 SaveFileDialog

c# - 如何将表作为参数传递给 MySqlCommand?

.net - 如何防止 HttpWebRequest 的数据包碎片