Truncated response received using HttpWebRequest

2019-07-07 03:29发布

问题:

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

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


回答2:

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



回答3:

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