C# “连接尝试失败,因为连接方在一段时间后没有正确响应”使用 WebClient

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17693353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:12:27  来源:igfitidea点击:

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

c#socketswebclient

提问by Ram Singh

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?

是否由于任何防火墙设置?

采纳答案by barca_d

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

我遇到了类似的问题,必须使用以下方法将 URL 从字符串转换为 Uri 对象:

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

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

(URLInStringFormat 是您的 URL)尝试使用 Uri 而不是字符串进行连接:

WebClient client = new WebClient();
client.OpenRead(myUri);

回答by Ehsan

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

这段代码在浏览器中访问的 URL 是否可以访问?

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 Fiddlertool to check what is passing over the network. It may be that URL that is being called through code is wrongly escaped.

您需要验证的第一件事是您创建的 URL 是否正确。然后在浏览器中查看是否正在浏览。然后使用Fiddler工具检查通过网络传递的内容。可能是通过代码调用的 URL 被错误地转义了。

Then check for firewall related issues.

然后检查防火墙相关问题。

回答by Syed Arbab Ahmed

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

  1. 第一种可能性:相关 Web.config 文件中的加密字符串应与连接字符串中输入的字符串相同(如上所示)而且,当您在“注册表编辑器”或regedit.exe(如运行时所写)中更改任何内容时,则进行任何更改后,关闭注册表编辑器并通过IISRESET在运行中键入来重置您的 Internet 信息服务。然后登录到您的环境。

  2. 在运行时输入 Services.msc 并检查:

    1. ASP.NET 状态服务的状态已启动。如果没有,则右键单击它,通过属性,将其启动类型更改为自动。

    2. 该特定银行的 Iris ReportManager 服务是否被列为已启动。如果它正在运行,它将在列表中显示“IRIS REPORT MANAGER SERVICE”作为启动。如果没有,则通过单击运行它IRIS.REPORTMANAGER.EXE

    然后再次重置IIS

回答by Mahmoud Farahat

setting the proxy address explicitly in web.configsolved my problem

明确设置代理地址web.config解决了我的问题

<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

在使用 Web 服务时解决“TCP 错误代码 10060:连接尝试失败...”

回答by Corran Horn

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.

在我调用的服务中,调用了另一个 Web 服务。就像一个傻瓜一样,我在发布 Web 服务时忘记确保 DNS 设置正确,因此我的 Web 服务在发布时试图从 api.myproductionserver.local 调用,而不是从 api.myproductionserver.com 调用。导致超时的是后端 Web 服务。

Anyways, I thought I would pass this along.

无论如何,我想我会传递这个。

回答by SteveL

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.

我有这个问题。代码在本地运行时运行良好,但在服务器上运行时则不然。使用 psPing ( https://technet.microsoft.com/en-us/sysinternals/psping.aspx) 我意识到应用程序端口没有返回任何东西。原来是防火墙的问题。我没有在 Windows 防火墙中启用我的应用程序端口。

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

管理工具 > 高级安全 Windows 防火墙将我的应用程序端口添加到入站规则并开始工作。

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

不知何故,应用程序端口号发生了变化,所以花了一段时间才弄清楚发生了什么 - 所以我想我会分享这种可能性,以防它节省其他人的时间......

回答by Jitendra G2

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

可能是服务器中的代理设置问题。您可以尝试通过禁用代理设置,
<defaultProxy enabled="false" />

回答by Allen Z.

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 WebClientgo through proxy server to make the call. Here is the code which worked for me. Hope it helps.

我知道这篇文章是 5 年前发布的,但我最近遇到了这个问题。这可能是由公司网络限制引起的。所以我的解决方案是让WebClient通过代理服务器拨打电话。这是对我有用的代码。希望能帮助到你。

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

回答by Illusions

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

我知道这是一篇旧帖子,但我遇到了完全相同的问题,我设法通过关闭MALWAREBYTES导致问题的程序来使用它。

回答by B.Dikmenn

Adding the following block of code in web.configsolves my problem

web.config 中添加以下代码块解决了我的问题

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