问题描述
我无法使用 .Net WebRequest 使 IE 选项中的代理自动配置 (PAC) 按预期工作.
I'm having trouble getting Proxy Automatic Configuration (PAC) in IE options to work as expected using .Net WebRequest.
根据这篇文章:
代理检测通过 .NET 中的自动配置减轻用户的负担
默认情况下应为每个 WebRequest 设置系统代理.
The system proxy should be set by default with to each WebRequest.
proxy.js pac 文件是这样的:
That's how the proxy.js pac file looks like:
function FindProxyForURL(url, host)
{
return "PROXY ProxyServerName:3118; DIRECT;";
}
我也看了这个帖子:我应该如何设置默认代理以使用默认凭据?
建议在 app.config 中添加:
Which suggests to add this in the app.config:
<system.net>
<defaultProxy useDefaultCredentials="true" />
</system.net>
添加这个没有帮助.
我创建了一个小的控制台应用程序只是为了测试这个......它是:
I created a small console application just to test this out.. here it is:
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
instead of Proxy for address is ProxyServerName:3118
仅在使用自动配置脚本时发生...
It happens only when using auto configuration script...
我错过了什么吗?请帮忙!
Did I miss anything? Please help!
推荐答案
找到解决方案!
PAC 文件的 MIME 类型为:[Content-type: application/x-ns-proxy-autoconfig]
It is really important that the mime type of the PAC file would be: [Content-type: application/x-ns-proxy-autoconfig]
其他 mime 类型可能不起作用.
Other mime types might not work.
确保使用 fiddler2(禁用缓存)mime 类型是合适的.某些配置可能会显示 Content-Type: text/plain 这很糟糕.
Make sure using fiddler2 (with cache disabled) that the mime type is appropriate.Some configurations might show Content-Type: text/plain which is bad.
这篇关于在 .Net 中使用 IE 设置中的代理自动配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!