Right now I am using the IHttpClientFactory interface and it works perfect. Now I need to use the Refit library. Is this possible to do?
i am doing this right now:
DI:
.AddHttpClient(
"name_client",
c =>
{
c.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0");
}
)
.ConfigurePrimaryHttpMessageHandler(() =>
{
// The web site needs login with certificate digital from path
CookieContainer? cookieContainer = new();
HttpClientHandler? handler =
new() { CookieContainer = cookieContainer };
handler.ClientCertificates.Add(
DigitalCert.GetCert(context.Configuration)
);
return handler;
});
LOGIN:
public async Task Connect(string url)
{
HttpClient httpclient = clientFactory.CreateClient(clientName);
var res = await httpclient!.GetAsync(
Path.Combine(
string.Format(
"https://serverlogin.com/cgi_AUT2000/CAutInicio.cgi?reference={0}",
url // https://server2.com/
)
)
);
}
GETDATA:
public async Task GetInfo(HttpRequestMessage message)
{
message.RequestUri = new Uri("https://server2.com/cve_cgi/api/users/");
HttpClient httpclient = clientFactory.CreateClient(clientName);
HttpResponseMessage? res = await httpclient!.SendAsync(message);
}
I'm using refit like this:
DI:
services
.AddRefitClient<IInterfaceRefit>()
.ConfigureHttpClient(c =>
{
c.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0");
c.BaseAddress = new Uri("https://serverlogin.com");
})
.ConfigurePrimaryHttpMessageHandler(() =>
{
// get cert
});
public interface IInterfaceRefit
{
[Get("/cgi_AUT2000/CAutInicio.cgi")]
Task Connect(string reference);
[Get("/cvc_cgi/dte/rf_reobtencion2_folios/")]
Task<string> GetInfo(
[Body(BodySerializationMethod.UrlEncoded)] Dictionary<string, object> data
);
}
My problem with this approach is the change of base address. How could doing well? I have tried to change the base address in runtime with DelegatingHandler, but that approach doesn't work because the login was done to another endpoint.