Aug 15 2006

Unable to read data from the transport connection

Posted by admin under .NET

Recently I was creating an application which in short works as a sort of webspider. In other words - non GUI application fetching pages from some intranet applications (doesn't matter, could as well has been public Internet pages).

 

It worked pretty well, I was using the simplest code possible

 

 

WebRequest webRequest = WebRequest.Create(strTheUrl);
WebResponse webResponse = webRequest.GetResponse();
string sTxt = new StreamReader(webResponse.GetResponseStream(), 
	Encoding.Default).ReadToEnd();
webResponse.Close();


Anyway, after a while I got an error message saying "Unable to read data from the transport connection: "

 

I couldn't understand what was going on, cause all sites were up and running. Turned out that if the sites failing used cookies - and therefore my request failed. So the correct syntax to fetch pages is instead

 

 

CookieContainer CC = new CookieContainer();

HttpWebRequest Req = (HttpWebRequest) WebRequest.Create(sUrl);
Req.Proxy = null;
Req.UseDefaultCredentials = true;
Req.CookieContainer = CC; 

WebResponse webResponse = Req.GetResponse();
string sTxt = new System.IO.StreamReader(webResponse.GetResponseStream(),
    Encoding.Default).ReadToEnd();
webResponse.Close();