Re: StreamReader.Close() response is very slow - please help

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Joerg Jooss (joerg.jooss_at_gmx.net)
Date: 04/10/04


Date: Sat, 10 Apr 2004 12:59:49 +0200

No_Excuses wrote:

> All,
>
> I am interested in reading the text of a web page and parsing it.
> After searching on this newgroup I decided to use the following:
>
> ******************************* START OF CODE ************************
> String sTemp =
>
"http://cgi3.igl.net/cgi-bin/ladder/teamsql/team_view.cgi?ladd=teamknights&n
um=238&showall=1";
>
> WebRequest myWebRequest = WebRequest.Create(sTemp);
> WebResponse myWebResponse = myWebRequest.GetResponse();
> Stream myStream = myWebResponse.GetResponseStream();
>
> // default encoding is utf-8
> StreamReader SR = new StreamReader( myStream );
>
> Char[] buffer = new Char[2048];
>
> // Read 256 charcters at a time.
> int count = SR.Read( buffer, 0, 2000 );
>
> //while (count > 0)
> //{
> // do some processing - may read all or part
> // count = SR.Read(buffer, 0, 2000);
> //}
>
> SR.Close(); // Release the resources
> myWebResponse.Close();
> ******************************* END OF CODE ************************
>
> This code should look very familiar because it is all over the
> newsgroup and Microsoft support help pages.

I doubt that, as the code doesn't do what it advertises ;-)

Char[] buffer = new Char[2048];

// Read 256 charcters at a time.
int count = SR.Read( buffer, 0, 2000 );

Why a 2 kB buffer, when you're supposedly reading only 256 chars, but you're
specifying 2000 chars for the Read() call?

> The web page has a big table on it and it takes a while to download
> (even with a cable modem).
>
> What I observe is the following. If I open and read all the data
> (i.e.
> until count > 0 fails, then stepping over SR.Close() execution time is
> immediate. If I read only 2000 bytes as the above example shows, when
> I step over SR.Close() it takes a long time (for me around 10-15
> seconds). This may be a coincidence but it seems to take the same
> amount of time as if I was reading all of the data.

Well, this particular page is an insane 6 MB large... the web server does
not help the client either, as there's no Content-Length header provided,
just Connection: close:

HTTP/1.1 200 OK
Date: Sat, 10 Apr 2004 10:20:31 GMT
Server: Apache/1.3.24 (Unix) mod_throttle/3.1.2 PHP/4.2.0
Connection: close
Content-Type: text/html

Even more interestingly, I cannot even download the entire page at all...
neither WebClient nor WebRequest/WebResponse are able to download that
beast. Both stop downloading at the exact same position -- I guess the
underlying TCP stream is prematurely closed. This must be some WinInet
default behaviour (quirk?), as the same thing happens to me when I download
the page using some ancient old Visual J++ code that uses plain TCP. I think
I'll write some plain HTTP client using System.Net.Sockets and see what
happens.

(Note: If the web server returns a Content-Length header, downloading the
page works just fine.)

[...]
> Does anyone know how to terminate the loading of the page so I can
> eliminate the delay? I had implemented this in C++ with MFC using
> CInternetSession.OpenURL() and did not have this problem.

Use asynchronous I/O -- see WebRequest.Abort(),
WebResponse.BeginGetResponse(), and WebResponse.EndGetResponse().

Cheers,

-- 
Joerg Jooss
joerg.jooss@gmx.net


Relevant Pages

  • (off topic) How to enjoy "free" Audiobooks from the Web
    ... I find listening to audiobooks a wonderful way to pursue my lifelong ... habit of voracious reading, and I find there's nothing better than to lie back ... Audiobooks are usually collections of .mp3 format files found usenet ... means you might download 99 components of a file only to find the 100th. ...
    (soc.culture.thai)
  • Re: (off topic) How to enjoy "free" Audiobooks from the Web
    ... > libaray of audiobooks from the internet. ... > amount of time I spend reading every day because I am doing technical ... > to 30 gigabytes of downloads per month (if you can download that much ... > found usenet newsgroups whose titles begin with "alt.binaries. ...
    (soc.culture.thai)
  • Re: Office 2003 SP2
    ... I'm a little surpriced about reading "Now go away". ... make it possible to install. ... > download SP-2, burn it and give you a copy. ... > || explicitly inquired about someone else downloading the patch for you, ...
    (microsoft.public.officeupdate)
  • Re: Office 2003 SP2
    ... > | If you post it on a web site for download, ... Anyone reading the EULA would have figured it ... it to a CD and then install it on your computer. ... you explicitly inquired about someone else downloading the patch ...
    (microsoft.public.officeupdate)
  • Re: StreamReader.Close() response is very slow - please help
    ... I did not notice that the entire page of data does not download. ... The reason for closing the connection is ... >> I am interested in reading the text of a web page and parsing it. ... > I'll write some plain HTTP client using System.Net.Sockets and see what ...
    (microsoft.public.dotnet.languages.csharp)