1 /* 2 * Copyright (C) 2014 Square, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.squareup.okhttp.recipes; 17 18 import com.squareup.okhttp.OkHttpClient; 19 import com.squareup.okhttp.Request; 20 import com.squareup.okhttp.Response; 21 import java.util.concurrent.TimeUnit; 22 23 public final class ConfigureTimeouts { 24 private final OkHttpClient client; 25 26 public ConfigureTimeouts() throws Exception { 27 client = new OkHttpClient(); 28 client.setConnectTimeout(10, TimeUnit.SECONDS); 29 client.setWriteTimeout(10, TimeUnit.SECONDS); 30 client.setReadTimeout(30, TimeUnit.SECONDS); 31 } 32 33 public void run() throws Exception { 34 Request request = new Request.Builder() 35 .url("http://httpbin.org/delay/2") // This URL is served with a 2 second delay. 36 .build(); 37 38 Response response = client.newCall(request).execute(); 39 System.out.println("Response completed: " + response); 40 } 41 42 public static void main(String... args) throws Exception { 43 new ConfigureTimeouts().run(); 44 } 45 } 46