Search code examples
javaspringhttpclientapache-httpclient-4.xapache-httpclient-5.x

setRelativeRedirectsAllowed() in RequestConfig apache-httpclient 5.x


I was migrating the usage of apache httpclient 4.x to httpclient 5.x version and there a lot of changes in the classes and methods. This particular question is about setRelativeRedirectsAllowed() method that was present in RequestConfig class upto 4.x version. As I am moving httpclient 5.x . I was not able to find the suitable replacement for it.

final RequestConfig requestConfig = RequestConfig
          .custom()
          .setCookieSpec(CookieSpecs.STANDARD)
          .setConnectionRequestTimeout(...)
          .setConnectTimeout(...)
          .setRedirectsEnabled(true)
          .setRelativeRedirectsAllowed(true)
          .setSocketTimeout(...)
          .build();

final CloseableHttpClient client = HttpClientBuilder
          .create()
          .setDefaultRequestConfig(requestConfig)
          .build()

final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(client);

I have provided the sample example of how we are using it in our code.

I have tried going through the migration document, there was not any mention of it. I also looked at the code of apache-httpcomponents 4.x and spring 5.3.x to see if I could find how exactly it was being used, I couldn't.

My questions are, Is there a suitable replacement present for that method (setRelativeRedirectsAllowed()) in httpclient 5.x, If not, is there a way to custom implement it?


Solution

  • You don't need anything as a replacement. It supports both relative and absolute redirects out-of-the-box now. This has been addressed in this commit (you gotta love some code archaeology :) ).

    So in short there is no replacement and relative redirects should just work (tm).