为什么 WebProxy BypassProxyOnLocal 对我不起作用?

发布于 2024-08-18 01:10:53 字数 808 浏览 6 评论 0原文

我试图获取从 C# .NET 到本地地址 (localhost:3000) 的 HTTP 调用,以使用我设置的代理(这样我就可以通过 fiddler)。如果我将目标 URL 指向非本地地址,则可以使用下面的 WebProxy 方法,但是我需要将其指向我拥有的本地 Web 服务器(位于 localhost:3000),并且当我这样做时,请求不会继续通过代理。

我已包含“proxyObject.BypassProxyOnLocal = false”。这应该可以让它工作吧?关于如何强制请求通过 WebProxy 以进行针对本地地址的 http 调用,有什么建议吗?

    WebProxy proxyObject = new WebProxy("http://localhost:8888/", false);
    proxyObject.Credentials = new NetworkCredential(); 
    proxyObject.BypassProxyOnLocal = false;
    WebRequest.DefaultWebProxy = proxyObject;

    var request = (HttpWebRequest)WebRequest.Create(targetUri);

    // I also included this line as a double check
    request.Proxy = proxyObject;

然而,后续调用不会通过代理,例如我这样做时:

 var res = (HttpWebResponse)req.GetResponse();

谢谢

I'm trying to get HTTP calls I'm making from C# .NET to a local address (localhost:3000) to use the proxy I set (so I can go through fiddler). Using the below WebProxy approach works if I point the target URL to a non-local address, however I need to point it to a local web-server I have (at localhost:3000), and when I do this the request is not going through the proxy.

I have inlcuded the "proxyObject.BypassProxyOnLocal = false". This should make it work no? Any suggestions re how to force the request to go through the WebProxy for http calls targetting a local address?

    WebProxy proxyObject = new WebProxy("http://localhost:8888/", false);
    proxyObject.Credentials = new NetworkCredential(); 
    proxyObject.BypassProxyOnLocal = false;
    WebRequest.DefaultWebProxy = proxyObject;

    var request = (HttpWebRequest)WebRequest.Create(targetUri);

    // I also included this line as a double check
    request.Proxy = proxyObject;

Subsequent calls do not go through the proxy however, such as when I do:

 var res = (HttpWebResponse)req.GetResponse();

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不咬妳我踢妳 2024-08-25 01:10:53

我只需在本地主机上添加一个“点”即可解决此问题,因此我尝试访问“本地主机”,而不是访问“本地主机”。 (注意主机名末尾的点)

应得的信用:
我从这个线程 http://www.west-wind.com/weblog/posts/2009/Jan/14/Monitoring-HTTP-Output-with-Fiddler-in -NET-HTTP-Clients-and-WCF-Proxies#596591

工作正常!

I get around this simply by appending a "dot" to localhost, so instead of accessing "localhost", I try to access "localhost." (notice the dot at the end of the hostname)

Credit where credit is due:
I got this unusual tip from this thread http://www.west-wind.com/weblog/posts/2009/Jan/14/Monitoring-HTTP-Output-with-Fiddler-in-NET-HTTP-Clients-and-WCF-Proxies#596591

Works fine!

何其悲哀 2024-08-25 01:10:53

请参阅 https://docs.telerik.com/fiddler/observe-traffic 的说明/故障排除/nottraffictolocalhost

Internet Explorer 和 .NET Framework 被硬编码为不通过任何代理发送对 Localhost 的请求,并且作为代理,Fiddler 将不会接收此类流量。

最简单的解决方法是使用您的计算机名称作为主机名,而不是 Localhost 或 127.0.0.1。因此,例如,不要点击 http://localhost:8081/mytestpage.aspx,而是访问 http://machinename:8081/mytestpage.aspx。

See explanation on https://docs.telerik.com/fiddler/observe-traffic/troubleshooting/notraffictolocalhost

Internet Explorer and the .NET Framework are hardcoded not to send requests for Localhost through any proxies, and as a proxy, Fiddler will not receive such traffic.

The simplest workaround is to use your machine name as the hostname instead of Localhost or 127.0.0.1. So, for instance, rather than hitting http://localhost:8081/mytestpage.aspx, instead visit http://machinename:8081/mytestpage.aspx.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文