made http client work under full .net 46 (#367)

* made http client work under full .net 46

* Changed the way the requests without body are checked & comments

* fixed a type
This commit is contained in:
Tsirkin Evgeny 2018-05-25 00:39:27 +03:00 committed by Tom Pallister
parent f96adf9583
commit 34598f4edf
2 changed files with 39 additions and 8 deletions

View File

@ -42,15 +42,29 @@ namespace Ocelot.Requester
{ {
return httpClient; return httpClient;
} }
bool useCookies = context.DownstreamReRoute.HttpHandlerOptions.UseCookieContainer;
var httpclientHandler = new HttpClientHandler HttpClientHandler httpclientHandler;
// Dont' create the CookieContainer if UseCookies is not set ot the HttpClient will complain
// under .Net Full Framework
if (useCookies)
{ {
AllowAutoRedirect = context.DownstreamReRoute.HttpHandlerOptions.AllowAutoRedirect, httpclientHandler = new HttpClientHandler
UseCookies = context.DownstreamReRoute.HttpHandlerOptions.UseCookieContainer, {
CookieContainer = new CookieContainer() AllowAutoRedirect = context.DownstreamReRoute.HttpHandlerOptions.AllowAutoRedirect,
}; UseCookies = context.DownstreamReRoute.HttpHandlerOptions.UseCookieContainer,
CookieContainer = new CookieContainer()
};
}
else
{
httpclientHandler = new HttpClientHandler
{
AllowAutoRedirect = context.DownstreamReRoute.HttpHandlerOptions.AllowAutoRedirect,
UseCookies = context.DownstreamReRoute.HttpHandlerOptions.UseCookieContainer,
};
}
if(context.DownstreamReRoute.DangerousAcceptAnyServerCertificateValidator) if (context.DownstreamReRoute.DangerousAcceptAnyServerCertificateValidator)
{ {
httpclientHandler.ServerCertificateCustomValidationCallback = (request, certificate, chain, errors) => true; httpclientHandler.ServerCertificateCustomValidationCallback = (request, certificate, chain, errors) => true;

View File

@ -32,7 +32,24 @@ namespace Ocelot.Requester
try try
{ {
var response = await httpClient.SendAsync(context.DownstreamRequest.ToHttpRequestMessage()); var message = context.DownstreamRequest.ToHttpRequestMessage();
/**
* According to https://tools.ietf.org/html/rfc7231
* GET,HEAD,DELETE,CONNECT,TRACE
* Can have body but server can reject the request.
* And MS HttpClient in Full Framework actually rejects it.
* see #366 issue
**/
if (message.Method == HttpMethod.Get ||
message.Method == HttpMethod.Head ||
message.Method == HttpMethod.Delete ||
message.Method == HttpMethod.Trace)
{
message.Content = null;
}
_logger.LogDebug(string.Format("Sending {0}", message));
var response = await httpClient.SendAsync(message);
return new OkResponse<HttpResponseMessage>(response); return new OkResponse<HttpResponseMessage>(response);
} }
catch (TimeoutRejectedException exception) catch (TimeoutRejectedException exception)