异常“System.Net.WebException:无法解析远程名称”的原因?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1410425/
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-09-03 13:18:26  来源:igfitidea点击:

Cause of exception "System.Net.WebException: The remote name could not be resolved"?

.netweb-services

提问by

I've got a .NET client application calling my .NET web service which in turn calls a third-party web service, for example, at 'www.foo.com'.

我有一个 .NET 客户端应用程序调用我的 .NET Web 服务,该服务又调用第三方 Web 服务,例如在“www.foo.com”。

.NET Client -> 'My Web Service' -> 'Third Party Web Service'

From Visual Studio 2008, this all works fine, that is, I'm running 'My Web Service' in localhost:someportwhich successfully calls the 'Third-Party Web Service'.

从 Visual Studio 2008 开始,这一切正常,也就是说,我正在运行“我的 Web 服务”,localhost:someport其中成功调用了“第三方 Web 服务”。

I then published 'My Web Service' onto a Windows 2003 Server box. Now when 'My Web Service' calls the 'Third-Party Web Service' I get the exception,
"System.Net.WebException: The remote name could not be resolved: 'www.foo.com'."

然后我将“我的 Web 服务”发布到 Windows 2003 Server 机器上。现在,当“我的 Web 服务”调用“第三方 Web 服务”时,出现异常
“System.Net.WebException:无法解析远程名称:“www.foo.com”。

I've tried the following:

我尝试了以下方法:

  1. On the server where I deployed 'My Web Service', I opened Internet Explorer and entered the web service address 'http://www.foo.com/somewebservice.asmx'. This was successful as the list of available web service methods were displayed.

  2. In the web.configfile of 'My Web Service', I changed the 'Third-Party Web Service' URL from the web site's name to its IP address. This failed with the exception, "System.Net.Sockets.SocketException: A socket operation was attempted to an unreachable network".

  3. I then wrote a simple Windows Forms test harness to just call the 'Third-Party Web Service'. I ran this test harness on the server and it called the 'Third-Party Web Service' successfully.

  4. I then tried a different third-party web service which gave the same behaviour as in 1, 2 and 3 above.

  5. Finally I added the IP address to the "hosts" file on the server but got the same "The remote name could not be resolved" error, albeit far quicker this time! If it was a DNS issue then I wouldn't expect my tests 1 and 3 to work.

  1. 在我部署“我的 Web 服务”的服务器上,我打开 Internet Explorer 并输入 Web 服务地址“ http://www.foo.com/somewebservice.asmx”。这是成功的,因为显示了可用的 Web 服务方法列表。

  2. web.config“我的网络服务”文件中,我将“第三方网络服务”URL 从网站名称更改为其 IP 地址。这失败并出现异常“System.Net.Sockets.SocketException:尝试对无法访问的网络进行套接字操作”。

  3. 然后我编写了一个简单的 Windows 窗体测试工具来调用“第三方 Web 服务”。我在服务器上运行了这个测试工具,它成功地调用了“第三方 Web 服务”。

  4. 然后我尝试了一个不同的第三方网络服务,它提供了与上面 1、2 和 3 相同的行为。

  5. 最后,我将 IP 地址添加到服务器上的“hosts”文件中,但得到了相同的“无法解析远程名称”错误,尽管这次要快得多!如果是 DNS 问题,那么我不希望我的测试 1 和 3 起作用。

How do I fix this problem?

我该如何解决这个问题?

回答by Kenny Evitt

From Mark's comment to his own question:

从马克的评论到他自己的问题:

It was a proxy server issue in the end. The exception message was a red herring. To fix it I created an instance of WebProxyusing the IP address and port of our proxy server:

最后是代理服务器问题。异常消息是一个红鲱鱼。为了修复它,我创建了一个WebProxy使用代理服务器的 IP 地址和端口的实例:

WebProxy proxy = new WebProxy( proxyServerIPAddress, Convert.ToInt32( proxyServerPort ) );       
myService.Proxy = proxy;

回答by iamtonyzhou

 <system.net>
        <defaultProxy>
            <proxy
               usesystemdefault = "false"
         />
        </defaultProxy>
    </system.net>

cancel your proxy setting

取消您的代理设置

回答by GeoMac

We had the some problem with two apps running on a problem server. The error would only occur when it would run under SQL Job Agent under a schedule so it was very difficult to isolate the issue. It turned out the network folks were having trouble migrating from ISA firewall to Forefront firewall. Iamtonyzhou's solution to use "usesystemdefault" in the app.config worked nicely as a workaround until the network firewall issues are resolved. I don't think I'd ever instanciate a WebProxy object because our firewall configurations are always subject to change.

我们在有问题的服务器上运行两个应用程序时遇到了一些问题。只有当它在 SQL 作业代理下按计划运行时才会发生错误,因此很难隔离问题。事实证明,网络人员在从 ISA 防火墙迁移到 Forefront 防火墙时遇到了麻烦。Iamtonyzhou 在 app.config 中使用“usesystemdefault”的解决方案在网络防火墙问题得到解决之前作为一种解决方法工作得很好。我认为我永远不会实例化 WebProxy 对象,因为我们的防火墙配置总是会发生变化。

回答by Craig from beerfriends.com

I had this issue here in 2018 on my fancy website which i just moved to a new GoDaddy VPS: System.Net.WebException: The remote name could not be resolved: 'yada.yada'

我在 2018 年在我喜欢的网站上遇到了这个问题,我刚搬到一个新的 GoDaddy VPS:System.Net.WebException:远程名称无法解析:'yada.yada'

This was happening while trying to send a SMTP, and trying to contact an external website/api. HttpRequests were failing. I checked everything. Got into IIS settings. Nothing.. All looked good (I was comparing values to my old dedicated server). I did add a Proxy 0 setting under IIS - but I don;t think that was it.

这是在尝试发送 SMTP 并尝试联系外部网站/api 时发生的。HttpRequests 失败。我检查了一切。进入IIS设置。没什么……一切看起来都不错(我正在将值与我的旧专用服务器进行比较)。我确实在 IIS 下添加了 Proxy 0 设置 - 但我不认为是这样。

Finally I noticed I couldn't use IE on the server to browse anything on the internet either. I checked all IE settings. All looked good. I recycled by websites app pool. Nothing. FINALLY, I restarted the server. When I logged back in Noticed I COULD now browse to websites using IE. SO I tried my website and FINALLY it started working. I don;t know for sure what I did, if anything. Or if the server was just so new some setting was off at initial setup and it needed a cold restart to fix. Anyway I was happy to finally get it.. I think I looked at everything 3 times.

最后我注意到我也无法在服务器上使用 IE 浏览互联网上的任何内容。我检查了所有 IE 设置。一切看起来都很好。我被网站应用程序池回收了。没有。最后,我重新启动了服务器。当我重新登录时注意我现在可以使用 IE 浏览网站。所以我尝试了我的网站,最后它开始工作了。我不知道我做了什么,如果有的话。或者,如果服务器只是这么新,一些设置在初始设置时关闭,它需要冷重启来修复。无论如何,我很高兴终于得到它.. 我想我把所有东西都看了 3 遍。

CHEERS

干杯

回答by Kenny Evitt

I think this could also indicate a DNS problem, as I'm tentatively concluding about my own recent experience with this exception. In my (recent) experience, the exception stopped being thrown (and logged by my app) 'on its own'.

我认为这也可能表明存在 DNS 问题,因为我暂时总结了我自己最近在此异常方面的经验。在我(最近)的经验中,异常停止“自行”抛出(并由我的应用程序记录)。