Fixing HttpClient warning “Invalid expires attribute” using fluent API

前端 未结 4 740
面向向阳花
面向向阳花 2020-12-28 12:53

I\'m using the fluent API of HttpClient to make a GET request:

String jsonResult = Request.Get(requestUrl)
            .connectTimeout(2000)
            .soc         


        
相关标签:
4条回答
  • 2020-12-28 13:23

    Solved with:

    System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.client.protocol.ResponseProcessCookies", "fatal");
    
    0 讨论(0)
  • 2020-12-28 13:27

    If you want to use HttpClientBuilder you can use the following sytax:

            HttpClient httpClient = HttpClientBuilder.create()
                .setDefaultRequestConfig(RequestConfig.custom()
                        .setCookieSpec(CookieSpecs.STANDARD).build()).build();
    
    0 讨论(0)
  • 2020-12-28 13:35

    The default HttpClient has difficulty understanding the latest RFC-compliant headers.

    Instead of hiding the warning, just switch to a standard cookie spec like this (HttpClient 4.4+):

    HttpClient httpClient = HttpClients.custom()
            .setDefaultRequestConfig(RequestConfig.custom()
                    .setCookieSpec(CookieSpecs.STANDARD).build())
            .build();
    
    0 讨论(0)
  • 2020-12-28 13:35

    For the developers don't want to think on the object model, wrapping the HttpClient for a RestTemplate might be used as below ( as @comiventor mentioned above especially for Spring Boot Developers).

    a Customizer for RestTemplate,

    public class RestTemplateStandardCookieCustomizer 
                             implements RestTemplateCustomizer {
    
        @Override
        public void customize(final RestTemplate restTemplate) {
    
            final HttpClient httpClient = HttpClients.custom()
                .setDefaultRequestConfig(RequestConfig.custom()
                    .setCookieSpec(CookieSpecs.STANDARD).build())
                .build();
    
            restTemplate.setRequestFactory(
              new HttpComponentsClientHttpRequestFactory(httpClient)
            );
        }
    }
    

    and using it with the RestTemplate Builder

    var restTemplate = restTemplateBuilder.additionalCustomizers(
                new RestTemplateStandardCookieCustomizer()
            ).build();
    
    0 讨论(0)
提交回复
热议问题