添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
追风的烤地瓜  ·  python - PyQt's ...·  1 年前    · 
侠义非凡的毛衣  ·  git - ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

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?

@EhsanUllah yes i have already done this step and the generated URL is working fine when i copied and paste it to another tab(Browser Tab) – Ram Singh Jul 17, 2013 at 7:19 Recently faced this error message since I was in VNP which was not allowing me to access S3 bucket. Putting here if someone face this issue might be helpful – Damith Aug 27, 2019 at 11:58

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

My code was working when running in VS, but I was getting it when deploying to IIS on the same server. Adding proxy settings explicitly like this solved the problem. – Anders May 25, 2018 at 12:57

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.

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";
                using (Stream stream = client.OpenRead(new Uri(noaaSourceUrl)))
                    //......
            catch (Exception ex)
                throw;

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

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.

yes i have already checked that and its creating the valid URL and i have already tried this thing – Ram Singh Jul 17, 2013 at 7:18

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...

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!

  • 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.

  • Type Services.msc on run and check:

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

  • 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