How to prevent apache http client from following a redirect

后端 未结 10 1787
独厮守ぢ
独厮守ぢ 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:31

    The magic, thanks to macbirdie , is:

    params.setParameter("http.protocol.handle-redirects",false);
    

    Imports are left out, here's a copy paste sample:

    HttpClient httpclient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    
    // HTTP parameters stores header etc.
    HttpParams params = new BasicHttpParams();
    params.setParameter("http.protocol.handle-redirects",false);
    
    // Create a local instance of cookie store
    CookieStore cookieStore = new BasicCookieStore();
    
    // Bind custom cookie store to the local context
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    
    // connect and receive 
    HttpGet httpget = new HttpGet("http://localhost/web/redirect");
    httpget.setParams(params);
    response = httpclient.execute(httpget, localContext);
    
    // obtain redirect target
    Header locationHeader = response.getFirstHeader("location");
    if (locationHeader != null) {
        redirectLocation = locationHeader.getValue();
      System.out.println("loaction: " + redirectLocation);
    } else {
      // The response is invalid and did not provide the new location for
      // the resource.  Report an error or possibly handle the response
      // like a 404 Not Found error.
    }
    
    0 讨论(0)
  • 2020-11-28 06:35

    this worked for me CloseableHttpClient client = HttpClientBuilder.create().disableRedirectHandling().build()

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

    Rather than use the property directly you can use:

    final HttpParams params = new BasicHttpParams();
    HttpClientParams.setRedirecting(params, false);
    
    0 讨论(0)
  • 2020-11-28 06:36

    Before HttpClient 4.3

    In older versions of the Http Client (before 4.3), we can configure what the client does with redirects as follows:

    @Test
    public void givenRedirectsAreDisabled_whenConsumingUrlWhichRedirects_thenNotRedirected() 
      throws ClientProtocolException, IOException {
        DefaultHttpClient instance = new DefaultHttpClient();
    
        HttpParams params = new BasicHttpParams();
        params.setParameter(ClientPNames.HANDLE_REDIRECTS, false);
        // HttpClientParams.setRedirecting(params, false); // alternative
    
        HttpGet httpGet = new HttpGet("http:/testabc.com");
        httpGet.setParams(params);
        CloseableHttpResponse response = instance.execute(httpGet);
    
        assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
    }
    

    Notice the alternative API that can be used to configure the redirect behavior without using setting the actual raw http.protocol.handle-redirects parameter:

    HttpClientParams.setRedirecting(params, false);
    

    Also notice that, with follow redirects disabled, we can now check that the Http Response status code is indeed 301 Moved Permanently – as it should be.

    After HttpClient 4.3

    HttpClient 4.3 introduced a cleaner, more high level API to build and configure the client:

    @Test
    public void givenRedirectsAreDisabled_whenConsumingUrlWhichRedirects_thenNotRedirected() 
      throws ClientProtocolException, IOException {
        HttpClient instance = HttpClientBuilder.create().disableRedirectHandling().build();
        HttpResponse response = instance.execute(new HttpGet("http://testabc.com"));
    
        assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
    }
    

    Note that the new API configures the entire client with this redirect behavior – not just the individual request. Reference: http://www.baeldung.com/httpclient-stop-follow-redirect

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

    Using HttpClient 4.3 and Fluent:

    final String url = "http://...";
    final HttpClient client = HttpClientBuilder.create()
        .disableRedirectHandling()
        .build();
    final Executor executor = Executor.newInstance(client);
    final HttpResponse response = executor.execute(Request.Get(url))
        .returnResponse();
    
    0 讨论(0)
  • 2020-11-28 06:52

    This worked for me:

    HttpGet httpGet = new HttpGet("www.google.com");
    HttpParams params = httpGet.getParams();
    params.setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.FALSE);
    httpGet.setParams(params);
    
    0 讨论(0)
提交回复
热议问题