How to make System.Net.WebProxy to not bypass local urls?
Asked Answered
S

1

7

I am trying to make Fiddler work with RestSharp witch uses System.Http.WebProxy, so I want it to be set to localhost:8888 or 127.0.0.1:8888

Here is the code:

    var webProxy = new WebProxy(new Uri("http://127.0.0.1:8888"))
    {
        BypassProxyOnLocal = false
    };

    var bypassed = webProxy.IsBypassed(new Uri("http://127.0.0.1"));
    Console.WriteLine(bypassed);

Outputs: true

MSDN states the following:

The IsBypassed method is used to determine whether to bypass the proxy server when accessing an Internet resource.

The BypassProxyOnLocal and BypassList properties control the return value of the IsBypassed method.

IsBypassed returns true under any of the following conditions:

  • If BypassProxyOnLocal is true and host is a local URI. Local requests are identified by the lack of a period (.) in the URI, as in "http://webserver/".

  • If host matches a regular expression in BypassList.

  • If Address is null.

All other conditions return false.

I don't understand why in my case it returns true, is this a bug? How to make it work then? Thanks!

Spradlin answered 11/9, 2012 at 22:7 Comment(0)
C
6

This is hard-coded behavior in the implementation of the HTTP client library in the .Net framework, mirroring the behavior of WinInet prior to Internet Explorer 9.

See Monitor traffic to localhost from IE or .NET from the Fiddler web site explains how to deal with it.

Clubby answered 11/9, 2012 at 22:11 Comment(2)
As a slight modification to what Fiddler suggests: if you use oSession.hostname = "localhost" then it will maintain your port number instead of changing it to 8081.Pomelo
(Note the usage of oSession.hostname instead of oSession.host)Pomelo

© 2022 - 2024 — McMap. All rights reserved.