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

12

我在使用 .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;";
}

我也看了这篇文章:如何设置默认代理以使用默认凭据?

建议在 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,而不是代理服务器名称:3118。

只有在使用自动配置脚本时才会发生这种情况...

我有什么遗漏吗?请帮忙!


1
问题出在 MIME 类型。 - bondar
2个回答

10

找到了解决方案!

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

其他MIME类型可能不起作用。

确保使用禁用缓存的fiddler2,MIME类型是正确的。有些配置可能会显示Content-Type: text/plain,这是不好的。


还要确保配置文件的扩展名为.pac。 - bondar
您可以接受自己的问题。在赞成/反对计数器下方单击复选标记。这将标记问题为“已回答”,并且它不会显示在未回答问题列表中。 - Artemix

0
请确保在Package.appxmanifest中已经勾选了Internet(客户端和服务器)Private Networks(客户端和服务器)的功能。

See this

[来源]


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接