簡體   English   中英

在localhost上獲取“遠程服務器無法解析”不會被defaultProxy修復?

[英]getting “remote server cannot be resolved” on localhost does not get fixed by defaultProxy?

這里是情況,我在家里的機器上測試我的localhost(沒有代理服務器和Windows默認防火牆)並檢索api.flickr.com xml文件,當我來上班時(使用ISA服務器連接)我得到“遠程服務器無法解析”所以我將這些行添加到web.config

<system.net>
  <defaultProxy>
   <proxy
    usesystemdefault="False"
    proxyaddress="http://localhost"
    bypassonlocal="True"
     />
   <bypasslist>
    <add address="[a-z]+\.flickr\.com\.+" />
   </bypasslist>

  </defaultProxy>
 </system.net>

返回: System.Net.WebException:遠程服務器返回錯誤:(404)Not Found 什么地方出了錯? 謝謝

這里有兩種可能的情況:

1:如果您正在構建客戶端應用程序(例如Console或WinForms)並且想要使用WebClient或HttpWebRequest訪問http:// localhost而沒有任何干預代理,那么bypassonlocal="True"應該可以實現此目的。 換句話說,您的app.config應如下所示:

<defaultProxy>
   <proxy
    usesystemdefault="False"
    bypassonlocal="True"
     />
  </defaultProxy>
 </system.net>

2:但是,如果你試圖讓你的ASP.NET應用程序(在http:// localhost上運行)能夠使用代理或沒有代理正確解析URI,那么你需要設置代理在web.config中正確信息(或在machine.config中,因此您不必更改應用程序的web.config),因此ASP.NET將知道您正在運行代理或未運行代理。 像這樣:

家:

<defaultProxy>
   <proxy
    usesystemdefault="False"
    bypassonlocal="True"
     />
  </defaultProxy>
 </system.net>

工作:

<defaultProxy>
   <proxy
    usesystemdefault="False"
    proxyaddress="http://yourproxyserver:8080"
    bypassonlocal="True"
     />
  </defaultProxy>
 </system.net>

它也可以使用代理自動檢測,從注冊表中獲取設置等,但我總是回避那些服務器應用程序的方法......太脆弱了。

順便說一句,如果你發現事情配置正確,但你仍然得到錯誤,我建議的第一件事是編寫一個快速測試,在你的WebClient / HttpWebRequest調用之前手動設置代理,而不是依賴於配置做到這一點。 像這樣:

WebProxy proxyObject = new WebProxy("http://proxyserver:80/",true);
WebClient wc = new WebClient();
wc.Proxy = proxyObject;
string s = wc.DownloadString ("http://www.google.com");

如果請求即使在您使用代碼時也沒有正確地通過您的工作代理,即使在您的代碼中正確配置了代理,代理本身也可能是問題所在。

WebClient中從本地下載數據沒有問題,但從Internet下載是有問題的,所以配置以下內容

在您的Web.config中添加以下行並替換您的Intenet代理地址端口

 <system.net> <defaultProxy useDefaultCredentials="true" enabled="true"> <proxy usesystemdefault="False" proxyaddress="http://your proxy address:port" bypassonlocal="True" /> </defaultProxy> <settings> <servicePointManager expect100Continue="false" /> </settings> </system.net> 

現在您的程序邏輯用於從Internet和公共URL下載內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM