2

I found that feign would run into an error(error message "ERROR HttpRetryException: cannot retry due to redirection, in streaming mode") if I used it to send a POST request with a body value to a server which would return an HTTP 302 status code to redirect. If the body value is not added, then feign would follow redirection with success. It seems to be a limitation from sun.net.www.protocol.http.HttpURLConnection, which blocks the POST redirection with the body. So is there any way to customize feign to make it follow redirection in such a case?

The code and error logs are shown below.

public interface FeignPostRedirectClient {
  @RequestLine("POST /redirect-server")
  void sendPostRequestWithBodyToRedirectServer(Map<String, ?> formBodyParams);  //follow redirection FAIL
  //void sendPostRequestWithBodyToRedirectServer();  //follow redirection OK
}

@Configuration
public class FeignConfiguration {
    @Bean
    public FeignPostRedirectClient feignPostRedirectClient() {
        return Feign.builder()
                .logLevel(Logger.Level.FULL)
                .logger(new Slf4jLogger())
                .encoder(new JacksonEncoder())
                .decoder(new JacksonDecoder())
                .options(new Request.Options(10, TimeUnit.SECONDS, 10, TimeUnit.SECONDS, true))
                .target(FeignPostRedirectClient.class, "http://localhost:8082");
    }
}

Map<String, String> formParams = new HashMap<>();
formParams.put("Username", "alice");
formParams.put("Password", "pass");
feignPostRedirectClient.sendPostRequestWithBodyToRedirectServer(formParams);

2022-11-21 21:53:49.880 DEBUG 3944 --- [ main] feign.Logger : [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] ---> POST http://localhost:8082/redirect-server HTTP/1.1 2022-11-21 21:53:49.880 DEBUG 3944 --- [ main] feign.Logger
: [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] Content-Length: 49 2022-11-21 21:53:49.880 DEBUG 3944 --- [
main] feign.Logger : [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] 2022-11-21 21:53:49.880 DEBUG 3944 --- [ main] feign.Logger : [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] { "Username" : "alice", "Password" : "pass" } 2022-11-21 21:53:49.881 DEBUG 3944 --- [ main] feign.Logger
: [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] ---> END HTTP (49-byte body) 2022-11-21 21:53:54.368 DEBUG 3944 --- [ main] feign.Logger : [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] <--- ERROR HttpRetryException: cannot retry due to redirection, in streaming mode (4485ms) 2022-11-21 21:53:54.371 DEBUG 3944 --- [
main] feign.Logger : [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] java.net.HttpRetryException: cannot retry due to redirection, in streaming mode at sun.net.www.protocol.http.HttpURLConnection.followRedirect0(HttpURLConnection.java:2682) at sun.net.www.protocol.http.HttpURLConnection.followRedirect(HttpURLConnection.java:2668) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1847) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1505) at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) at feign.Client$Default.convertResponse(Client.java:110) at feign.Client$Default.execute(Client.java:106) at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:102) at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:72) at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:109) at com.sun.proxy.$Proxy71.sendPostRequestWithBodyToRedirectServer(Unknown Source) at com.kingsoo.online.store.service.OnlineStoreServiceApplication.run(OnlineStoreServiceApplication.java:28) at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:771) at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:755) at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) at com.kingsoo.online.store.service.OnlineStoreServiceApplication.main(OnlineStoreServiceApplication.java:19)

2022-11-21 21:53:54.371 DEBUG 3944 --- [ main] feign.Logger : [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] <--- END ERROR 2022-11-21 21:53:54.588 DEBUG 3944 --- [
main] feign.Logger : [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] ---> RETRYING 2022-11-21 21:53:54.589 DEBUG 3944 --- [ main] feign.Logger : [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] ---> POST http://localhost:8082/redirect-server HTTP/1.1 2022-11-21 21:53:54.591 DEBUG 3944 --- [ main] feign.Logger
: [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] Content-Length: 49 2022-11-21 21:53:54.591 DEBUG 3944 --- [
main] feign.Logger : [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] 2022-11-21 21:53:54.592 DEBUG 3944 --- [ main] feign.Logger : [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] { "Username" : "alice", "Password" : "pass" } 2022-11-21 21:53:54.592 DEBUG 3944 --- [ main] feign.Logger
: [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] ---> END HTTP (49-byte body) 2022-11-21 21:54:54.606 DEBUG 3944 --- [ main] feign.Logger : [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] <--- ERROR SocketTimeoutException: Read timed out (60010ms) 2022-11-21 21:54:54.609 DEBUG 3944 --- [ main] feign.Logger
: [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] java.net.SocketTimeoutException: Read timed out at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.SocketInputStream.read(SocketInputStream.java:171) at java.net.SocketInputStream.read(SocketInputStream.java:141) at java.io.BufferedInputStream.fill(BufferedInputStream.java:246) at java.io.BufferedInputStream.read1(BufferedInputStream.java:286) at java.io.BufferedInputStream.read(BufferedInputStream.java:345) at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:743) at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1600) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1505) at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) at feign.Client$Default.convertResponse(Client.java:110) at feign.Client$Default.execute(Client.java:106) at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:102) at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:72) at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:109) at com.sun.proxy.$Proxy71.sendPostRequestWithBodyToRedirectServer(Unknown Source) at com.kingsoo.online.store.service.OnlineStoreServiceApplication.run(OnlineStoreServiceApplication.java:28) at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:771) at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:755) at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) at com.kingsoo.online.store.service.OnlineStoreServiceApplication.main(OnlineStoreServiceApplication.java:19)

2022-11-21 21:54:54.609 DEBUG 3944 --- [ main] feign.Logger : [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] <--- END ERROR 2022-11-21 21:54:54.840 DEBUG 3944 --- [
main] feign.Logger : [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] ---> RETRYING 2022-11-21 21:54:54.842 DEBUG 3944 --- [ main] feign.Logger : [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] ---> POST http://localhost:8082/redirect-server HTTP/1.1 2022-11-21 21:54:54.842 DEBUG 3944 --- [ main] feign.Logger
: [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] Content-Length: 49 2022-11-21 21:54:54.842 DEBUG 3944 --- [
main] feign.Logger : [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] 2022-11-21 21:54:54.842 DEBUG 3944 --- [ main] feign.Logger : [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] { "Username" : "alice", "Password" : "pass" } 2022-11-21 21:54:54.842 DEBUG 3944 --- [ main] feign.Logger
: [FeignPostRedirectClient#sendPostRequestWithBodyToRedirectServer] ---> END HTTP (49-byte body)

user1726366
  • 2,256
  • 4
  • 15
  • 17

0 Answers0