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;
}
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;
}