3

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

Greg
  • 34,042
  • 79
  • 253
  • 454
  • Seems like a bug in WCF. Check this: http://connect.microsoft.com/VisualStudio/feedback/details/358592/wcf-bypasses-the-proxy-server-for-requests-to-localhost-when-basichttpbinding-bypassproxyonlocal-is-false – Sidharth Panwar Jan 23 '12 at 06:12

2 Answers2

8

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!

Lee Francis
  • 547
  • 6
  • 15
  • Forgot about this trick. Only needed in certain browsers. Worked well for me in IE7 – PJH Jun 06 '13 at 14:54
5

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.

aKzenT
  • 7,775
  • 2
  • 36
  • 65