Groups | Blog | Home
all groups > dotnet clr > february 2007 >

dotnet clr : Trouble with timeout when code runs the second time


Per [Pedalen]
2/5/2007 3:50:01 PM
Hi,

I am trying to solve a tutorial problem of mine where I am using the
HttpWebRequest and HttpWebResponse objects to call MSN Search to retrieve the
page. This works fine the first time but doing it all over again is just
giving me a timeout.

Below is my code, somewhat simplified;

for (Int32 i = 0; i < 2; i++)
{

String answer = String.Empty;
HttpWebRequest request =
WebRequest.Create(uri[i]) as HttpWebRequest;
request.KeepAlive = false;
request.Method = "GET";
request.ContentType = "text/html";
request.ContentLength = GetContentLength(searchstring);
request.Timeout = 15000;

HttpWebResponse response = request.GetResponse();

using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, _encoding);
answer = reader.ReadToEnd();
}
response.Close();
}
Per [Pedalen]
2/8/2007 2:01:00 PM
Yes, it is safer and it was the way I did it first. And yes, I have tried
that one too.

Still, I have the same problem.

I have narrowed my problem to be the StreamReader and the method
ReadToEnd(). And yes, I have tried ReadLine() as well as Read() too.

[quoted text, click to view]
Laura T.
2/8/2007 5:38:37 PM
Wouldn't it be safer to wrap streamreader inside using()

using(StreamReader reader = new StreamReader(stream, _encoding))
{
answer = reader.ReadToEnd();
}

or add reader.Close(), rather than closing the stream itself?



"Per [Pedalen]" <PerPedalen@discussions.microsoft.com> ha scritto nel
messaggio news:E461F675-AE2B-422F-AE00-F13CAEA47A2B@microsoft.com...
[quoted text, click to view]

AddThis Social Bookmark Button