Asp.net core defaultProxy

In net 4.5, we work with proxies this way:

<system.net>
    <!-- -->
    <defaultProxy enabled="true" useDefaultCredentials="false">
        <proxy usesystemdefault="True" proxyaddress="http://192.168.1.1:8888" bypassonlocal="True" autoDetect="False" />
        <module type="CommonLibrary.Proxy.MyProxy, CommonLibrary, Version=1.0.0.0, Culture=neutral" />
    </defaultProxy>

    <settings>
        <httpWebRequest useUnsafeHeaderParsing="true" />
        <servicePointManager expect100Continue="false" />
    </settings>
</system.net>

but in asp.net core or testing we cannot find a solution similar to the one above Can someone please help me?

I really appreciate your help

Thanks, Regards

+12
source share
5 answers

Although manual proxy configuration works when you can use it HttpClientHander, by default all requests for this without code, as you could do in the .NET Framework, are currently impossible. Which is a shame if you use a library that does not provide this functionality.

, .NET Core 3.0 (.. , Linux ): https://github.com/dotnet/corefx/issues/37187

, https://github.com/dotnet/corefx/issues/36553 DefaultWebProxy HttpClient, . Core 3.0.

+6

- HTTP .net, IWebProxy. System.Net.Primitives.dll. project.json,

.

"frameworks": {
    "dotnet4.5": {
      "dependencies": {
        "System.Net.Primitives": "4.3.0"
      }
    }
}

public class MyHttpProxy : IWebProxy
    {

        public MyHttpProxy()
        {
           //here you can load it from your custom config settings 
            this.ProxyUri = new Uri(proxyUri);
        }

        public Uri ProxyUri { get; set; }

        public ICredentials Credentials { get; set; }

        public Uri GetProxy(Uri destination)
        {
            return this.ProxyUri;
        }

        public bool IsBypassed(Uri host)
        {
            //you can proxy all requests or implement bypass urls based on config settings
            return false; 

        }
    }


var config = new HttpClientHandler
{
    UseProxy = true,
    Proxy = new MyHttpProxy()
};

//then you can simply pass the config to HttpClient
var http = new HttpClient(config)

checkout https://msdn.microsoft.com/en-us/library/system.net.iwebproxy(v=vs.100).aspx

+5

, . System.Net.WebRequest.DefaultWebProxy = new WebProxy (" https://webproxy.fibe.fortis: 8080 ", true, null);

0

:

.NET Core - ( IIS). web.config.

 <system.net>
 <defaultProxy useDefaultCredentials="true">
 </defaultProxy>
 </system.net>

, . .

-3

Source: https://habr.com/ru/post/1017305/


All Articles