如何在C#中使用代理自动配置脚本(.pac)?

问题描述 投票:0回答:0

我无法在 Windows 10 上获得系统正确的代理设置。系统范围的代理配置设置为使用 pac 文件:

有一个简单的 NodeJS 服务器在端口 3000 上运行。它提供内容类型设置为 application/x-ns-proxy-autoconfig 的 pac 文件,如本topichere:

中所建议
const http = require('http');
const fs = require('fs');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'application/x-ns-proxy-autoconfig');
  fs.readFile('proxy.pac', (err, data) => {
    if (err) throw err;
    res.end(data);
  });
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

pac 文件尽可能简单,只返回本地代理服务器(WinGate)在端口 8888 上的地址:

function FindProxyForURL(url, host) {
    return "PROXY 127.0.0.1:8888";
}

当系统的代理手动设置为 127.0.0.1:8888 时,此设置工作正常,但使用 pac 脚本时未正确检测到代理。

以下代码用于获取系统的代理:

var url = new Uri("https://google.com");
var proxySettings = WebRequest.GetSystemWebProxy();
var host = proxySettings.GetProxy(url).ToString();

Debug.Log($"ProxyHost: {host}");

输出(自动代理):ProxyHost:https://google.com
输出(手动代理):ProxyHost:http://127.0.0.1:8888/

这里有什么问题以及如何使用代理自动配置脚本检测正确的代理设置?

c# proxy webrequest pac
© www.soinside.com 2019 - 2024. All rights reserved.