Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"A connection attempt failed because the connected party did not properly respond after a period of time" using WebClient

I am using the following code which is working on local machine, but when i tried the same code on server it throws me error

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Here is my code:

WebClient client = new WebClient(); // Add a user agent header in case the  // requested URI contains a query. //client.Headers.Add ("ID", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); Stream data = client.OpenRead("http://" + Request.ServerVariables["HTTP_HOST"] + Request.ApplicationPath + "/PageDetails.aspx?ModuleID=" + ID); StreamReader reader = new StreamReader(data); string s = reader.ReadToEnd(); Console.WriteLine(s); data.Close(); reader.Close(); 

I am getting error on

Stream data = client.OpenRead("http://" + Request.ServerVariables["HTTP_HOST"] + Request.ApplicationPath + "/PageDetails.aspx?ModuleID=" + ID); 

is it due any firewall setting?

like image 840
Ram Singh Avatar asked Jul 17 '13 07:07

Ram Singh


1 Answers

I had a similar problem and had to convert the URL from string to Uri object using:

Uri myUri = new Uri(URLInStringFormat, UriKind.Absolute); 

(URLInStringFormat is your URL) Try to connect using the Uri instead of the string as:

WebClient client = new WebClient(); client.OpenRead(myUri); 
like image 174
barca_d Avatar answered Oct 04 '22 14:10

barca_d