Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to read data from the transport connection: The connection was closed error in console application

I have this code in console application and it runs in a loop

 try
 {
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(search);
      request.Headers.Add("Accept-Language", "de-DE");
      request.Method = "GET";
      request.Accept = "text/html";
      using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
      {
           using (StreamReader reader = new StreamReader(response.GetResponseStream(),
                    Encoding.ASCII))
           {
                string html = reader.ReadToEnd();
                    FindForMatch(html, url);
           }
      }
 }
 catch (Exception ex)
 {
      throw new Exception(ex.Message);
 }

after few loops it gives

Unable to read data from the transport connection: The connection was closed

error. any idea why this happen? thanx..

like image 939
Darshana Avatar asked Jul 04 '12 06:07

Darshana


1 Answers

After adding

request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10; 

it works fine..

I found it form this blog post

WebRequest and Unable to read data from the transport connection Error

like image 151
Darshana Avatar answered Nov 06 '22 02:11

Darshana