使用HttpWebRequest收到截断的响应 [英] Truncated response received using HttpWebRequest
问题描述
我正在使用以下代码向网站发出HttpWebRequests:
I'm using the following code to make HttpWebRequests to a web site:
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();
}
在几个网页上一切正常,直到我尝试了一个新的网页,并且只收到了页面的最后一部分.这是收到的响应:
Everything worked fine with several web pages until I tried with a new one and only received the last part of the page. This is the received response:
<tr>
<td colspan="2" height="5"><spacer type="block" width="100%" height="5"></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
标头正确,并指出仅发送接收到的数据.以下是请求和响应的标题:
The header is correct and says that only the received data is sent. The following are the headers of the request and response:
请求:
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字节的内容长度.
Doing the same with a web browser works fine and returns a content length of about 4000 bytes.
有什么想法吗?
PD:以防万一,我正在从不同线程到同一个站点多次调用SendGETRequest,但由于没有共享变量,我认为这没有什么用.
PD: Just in case it matters I'm doing several calls to the SendGETRequest from different threads to the same site but as there are no shared variables I think it shouldn't make a difference.
这是我用来从流中提取文本的扩展名:
This is the extension I use to extract the text from the 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();
}
据我所知这是正确的.另外,请注意,来自服务器的响应标头包含截断的数据的长度
As far as I know it's correct. Also, note that the response header from the server contains the length of the truncated data
推荐答案
难以置信...我正在发送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将返回整页.对我来说,这听起来像是一个缺陷
Incredible ... I was sending the URL /Broker/Ops/FichaContratoJS.asp?nc=815044&IP=5&YY=2012&M=6 and the browser was sending /Broker/Ops/FichaContratoJS.asp?nc=815044&IP=5&YY=2012&M=06& (note the extra 0 on the M parameter (it's a month). Putting there that 0 returned the full page. Sounds like a defect to me
这篇关于使用HttpWebRequest收到截断的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!