[英]Azure web app - “HttpClient Unable to connect to the remote server”
[英]Why do I get an Unable to connect to the remote server exception from a web app and not console app?
我在啟用了匿名訪問的測試服務器上運行了一個 asmx Web 服務。
當我將 web 引用添加到控制台應用程序並調用一個簡單的 Hello World 方法時:
PivotService.PivotService p = new PivotService.PivotService();
String s = p.SayHello();
當我在 Web 應用程序的頁面加載中執行完全相同的操作時,我收到System.Net.WebException: {"Unable to connect to the remote server"}
。
內部異常是{"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。
為什么這可以從控制台應用程序而不是網絡應用程序工作?
這可能是由於應用程序之間的代理設置不同。 查看有關配置文件中默認代理元素的 MSDN 文檔。
在使用System.Net.Http
命名空間中的HttpClient
類時,我也遇到了類似的情況。 我試圖通過公司代理服務器進行網絡呼叫。
雖然此HttpClient
調用在從 Visual Studio 中的控制台應用程序調用時能夠成功解析 dns 的 IP,但從 Web API 進行調用時,dns 解析失敗。 事實證明,我必須在 web api 應用程序的 web.config 中有這個條目。
<system.net>
<defaultProxy />
</system.net>
歸功於 Mark 909 為我指明了正確方向的回答。 有關其他信息,請參閱默認代理。
首先從控制台刪除 web 引用運行你的 web 服務並復制 URL 到你的解決方案資源管理器並添加 web 引用(排除舊的)>高級>添加 web 引用添加粘貼 URL 然后添加你的 web 引用名稱然后重建你的解決方案它工作正常為我
正如@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>
使用這一行:
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
就在httpclient
對象創建之上
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.