Skip to content

Commit 80ac61c

Browse files
committed
set timeouts for OkHttpClient
1 parent a33925b commit 80ac61c

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

okhttp-examples/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<dependency>
1919
<groupId>com.squareup.okhttp3</groupId>
2020
<artifactId>okhttp</artifactId>
21-
<version>3.4.1</version>
21+
<version>3.4.2</version>
2222
</dependency>
2323
<dependency>
2424
<groupId>junit</groupId>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.howtoprogram.okhttp.timeout;
2+
3+
import java.io.IOException;
4+
import java.util.concurrent.TimeUnit;
5+
6+
import okhttp3.OkHttpClient;
7+
import okhttp3.Request;
8+
import okhttp3.Response;
9+
10+
public class CrawlService {
11+
12+
public void doCrawl(String crawlURL) throws IOException {
13+
14+
OkHttpClient client = new OkHttpClient.Builder()
15+
.connectTimeout(100, TimeUnit.SECONDS)
16+
.writeTimeout(10, TimeUnit.SECONDS)
17+
.readTimeout(20, TimeUnit.SECONDS).build();
18+
19+
// creates new request
20+
Request request = new Request.Builder()
21+
.url(crawlURL)
22+
.build();
23+
Response response = client.newCall(request).execute();
24+
25+
if (!response.isSuccessful()) {
26+
throw new IOException("Unexpected code " + response);
27+
}
28+
29+
}
30+
31+
}

0 commit comments

Comments
 (0)