3

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:

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

Response:

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'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.

EDIT: 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

Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207

3 Answers3

1

I think that you are not using right the HttpWebResponse object.

Maybe you are not closing the request or reading all the response strem.

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse.aspx

Your method should be:

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;
    }
giammin
  • 18,620
  • 8
  • 71
  • 89
  • I don't think this is the problem. The response header from the server contains the length of the truncated data – Ignacio Soler Garcia May 09 '12 at 08:59
  • I'll try, but again, fiddler (an external web proxy debugger) is showing me the same data so looks like the web server is sending that data. Another thing, you don't need to call Close when using the using keyword. – Ignacio Soler Garcia May 09 '12 at 10:13
  • Resharper refactoring leaves Close. i didn't notice. :p – giammin May 09 '12 at 10:21
  • if with fiddler you see the same response it probably means that you are requesting only a chunk of that page. http1.1 permits that kind of requests – giammin May 09 '12 at 10:32
  • Not intentionally. Do you know what kind of header would do that? – Ignacio Soler Garcia May 09 '12 at 10:43
  • i'm asking you to use my code because I currently use it without problems in many project for doing the same thing you are trying to achieve. if it doesn't work maybe your problem is due to the useragent or cookiecontainer you are passing. – giammin May 09 '12 at 10:43
  • i don't remember the header but the value should be something like chunk or chunked – giammin May 09 '12 at 10:49
  • I cannot try until 21:00 GTM+1, but I will update the question for sure. Anyway I've been using my code also on a lot of projects ... I think that maybe is something related with the web server, the sessions or something like that. – Ignacio Soler Garcia May 09 '12 at 13:48
  • 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. – Ignacio Soler Garcia May 09 '12 at 19:31
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

Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207
0

I have run into a similar situation and found that copying the response stream into a MemoryStream seemed to fix my problems.

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