使用HttpWebRequest收到截断的响应

我正在使用以下代码将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(); } 

几个网页都运行良好,直到我尝试使用新网页并且只收到页面的最后一部分。 这是收到的回复:

          

标题是正确的,并表示只发送接收的数据。 以下是请求和响应的标头:

请求:

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

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

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

难以置信……我发送的url为/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放回整页。对我来说听起来像是一个缺陷

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