HTTPWebResponse Timeout problems

B

Brent

I thought I was doing a simple thing, here -- asking a server for a text
document, getting the first 150 lines, and then returning the lines. But
I keep getting timeout errors:

"Exception Details: System.Net.WebException: The operation has timed-out."

I read somewhere that generally these errors come from not closing a
HTTPWebResponse connection before opening another. But -- to my
knowledge -- I've shut down connections in every way I could, and I'm
still getting the errors.

I'd sure appreciate any pointers!

Here's my code:

========================================

public string getHeader (string strURL)
{
StringBuilder returnString = new StringBuilder();

String strreturn;
HttpWebResponse oResponse = null;
Stream myStream = null;
StreamReader sr = null;
string thisRow = "";

try
{

HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(strURL);

oRequest.Timeout = 10000;
oRequest.UserAgent = "Web Client";

oResponse = (HttpWebResponse)oRequest.GetResponse();

myStream = oResponse.GetResponseStream();


//capture first 150 lines

sr = new StreamReader(myStream);
int i = 0;
while ((thisRow = sr.ReadLine()) != null)
{
if (i==150){break;}
thisRow = thisRow.Replace("_"," ");
returnString.Append(thisRow+"\n");
i++;
}

strreturn = returnString.ToString();
sr.Close();
myStream.Close();
oResponse.Close();

}
catch
{
strreturn = "0";
}

finally
{
if (sr != null){sr.Close();}

if(myStream != null){myStream.Close();}

if(oResponse != null){oResponse.Close();}

}

return strreturn;

}
 
S

S. Justin Gengo

Brent,

The entire web request is getting delivered before you display the first 150
lines. So you need to first receive the entire page you are scraping. How
long does that take? If it's more than the 10000 milisecond timeout
parameter you've set that would be the cause of the exception... You may
need to set your timeout higher.

To see if this is the problem set a breakpoint on the:

oResponse = (HttpWebResponse)oRequest.GetResponse();

line of code and then in the debugger see if that is returned or if it times
out.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top