Kevin
Fri May 02 13:59:00 CDT 2008
Jon,
I couldn't figure out how to accomplish my goal of passing in a new URL into
the OpenRead and getting the appropriate response without putting the entire
block of code (see below) in the loop. So the answer to your question is Yes.
If I could instantiate the StreamReader further up and assign the instance
the Stream in the loop I'd do that but it didn't seem to work. Using
sr.DiscardBufferedData(); seems to be working ok now but the fact that I'm
going through our proxy server causes other problems but there isn't much I
can do about that, politricks ya know.
strm = wc.OpenRead(nextSubURL);
System.IO.StreamReader sr = new System.IO.StreamReader(strm);
System.IO.StringReader strReader = new System.IO.StringReader(sr.ReadToEnd());
Thank you!
--
Kevin C. Brown
Developer
"Jon Skeet [C# MVP]" wrote:
> KevinB <Kevin.Brown@DarbyDentalSupply.com> wrote:
> > Here is my original code, below it is what I did to fix my problem.
> >
> > System.Net.WebClient wc = new System.Net.WebClient();
> > > > > System.Net.WebProxy wp = new System.Net.WebProxy();
> > > > > wp.UseDefaultCredentials = true;
> > > > > wc.Headers.Add("User-Agent", "Mozilla/4.0+");
> > > > > wc.Proxy = wp;
> > > > > strm = wc.OpenRead(nextSubURL);
> > > > > System.IO.StreamReader sr = new System.IO.StreamReader(strm);
> > > > > System.IO.StringReader strReader = new
> > > > > System.IO.StringReader(sr.ReadToEnd());
> >
> > the fix:
> >
> > sr.DiscardBufferedData();
>
> No, that's not going to fix it at all. How could it? If you call it
> before ReadToEnd() it's just going to throw away nothing (because you
> haven't read anything yet). If you call it *after* ReadToEnd(), it
> obviously can't stop
>
> > and of course you should always:
> > sr.close();
>
> Well, you shouldn't do it explicitly - you should use a "using"
> statement to wrap it in a try/finally block automatically.
>
> That could actually be the fix - if you weren't closing the
> StreamReader, it's possible that it wasn't closing the network
> connection in the background, and the next request to the same server
> may have been waiting for that connection to be closed.
>
> > > > > strm = wc.OpenRead(nextSubURL);
> > > > > System.IO.StreamReader sr = new System.IO.StreamReader(strm);
> > > > > System.IO.StringReader strReader = new
> > > > > System.IO.StringReader(sr.ReadToEnd());
> >
> > is in a loop grabbing a different nextSubURL from a table in my Db and I was
> > putting a different HTML page in the StreamReader everytime, clearing it
> > would be a tremendous help solve all timeout problems.
>
> No, you weren't putting a different HTML page into the StreamReader -
> you were creating a *new* (empty) StreamReader. Big difference.
>
> --
> Jon Skeet - <skeet@pobox.com>
> Web site:
http://www.pobox.com/~skeet
> Blog:
http://www.msmvps.com/jon.skeet
> C# in Depth:
http://csharpindepth.com
>