Chris Chris - 7 months ago 16
Java Question

How to prevent apache http client from following a redirect

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 automatically so that i can extract the propper header and do whatever i want with the target.

i'm looking for a simple working code sample (copy paste) that stops the automatic redirect following behaviour.

i found Preventing HttpClient 4 from following redirect, but it seems i'm too stupid to implement it with HttpClient 4.0 (GA)

Answer

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.