0

I would like to replace Location header in one condition. I use the following to enable Redirect

client.prepareGet(request.getUrl())
             .setFollowRedirect(true)

What I did is to extended AsyncCompletionHandler class, and from there I @Override onHeadersReceived(),

@Override
public State onHeadersReceived(final HttpResponseHeaders headers) throws Exception {

String location = headers.getHeaders().get("Location").replace("itmss", "https"
);
DefaultHttpHeaders httpHeaders = new DefaultHttpHeaders();
httpHeaders.add(HEADER, location);
httpHeaders.add(headers.getHeaders());
return inner.onHeadersReceived(new HttpResponseHeaders(httpHeaders));

}

However, I see that the request still uses itms instead of https, which means the Location header is not replaced.

The current plan is to implement a interceptor that runs before Redirect30xInterceptor.java(https://github.com/AsyncHttpClient/async-http-client/blob/dd459294434a408cff3c65c9f5c402b82d60aaa2/client/src/main/java/org/asynchttpclient/netty/handler/intercept/Redirect30xInterceptor.java), and change the url.

The other plan is to do a while loop for the following code, and whenever receiving 3XX, set a new url in the next request.

client.prepareGet(request.getUrl())
                             .addHeader("User-Agent", request.getUserAgent())
                             .setProxyServer(proxyServer)
                             .execute(new ResponseAsyncCompletionHandler(request))
                             .toCompletableFuture();

The ultimate goal is to replace itms:// to https://, so the next request uses https instead of itms.

Holm
  • 2,987
  • 3
  • 27
  • 48

1 Answers1

0

After studying more, okhttp supports interceptor easily.

If wanted to use AsyncHttp it's much more tricky. You have to implement the ResposeFilter, and from there you change the request, and setup replayRequest(true), which means a new request is made.

example code

builder.request(new RequestBuilder(nextRequest).build()).replayRequest(true);

https://github.com/AsyncHttpClient/async-http-client/blob/master/client/src/main/java/org/asynchttpclient/netty/handler/intercept/ResponseFiltersInterceptor.java#L62

Holm
  • 2,987
  • 3
  • 27
  • 48