在 F# 中启用 Web 请求通过反向代理

Enabling Web Requests go via reverse proxy in F#

代码在 F# 中,但也标记了 C# 以备不时之需。这是一个基于 SSL 的请求,它无法解析代理名称,除了 127.0.0.1

之外,我还尝试了 http://localhost and https://localhost

代码:

let request = WebRequest.Create("https://foo.example.com") :?> HttpWebRequest

let myproxy = WebProxy("http://127.0.0.1", 60103);
myproxy.BypassProxyOnLocal <- false;
request.Proxy <- myproxy;

错误:

System.Net.WebException: An error occurred while sending the request. Couldn't resolve proxy name ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.CurlException: Couldn't resolve proxy name

我的配置

Reverse Proxy Settings
Local port: 60103
Remote Hot: foo.example.com
Remote Port: 443

(已为 "Enable Reverse Proxies" 和上述记录本身启用复选框)

SSL 代理设置

Host: foo.example.com
Port: 443

"Enable SSL Proxying" 和记录本身都选中了两个复选框。

在帮助 -> 代理 -> 安装 Charles 根证书下,证书已安装并在钥匙串中标记为受信任

这很痛苦,我意识到了错误,这是解决方案的答案。

问题的原因是一个在 nodejs 中对我有用的解决方案,它在这里引起了混乱,而没有意识到 C#/F# 为此提供了一个 WebProxy class。 nodejs 没有 WebProxy class (据我所知)所以概念是在 nodejs 中使用反向代理,即将请求发送到本地主机和映射到的特定端口远程 url.

因此,请保持您的 URL 与应指向的位置相同,并在此处使用 WebProxy class 指向 Charles Proxy 正在侦听和拦截请求的本地主机和端口(在我的例子中为 8888) .

感谢 Fyodor Soikin 指出不要使用 url(http:// 等)而只使用主机名。

// let url = "http://localhost:60103"; // don't do this, reverse proxy settings
let url = "https://example.com"; // keep the url intact

let request = WebRequest.Create(url) :?> HttpWebRequest

let myproxy = WebProxy("localhost", 8888); // port where Charles proxy is running
myproxy.BypassProxyOnLocal <- false;
request.Proxy <- myproxy;