[英]Why do I get an Unable to connect to the remote server exception from a web app and not console app?
I have a asmx web service running on a test server that has anonymous access enabled.我在启用了匿名访问的测试服务器上运行了一个 asmx Web 服务。
When I add the web reference to a console application and call a simple Hello World method like so:当我将 web 引用添加到控制台应用程序并调用一个简单的 Hello World 方法时:
PivotService.PivotService p = new PivotService.PivotService();
String s = p.SayHello();
When I do exactly the same thing in the page load of a web application I get a System.Net.WebException: {"Unable to connect to the remote server"}
.当我在 Web 应用程序的页面加载中执行完全相同的操作时,我收到System.Net.WebException: {"Unable to connect to the remote server"}
。
The inner exception is {"No connection could be made because the target machine actively refused it 127.0.0.1:8888"}
, errorCode 10061.内部异常是{"No connection could be made because the target machine actively refused it 127.0.0.1:8888"}
active denied {"No connection could be made because the target machine actively refused it 127.0.0.1:8888"}
10061。
Why would this work from a console app and not a web app?为什么这可以从控制台应用程序而不是网络应用程序工作?
It could be due to differences in the proxy settings between applications.这可能是由于应用程序之间的代理设置不同。 Check out the MSDN documentation around the Default Proxy element in config file.查看有关配置文件中默认代理元素的 MSDN 文档。
I too was in a similar situation while using HttpClient
class from System.Net.Http
namespace.在使用System.Net.Http
命名空间中的HttpClient
类时,我也遇到了类似的情况。 I was trying to make a network call which goes through the company proxy server.我试图通过公司代理服务器进行网络呼叫。
While this HttpClient
call was able to successfully resolve the IP of the dns when called from a console app in visual studio the dns resolution failed when the call was made from a Web API.虽然此HttpClient
调用在从 Visual Studio 中的控制台应用程序调用时能够成功解析 dns 的 IP,但从 Web API 进行调用时,dns 解析失败。 And it turned out that I had to have this entry in the web.config of the web api app.事实证明,我必须在 web api 应用程序的 web.config 中有这个条目。
<system.net>
<defaultProxy />
</system.net>
Credit to Mark 909's answer for pointing me in right direction.归功于 Mark 909 为我指明了正确方向的回答。 Please see Default Proxy for additional information.有关其他信息,请参阅默认代理。
首先从控制台删除 web 引用运行你的 web 服务并复制 URL 到你的解决方案资源管理器并添加 web 引用(排除旧的)>高级>添加 web 引用添加粘贴 URL 然后添加你的 web 引用名称然后重建你的解决方案它工作正常为我
As @R Kiran Kumar suggests, I had to also add the proxy values:正如@R Kiran Kumar 所建议的,我还必须添加代理值:
<system.net>
<defaultProxy>
<proxy usesystemdefault="False"
proxyaddress="Specify Your Proxy Address: Specify Port Number"
// Example, "http://192.168.0.10:1234"
bypassonlocal="False"/>
</defaultProxy>
</system.net>
Use this line :使用这一行:
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
Just above the httpclient
object creation就在httpclient
对象创建之上
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.