在.Net中使用IE设置中的代理自动配置

bon*_*dar 12 .net c# proxy pac

我无法使用.Net WebRequest在IE选项中获得代理自动配置(PAC)以按预期工作.

根据这篇文章:
代理检测在.NET中使用自动配置减轻用户负担

默认情况下,系统代理应设置为每个WebRequest.

这就是proxy.js pac文件的样子:

function FindProxyForURL(url, host)
{
  return "PROXY ProxyServerName:3118; DIRECT;";
}
Run Code Online (Sandbox Code Playgroud)

我还看了一下这篇文章:我应该如何设置默认代理以使用默认凭据?

这建议在app.config中添加:

<system.net>
  <defaultProxy useDefaultCredentials="true" />
</system.net>
Run Code Online (Sandbox Code Playgroud)

添加这个没有帮助.

我创建了一个小型控制台应用程序,只是为了测试它..这里是:

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");
    }

}
Run Code Online (Sandbox Code Playgroud)

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

代替地址的代理是ProxyServerName:3118

它仅在使用自动配置脚本时发生...

我错过了什么吗?请帮忙!

bon*_*dar 9

找到了解决方案!

PAC文件的mime类型非常重要:[Content-type:application/x-ns-proxy-autoconfig]

其他mime类型可能无法正常工作.

确保使用fiddler2(禁用缓存)mime类型是合适的.某些配置可能会显示Content-Type:text/plain,这是不好的.