Never Ends Stream in HttpWebResponse

How can I read a few bytes and disconnect? I use this code

using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
{
    using (Stream sm = resp.GetResponseStream())
    {
        using (StreamReader sr = new StreamReader(sm, Encoding.Default))
        {
            sr.Read();
            sr.Close();
        }
    }
}

but he is waiting for the end of the stream

+3
source share
5 answers

You probably do not want to use a StreamReaderstream to read WebResonseunless you know that the stream contains newline characters. StreamReaderlike to think in terms of lines, and if there are no new lines in the stream, it will hang.

It is best to read as many bytes as you want in a buffer byte[], and then convert it to text. For instance:

int BYTES_TO_READ = 1000;
var buffer = new byte[BYTES_TO_READ];

using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
{
    using (Stream sm = resp.GetResponseStream())
    {
        int totalBytesRead = 0;
        int bytesRead;
        do
        {
            // You have to do this in a loop because there no guarantee that
            // all the bytes you need will be ready when you call.
            bytesRead = sm.Read(buffer, totalBytesRead, BYTES_TO_READ-totalBytesRead);
            totalBytesRead += bytesRead;
        } while (totalBytesRead < BYTES_TO_READ);

        // Sometimes WebResponse will hang if you try to close before
        // you've read the entire stream.  So you can abort the request.
        request.Abort();
    }
}

At this point, the buffer has the first BYTES_TO_READbytes from the buffer. Then you can convert this to a string, for example:

string s = Encoding.Default.GetString(buffer);

MemoryStream , StreamReader.

WebResponse , . , , , , request.Abort() , . .

, , " ", "".

+5

- ?

        string GetWebPageContent(string url)
        {
            string result = string.Empty;
            HttpWebRequest request;
            const int bytesToGet = 1000;
            request = WebRequest.Create(url) as HttpWebRequest;

//get first 1000 bytes
            request.AddRange(0, bytesToGet - 1);

            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    result = sr.ReadToEnd();
                }
            }
            return result;
        }

AddRange .

+4

beacuse http 1.1 - , tcp , .

myHttpWebRequest1.KeepAlive = false; tcp HTTP.

https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.connection(v=vs.110).aspx#

+1

If you say winforms or webforms, I would put the request in threadpool (or Task if you use .net 4). Streams, even with good handling, are too easy to put a GUI in a wait state that most users don’t like.

0
source

I had a similar endless parsing request with the examples listed here. To fix these issues, I came to the following:

// url is a string where the request is going.
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

// Do what you have to do with the Request.

StringBuilder builder = new StringBuilder();
int toRead = 1000;

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    HttpStatusCode status = response.StatusCode;

    using (Stream receiveStream = response.GetResponseStream())
    {
        using (StreamReader readStream = new StreamReader(receiveStream, Encoding.GetEncoding("utf-8")))
        {
            Char[] read = new Char[toRead];
            int count = readStream.Read(read, 0, toRead);

            while (count > 0)
            {
                string str = new String(read, 0, count);
                builder.Append(str);

                count = readStream.Read(read, 0, toRead);
            }

            readStream.Close();
        }   
    }

    response.Close();
}

return builder.ToString();
0
source

Source: https://habr.com/ru/post/1796524/


All Articles