2012-11-25 34 views
11

我無法在IE選項中使用代理自動配置(PAC)按預期方式使用.Net WebRequest進行工作。在.Net中使用代理服務器自動配置從IE設置中

根據這篇文章:
Proxy Detection Take the Burden Off Users with Automatic Configuration in .NET

系統代理應該默認與每個WebRequest的設置。

那是的proxy.js PAC文件的樣子:

function FindProxyForURL(url, host) 
{ 
    return "PROXY ProxyServerName:3118; DIRECT;"; 
} 

我也看了看這個帖子:How should I set the default proxy to use default credentials?

這表明在app.config中添加此:

<system.net> 
    <defaultProxy useDefaultCredentials="true" /> 
</system.net> 

添加這沒有幫助。

我創建了一個小型控制檯應用程序只是爲了測試這一點..這就是:

static void Main(string[] args) 
{ 
    HttpWebRequest request = null; 
    try 
    {    
     String resolvedAddress = WebRequest.DefaultWebProxy.GetProxy(new Uri("http://www.google.com")).ToString(); 
     Console.WriteLine("Proxy for address is: " + resolvedAddress); 

     Uri m_URLToTest = new Uri("http://www.google.com"); 
     request = WebRequest.Create(m_URLToTest) as HttpWebRequest; 
     request.Method = "GET"; 
     request.KeepAlive = false; 
     request.Timeout = 5000; 
     request.Proxy = WebRequest.DefaultWebProxy; 
     WebResponse response = request.GetResponse(); 
     StreamReader reader = new StreamReader(response.GetResponseStream()); 
     string message = reader.ReadToEnd(); 
    } 
    catch (Exception ex) 
    { 
     Console.Write("Exception"); 
    } 

} 

輸出: 代理的地址是http://www.google.com

,而不是代理的地址是ProxyServerName:3118

只有在使用自動配置腳本時纔會發生......

我錯過了什麼嗎?請幫忙!

+0

的問題是與MIME類型 – bondar

回答

8

找到解決方案!

這是非常重要的是,PAC文件的MIME類型是:內容類型:應用程序/ x-NS-代理自動配置]

其他MIME類型可能無法正常工作。

確保使用fiddler2(禁用緩存)MIME類型是合適的。 某些配置可能會顯示Content-Type:text/plain,這是不好的。

+0

還要確保配置文件擴展名的.pac – bondar

+0

您可以接受你自己的問題。點擊upvote/downvote計數器下的勾號。這會將問題標記爲「已回答」,並且不會顯示在沒有回答的問題列表中。 – Artemix

0

請確保您已選中Package.appxmanifest中的Internet (Client & Server)Private Networks (Client & Server)功能。

See this

[Source]

相關問題