1

I have been playing with Http in Java and I faced a strange problem. Below is a piece code which executes GET method:

OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(100, TimeUnit.SECONDS);
client.setReadTimeout(100, TimeUnit.SECONDS);
Response response = null;
Request request = new Request.Builder()
        .url("https://www.celebritycruises.com/")
        .get()
        .build();
try {
    response = client.newCall(request).execute();
    System.out.println(response.code());
} catch (IOException e) {
    e.printStackTrace();
} 

So there are some sites (like the one provided in the Builder which cause:

java.net.SocketTimeoutException: timeout
at okio.Okio$3.newTimeoutException(Okio.java:207)
at okio.AsyncTimeout.exit(AsyncTimeout.java:261)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:215)
[...]
Caused by: 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 sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:940)

at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
at okio.Okio$2.read(Okio.java:139)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
... 38 more

What I do NOT understand is: why this works fine in the browser? I already set timeouts for 100seconds. I also tried with other HttpClients but with the same results (I believe deep down they use same classes anyways). Could anyone tell me where the problem lies, please?

Edit

I tried wget - worked, saved some HTML content. curl returned: (52) Empty reply from server

3
  • What is Okio? It can be related Commented Dec 5, 2017 at 14:24
  • I use OkHttpClient so I assume Okio is some OK-Input-Output kind-of-thing. Commented Dec 5, 2017 at 14:26
  • Relayed to stackoverflow.com/questions/13670692/… Commented Jul 26, 2018 at 12:38

1 Answer 1

2

You have to use an User-Agent in your request. This works:

Request request = new Request.Builder().url("https://www.celebritycruises.com/").header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0").build();
Sign up to request clarification or add additional context in comments.

2 Comments

Okay, this solved this exact problem. Could you please explain why?
Probably the web is rejecting connections without User-Agent. They should return a response code 400 or 403 instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.