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

标签 c# .net proxy pac

我无法使用 .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/

相关文章:

c# - 如何为各种构造函数做Activator.CreateInstance?

c# - 分析 bool 运算符的字符串

c# - 语音合成;继续获取 "fifty"而不是 "50"

git - 在防火墙后通过 ssh 访问 git 存储库

c# - 在 FakeItEasy 中模拟 TreeView 时出错

c# - Automapper 在映射 1400 条记录时运行速度极慢

python - 为什么即使使用代理,我的真实 IP 地址仍然可见?

proxy - 是否可以从 create-react-app 代理 html get 请求到/graphql?

c# - 为什么重用 DataContext 会对性能产生负面影响?

c# - .NET MVC 应用程序的高 CPU 和内存使用率