Using Refit with Factory and Proxy

Sometimes a webhost wants you to use a proxy to communicate via http with an API for example. If you use Refit in C# with Factory implementation you can use the following code:


services.AddRefitClient()
.ConfigureHttpClient(c => c.BaseAddress = new Uri(Configuration.GetValue("The name of the value in your appsettings.json")))
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler()
{
UseProxy = true,
Proxy = new WebProxy("http://yourProxyUrl:yourProxyPort", false),
PreAuthenticate = true,
UseDefaultCredentials = false
});

Proxy on ionos 1&1 webhosting

If you want to do a http(s) call per httpclient to an external destination in a Asp.NET Application on ionos webhosting you have to use a proxy server. Otherwise you’ll get a timeout of connection trial after a while with the following error message:


System.Net.Http.HttpRequestException: A connection attempt failed

because the connected party did not properly respond after a period of

time, or established connection failed because connected host has

failed to respond

The url of the internal win proxy is http://winproxy.server.lan:3128 to allow http(s) access to external destinations.

Link to ionos entry in help documentation:

https://www.ionos.de/hilfe/index.php?id=4426