代码在 F# 中,但也标记了 C# 以备不时之需。这是一个基于 SSL 的请求,它无法解析代理名称,我都试过了 http://localhost和 https://localhost除了 127.0.0.1
代码:
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
(“启用反向代理”和上述记录本身都启用了复选框)
SSL 代理设置
Host: foo.example.com
Port: 443
“启用 SSL 代理”和记录本身都选中了两个复选框。
在 Help -> Proxying -> Install Charles Root Certificate 下,证书已安装并在钥匙串(keychain)中标记为受信任
最佳答案
这很痛苦,我意识到了错误,这是解决方案的答案。
问题的原因是一个在 nodejs 中对我有用的解决方案,它在这里造成了困惑,而没有意识到 C#/F# 为此提供了一个 WebProxy 类。 nodejs它没有 WebProxy 类(据我所知),所以概念是在 nodejs 中使用反向代理,即将请求发送到本地主机和映射到远程 url 的特定端口。
因此,请保持您的 URL 与其应指向的位置相同,并在此处使用 WebProxy 类指向 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;
关于c# - 在 F# 中启用 Web 请求通过反向代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49303420/