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

C#SocketsWebclient

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

setting the proxy address explicitly in web.config solved my problem

<system.net> 
	<defaultProxy> 
			<proxy usesystemdefault = "false" proxyaddress="http://address:port" bypassonlocal="false"/> 
	</defaultProxy> 
</system.net>

Resolving the “TCP error code 10060: A connection attempt failed…” while consuming a web service

Solution 3 - C#

I know this ticket is old, but I just ran into this issue and I thought I would post what was happening to me and how I resolved it:

In my service I was calling there was a call to another web service. Like a goof, I forgot to make sure that the DNS settings were correct when I published the web service, thus my web service, when published, was trying to call from api.myproductionserver.local, rather than api.myproductionserver.com. It was the backend web service that was causing the timeout.

Anyways, I thought I would pass this along.

Solution 4 - C#

Adding the following block of code in web.config solves my problem

 <system.net>
    <defaultProxy enabled="false" >
    </defaultProxy>
  </system.net>

Solution 5 - C#

I know this post was posted 5 years ago, but I had this problem recently. It may be cause by corporate network limitations. So my solution is letting WebClient go through proxy server to make the call. Here is the code which worked for me. Hope it helps.

        using (WebClient client = new WebClient())
        {
            client.Encoding = Encoding.UTF8;
            WebProxy proxy = new WebProxy("your proxy host IP", port);
            client.Proxy = proxy;
            string sourceUrl = "xxxxxx";
            try
            {
                using (Stream stream = client.OpenRead(new Uri(noaaSourceUrl)))
                {
                    //......
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

Solution 6 - C#

Is the URL that this code is making accessible in the browser?

http://" + Request.ServerVariables["HTTP_HOST"] + Request.ApplicationPath + "/PageDetails.aspx?ModuleID=" + ID

First thing you need to verify is that the URL you are making is correct. Then check in the browser to see if it is browsing. then use Fiddler tool to check what is passing over the network. It may be that URL that is being called through code is wrongly escaped.

Then check for firewall related issues.

Solution 7 - C#

I had this problem. Code worked fine when running locally but not when on server. Using psPing (https://technet.microsoft.com/en-us/sysinternals/psping.aspx) I realised the applications port wasn't returning anything. Turned out to be a firewall issue. I hadn't enabled my applications port in the Windows Firewall.

Administrative Tools > Windows Firewall with Advanced Security added my applications port to the Inbound Rules and it started working.

Somehow the application port number had got changed, so took a while to figure out what was going on - so thought I'd share this possibility in case it saves someone else time...

Solution 8 - C#

In my case I got this error because my domain was not listed in Hosts file on Server. If in future anyone else is facing the same issue, try making entry in Host file and check.

Path : C:\Windows\System32\drivers\etc
FileName: hosts

Solution 9 - C#

I have resolved this below issue

> 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

Solution

We need to configured proxy setting in code. my scenario using Web.config file

Added Proxy address as below

<system.net>
<defaultProxy>
  <proxy  proxyaddress="http://XXXXX:XXXX" bypassonlocal="True"/>
</defaultProxy>
</system.net>

Also added proxy credential using below code NetworkCredential - I have used my local credential here.

 HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url);
 webReq.Credentials = new NetworkCredential("XXXX", "XXXXX");
 webReq.Proxy.Credentials = new NetworkCredential("XXXX", "XXXX");

It works for me!

Solution 10 - C#

  1. First Possibility: The encrypted string in the Related Web.config File should be same as entered in the connection string (which is shown above) And also, when you change anything in the "Registry Editor" or regedit.exe (as written at Run), then after any change, close the registry editor and reset your Internet Information Services by typing IISRESET at Run. And then login to your environment.

  2. Type Services.msc on run and check:

    1. Status of ASP.NET State Services is started. If not, then right click on it, through Properties, change its Startup type to automatic.

    2. Iris ReportManager Service of that particular bank is Listed as Started or not. If its Running, It will show "IRIS REPORT MANAGER SERVICE" as started in the list. If not, then run it by clicking IRIS.REPORTMANAGER.EXE

Then Again RESET IIS

Solution 11 - C#

I know it's an old post but I came across the exact same issue and I managed to use this by turning off MALWAREBYTES program which was causing the issue.

Solution 12 - C#

It might be issue by proxy settings in server. You can try by disabling proxy setting,
<defaultProxy enabled="false" />

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRam SinghView Question on Stackoverflow
Solution 1 - C#barca_dView Answer on Stackoverflow
Solution 2 - C#Mahmoud FarahatView Answer on Stackoverflow
Solution 3 - C#Corran HornView Answer on Stackoverflow
Solution 4 - C#B.DikmennView Answer on Stackoverflow
Solution 5 - C#Allen Z.View Answer on Stackoverflow
Solution 6 - C#EhsanView Answer on Stackoverflow
Solution 7 - C#SteveLView Answer on Stackoverflow
Solution 8 - C#jackkds7View Answer on Stackoverflow
Solution 9 - C#Anand SutarView Answer on Stackoverflow
Solution 10 - C#Syed Arbab AhmedView Answer on Stackoverflow
Solution 11 - C#IllusionsView Answer on Stackoverflow
Solution 12 - C#Jitendra G2View Answer on Stackoverflow