How to prevent apache http client from following a redirect

后端 未结 10 1801
独厮守ぢ
独厮守ぢ 2020-11-28 06:08

I\'m connecting to a remote server with apache http client. the remote server sends a redirect, and i want to achieve that my client isn\'t following the redirect automatica

相关标签:
10条回答
  • 2020-11-28 06:52
    GetMethod method = new GetMethod(url);
    method.setFollowRedirects(false); 
    
    0 讨论(0)
  • 2020-11-28 06:54

    The default HttpClient implementation is pretty limited in configurability, but you can control the redirect handling by using HttpClient's boolean parameter http.protocol.handle-redirects.

    See the docs for reference.

    0 讨论(0)
  • 2020-11-28 06:56

    instead of call HttpClientBuilder directly, you can use

    HttpClients.custom().disableRedirectHandling().build();
    
    0 讨论(0)
  • 2020-11-28 06:57

    To avoid automatic redirection header, one must first configure the request to not do automatic redirects. You can do this by calling HttPClientParams.setRedirection and set it to false. Code snippet is shown below:

    HttpPost postURL = new HttpPost(resourceURL);
    ...
    HttpClientParams.setRedirecting(postURL.getParams(), false);
    
    0 讨论(0)
提交回复
热议问题