OkHttp3 newCall.execute() causes hang. HttpUrlConnection code works fine

Viewed 505

My issue is the following statement causes the exiting program to hang for at least 30 seconds.

OkHttpClient client = new OkHttpClient.Builder().build();
...
Request request = new Request.Builder()
                .method("POST", RequestBody.create(mediaType, ""))
                .url(myURL)
                .addHeader("Content-Type", "application/json").build();
...
try (Response response = client.newCall(request).execute()) //! Causes hang for some reason
        {
            resBody = response.body().string();
        }

This method is passed and I get a successful response. But when the program gets to the last statement, it hangs for around 30 more seconds.

If I switch up to using HttpUrlConnection code, no issues are seen.

I tried using jstack but not sure what I am looking at. Also you cannot "tail" with jstack. Something with the OkHttp3 lib is causing this.

I am hitting an https mulesoft API in case you need some details. But the Mulesoft application logs show a clean request and response, no errors.

Any ideas?

1 Answers
Related