Add request header in callback DefaultRedirectHandler.getLocationURI

I have the following scenario:

Android HttpClient is redirected to an alternate location, the redirect url is returned with a specific url parameter to be passed as an optional HTTP header in the redirect request. The only way to intervene is to override the DefaultRedirectHandler.getLocationURI handler. Now my question is how inside

public URI getLocationURI(HttpResponse response, HttpContext context)

      

I can change the outgoing request.

+3


source to share


2 answers


You need to create an implementation RedirectHandler

and overwrite it getLocationURI()

. You can get the redirect url with

    Header locationHeader = response.getFirstHeader("location");
    String location = locationHeader.getValue();

      



extract your url parameter from location

and then you can get HttpRequest

on

    BasicHttpRequest request = (BasicHttpRequest) context.getAttribute(
                            ExecutionContext.HTTP_REQUEST);
    header.addHeader();

      

+2


source


The only way I could change for the redirect request is to abort the original one and create a new request manually in

  RedirectHandler and overwrite getLocationURI()

      



The original request aborts and throws

 IllegalStateException

      

0


source







All Articles