HttpWebResponse returns an 'incomplete' st

2019-09-11 00:55发布

问题:

I´m making repeated requests to a web server using HttpWebRequest, but I randomly get a 'broken' response stream in return. e.g it doesn´t contain the tags that I KNOW is supposed to be there. If I request the same page multiple times in a row it turns up 'broken' ~3/5.

The request always returns a 200 response so I first thought there was a null value inserted in the response that made the StreamReader think it reached the end.

I´ve tried: 1) reading everything into a byte array and cleaning it 2) inserting a random Thread.Sleep after each request

Is there any potentially bad practice with my code below or can anyone tell me why I´m randomly getting an incomplete response stream? As far as I can see I´m closing all unmanaged resources so that shouldn´t be a problem, right?

public string ReturnHtmlResponse(string url)
        {
        string result;
        var request = (HttpWebRequest)WebRequest.Create(url);
            {
            using(var response = (HttpWebResponse)request.GetResponse())
                {
                  Console.WriteLine((int)response.StatusCode);
                  var encoding = Encoding.GetEncoding(response.CharacterSet);

                    using(var stream = response.GetResponseStream())
                       {
                         using(var sr = new StreamReader(stream,encoding))
                            {
                             result = sr.ReadToEnd();
                            }
                       }
                }
            }
            return result;
        }

回答1:

I do not see any direct flaws in you're code. What could be is that one of the 'Parent' using statements is done before the nested one. Try changing the using to a Dispose() and Close() method.

public string ReturnHtmlResponse(string url)
        {
        string result;
        var request = (HttpWebRequest)WebRequest.Create(url);

        var response = (HttpWebResponse)request.GetResponse();
        Console.WriteLine((int)response.StatusCode);
        var encoding = Encoding.GetEncoding(response.CharacterSet);
        var stream = response.GetResponseStream();
        var sr = new StreamReader(stream,encoding);
        result = sr.ReadToEnd();

        sr.Close();
        stream.Close();
        response.Close();

        sr.Dispose();
        stream.Dispose();
        response.Dispose();

        return result;
        }