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.WebE xception: 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.Cre ate(strURL);
oRequest.Timeou t = 10000;
oRequest.UserAg ent = "Web Client";
oResponse = (HttpWebRespons e)oRequest.GetR esponse();
myStream = oResponse.GetRe sponseStream();
//capture first 150 lines
sr = new StreamReader(my Stream);
int i = 0;
while ((thisRow = sr.ReadLine()) != null)
{
if (i==150){break; }
thisRow = thisRow.Replace ("_"," ");
returnString.Ap pend(thisRow+"\ n");
i++;
}
strreturn = returnString.To String();
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;
}