Re: StreamReader.Close() response is very slow - please help
From: Joerg Jooss (joerg.jooss_at_gmx.net)
Date: 04/10/04
- Next message: Paul E Collins: "Re: Stack size setting in VS.net"
- Previous message: DalePres: "Re: Why does FALSE return TRUE?"
- In reply to: No_Excuses: "StreamReader.Close() response is very slow - please help"
- Next in thread: No_Excuses: "Re: StreamReader.Close() response is very slow - please help"
- Reply: No_Excuses: "Re: StreamReader.Close() response is very slow - please help"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Paul E Collins: "Re: Stack size setting in VS.net"
- Previous message: DalePres: "Re: Why does FALSE return TRUE?"
- In reply to: No_Excuses: "StreamReader.Close() response is very slow - please help"
- Next in thread: No_Excuses: "Re: StreamReader.Close() response is very slow - please help"
- Reply: No_Excuses: "Re: StreamReader.Close() response is very slow - please help"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|