mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 15:10:50 +08:00 
			
		
		
		
	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:
		
				
					committed by
					
						
						Tom Pallister
					
				
			
			
				
	
			
			
			
						parent
						
							f96adf9583
						
					
				
				
					commit
					34598f4edf
				
			@@ -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;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user