简体   繁体   中英

HttpWebResponse returns an 'incomplete' stream

I´m making repeated requests to a web server using HttpWebRequest, but I randomly get a 'broken' response stream in return. eg 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;
        }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM