我无法使用 .Net WebRequest 使 IE 选项中的代理自动配置 (PAC) 按预期工作。
根据这篇文章:
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
只有在使用自动配置脚本时才会发生...
我错过了什么吗?请帮忙!
最佳答案
找到解决方案!
PAC 文件的 mime 类型非常重要:[Content-type: application/x-ns-proxy-autoconfig]
其他 MIME 类型可能不起作用。
确保使用 fiddler2(禁用缓存)mime 类型是合适的。 一些配置可能会显示 Content-Type: text/plain 这是不好的。
关于c# - 在 .Net 中使用 IE 设置中的代理自动配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13552227/