I use the following code to do HttpWebRequests on a website:
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 multiple web pages until I tried with a new one and only got the last part of the page. This is the response received:
<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 received data is sent. The following are the request and response headers:
Request:
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
Answer:
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
Doing the same with a web browser works fine and returns a content length of about 4000 bytes.
Any ideas?
PD: Just in case, it matters. I make several calls to SendGETRequest from different threads to the same site, but since there are no common variables, I think this should not change.
EDIT: this is the extension I use to extract text from a 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, this is correct. Also, note that the response header from the server contains the length of the truncated data.