Commit a182bfb9f8e98ac78a1783f7e0511086a70078ce
1 parent
3e97a1d2
Exists in
master
1.okhttp add timeout config
Showing
2 changed files
with
61 additions
and
27 deletions
Show diff stats
build.gradle
@@ -54,7 +54,7 @@ uploadArchives { | @@ -54,7 +54,7 @@ uploadArchives { | ||
54 | authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) | 54 | authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) |
55 | } | 55 | } |
56 | pom.project { | 56 | pom.project { |
57 | - version '1.1.12' | 57 | + version '1.1.13' |
58 | artifactId ARTIFACT_Id | 58 | artifactId ARTIFACT_Id |
59 | groupId GROUP_ID | 59 | groupId GROUP_ID |
60 | packaging TYPE | 60 | packaging TYPE |
src/main/java/com/taover/util/UtilHttpByOkHttp.java
@@ -34,72 +34,105 @@ public class UtilHttpByOkHttp { | @@ -34,72 +34,105 @@ public class UtilHttpByOkHttp { | ||
34 | public static final String METHOD_POST = "POST"; | 34 | public static final String METHOD_POST = "POST"; |
35 | public static final String METHOD_DELETE = "DELETE"; | 35 | public static final String METHOD_DELETE = "DELETE"; |
36 | public static final String METHOD_PUT = "PUT"; | 36 | public static final String METHOD_PUT = "PUT"; |
37 | + | ||
38 | + public static String sendGet(String url, final Map<String, String> headers) throws Exception { | ||
39 | + return sendGet(url, headers, 60); | ||
40 | + } | ||
41 | + | ||
42 | + public static String sendPostForm(String url, final Map<String, String> headers, final Map<String, Object> params) throws Exception { | ||
43 | + return sendPostForm(url, headers, params, 60); | ||
44 | + } | ||
45 | + | ||
46 | + public static String sendPutForm(String url, final Map<String, String> headers, final Map<String, Object> params) throws Exception { | ||
47 | + return sendPutForm(url, headers, params, 60); | ||
48 | + } | ||
49 | + | ||
50 | + public static String sendPostString(String url, final Map<String, String> headers, final String content) throws Exception{ | ||
51 | + return sendPostString(url, headers, content, 60); | ||
52 | + } | ||
53 | + | ||
54 | + public static String sendPostJson(String url, final Map<String, String> headers, String jsonStr) throws Exception{ | ||
55 | + return sendPostJson(url, headers, jsonStr, 60); | ||
56 | + } | ||
57 | + | ||
58 | + public static String sendDelete(String url, final Map<String, String> headers) throws Exception { | ||
59 | + return sendDelete(url, headers, 60); | ||
60 | + } | ||
61 | + | ||
62 | + public static byte[] downloadFile(String url, final Map<String, String> headers) throws Exception{ | ||
63 | + return downloadFile(url, headers, 60); | ||
64 | + } | ||
37 | 65 | ||
38 | - public static String sendGet(String url, final Map<String, String> headers) throws Exception { | 66 | + public static String sendGet(String url, final Map<String, String> headers, int timeoutInSecond) throws Exception { |
39 | //http头信息拼装 | 67 | //http头信息拼装 |
40 | Request request = getRequestBuilder(url, headers).get().build(); | 68 | Request request = getRequestBuilder(url, headers).get().build(); |
41 | 69 | ||
42 | - return getHttpClient(url).newCall(request).execute().body().string(); | 70 | + return getHttpClient(url, timeoutInSecond).newCall(request).execute().body().string(); |
43 | } | 71 | } |
44 | 72 | ||
45 | - public static String sendPostForm(String url, final Map<String, String> headers, final Map<String, Object> params) throws Exception { | 73 | + public static String sendPostForm(String url, final Map<String, String> headers, final Map<String, Object> params, int timeoutInSecond) throws Exception { |
46 | //请求体信息 | 74 | //请求体信息 |
47 | RequestBody requestBody = getReqeustBody(params); | 75 | RequestBody requestBody = getReqeustBody(params); |
48 | 76 | ||
49 | //http头信息拼装 | 77 | //http头信息拼装 |
50 | Request request = getRequestBuilder(url, headers).post(requestBody).build(); | 78 | Request request = getRequestBuilder(url, headers).post(requestBody).build(); |
51 | 79 | ||
52 | - return getHttpClient(url).newCall(request).execute().body().string(); | 80 | + return getHttpClient(url, timeoutInSecond).newCall(request).execute().body().string(); |
53 | } | 81 | } |
54 | 82 | ||
55 | - public static String sendPutForm(String url, final Map<String, String> headers, final Map<String, Object> params) throws Exception { | 83 | + public static String sendPutForm(String url, final Map<String, String> headers, final Map<String, Object> params, int timeoutInSecond) throws Exception { |
56 | //请求体信息 | 84 | //请求体信息 |
57 | RequestBody requestBody = getReqeustBody(params); | 85 | RequestBody requestBody = getReqeustBody(params); |
58 | 86 | ||
59 | //http头信息拼装 | 87 | //http头信息拼装 |
60 | Request request = getRequestBuilder(url, headers).put(requestBody).build(); | 88 | Request request = getRequestBuilder(url, headers).put(requestBody).build(); |
61 | 89 | ||
62 | - return getHttpClient(url).newCall(request).execute().body().string(); | 90 | + return getHttpClient(url, timeoutInSecond).newCall(request).execute().body().string(); |
63 | } | 91 | } |
64 | 92 | ||
65 | - public static String sendPostString(String url, final Map<String, String> headers, final String content) throws Exception{ | 93 | + public static String sendPostString(String url, final Map<String, String> headers, final String content, int timeoutInSecond) throws Exception{ |
66 | //请求体 | 94 | //请求体 |
67 | - RequestBody requestBody = getReqeustBody(content); | 95 | + RequestBody requestBody = getReqeustBodyTextPlain(content); |
68 | 96 | ||
69 | //http头信息拼装 | 97 | //http头信息拼装 |
70 | Request request = getRequestBuilder(url, headers).post(requestBody).build(); | 98 | Request request = getRequestBuilder(url, headers).post(requestBody).build(); |
71 | 99 | ||
72 | - return getHttpClient(url).newCall(request).execute().body().string(); | 100 | + return getHttpClient(url, timeoutInSecond).newCall(request).execute().body().string(); |
73 | } | 101 | } |
74 | 102 | ||
75 | - public static String sendPostJson(String url, final Map<String, String> headers, String jsonStr) throws Exception{ | 103 | + public static String sendPostJson(String url, final Map<String, String> headers, String jsonStr, int timeoutInSecond) throws Exception{ |
76 | //请求体 | 104 | //请求体 |
77 | - RequestBody requestBody = getReqeustBodyJson(jsonStr); | 105 | + RequestBody requestBody = getReqeustBodyApplicationJson(jsonStr); |
78 | 106 | ||
79 | //http头信息拼装 | 107 | //http头信息拼装 |
80 | Request request = getRequestBuilder(url, headers).post(requestBody).build(); | 108 | Request request = getRequestBuilder(url, headers).post(requestBody).build(); |
81 | 109 | ||
82 | - return getHttpClient(url).newCall(request).execute().body().string(); | 110 | + return getHttpClient(url, timeoutInSecond).newCall(request).execute().body().string(); |
83 | } | 111 | } |
84 | 112 | ||
85 | - public static String sendDelete(String url, final Map<String, String> headers) throws Exception { | 113 | + public static String sendDelete(String url, final Map<String, String> headers, int timeoutInSecond) throws Exception { |
86 | //http头信息拼装 | 114 | //http头信息拼装 |
87 | Request request = getRequestBuilder(url, headers).delete().build(); | 115 | Request request = getRequestBuilder(url, headers).delete().build(); |
88 | 116 | ||
89 | - return getHttpClient(url).newCall(request).execute().body().string(); | 117 | + return getHttpClient(url, timeoutInSecond).newCall(request).execute().body().string(); |
90 | } | 118 | } |
91 | 119 | ||
92 | - public static byte[] downloadFile(String url, final Map<String, String> headers) throws Exception{ | 120 | + public static byte[] downloadFile(String url, final Map<String, String> headers, int timeoutInSecond) throws Exception{ |
93 | Request request = getRequestBuilder(url, headers).get().build(); | 121 | Request request = getRequestBuilder(url, headers).get().build(); |
94 | 122 | ||
95 | - return getHttpClient(url).newCall(request).execute().body().bytes(); | 123 | + return getHttpClient(url, timeoutInSecond).newCall(request).execute().body().bytes(); |
96 | } | 124 | } |
97 | 125 | ||
98 | - private static OkHttpClient getHttpClient(String url){ | 126 | + private static OkHttpClient getHttpClient(String url, int timeoutInSecond){ |
99 | if(url.trim().toLowerCase().startsWith("https")){ | 127 | if(url.trim().toLowerCase().startsWith("https")){ |
100 | - return buildOkHttpClientForHttps(); | 128 | + return buildOkHttpClientForHttps(timeoutInSecond); |
101 | }else{ | 129 | }else{ |
102 | - return new OkHttpClient(); | 130 | + OkHttpClient.Builder builder = new OkHttpClient.Builder(); |
131 | + builder.connectTimeout(timeoutInSecond, TimeUnit.SECONDS) | ||
132 | + .readTimeout(timeoutInSecond, TimeUnit.SECONDS) | ||
133 | + .writeTimeout(timeoutInSecond,TimeUnit.SECONDS) | ||
134 | + .retryOnConnectionFailure(true); | ||
135 | + return builder.build(); | ||
103 | } | 136 | } |
104 | } | 137 | } |
105 | 138 | ||
@@ -122,11 +155,11 @@ public class UtilHttpByOkHttp { | @@ -122,11 +155,11 @@ public class UtilHttpByOkHttp { | ||
122 | return builder.build(); | 155 | return builder.build(); |
123 | } | 156 | } |
124 | 157 | ||
125 | - private static RequestBody getReqeustBody(String content){ | 158 | + private static RequestBody getReqeustBodyTextPlain(String content){ |
126 | return RequestBody.create(MediaType.parse("text/plain;charse=utf-8"), content); | 159 | return RequestBody.create(MediaType.parse("text/plain;charse=utf-8"), content); |
127 | } | 160 | } |
128 | 161 | ||
129 | - private static RequestBody getReqeustBodyJson(String jsonStr){ | 162 | + private static RequestBody getReqeustBodyApplicationJson(String jsonStr){ |
130 | return RequestBody.create(MediaType.parse("application/json;charse=utf-8"), jsonStr); | 163 | return RequestBody.create(MediaType.parse("application/json;charse=utf-8"), jsonStr); |
131 | } | 164 | } |
132 | 165 | ||
@@ -141,11 +174,11 @@ public class UtilHttpByOkHttp { | @@ -141,11 +174,11 @@ public class UtilHttpByOkHttp { | ||
141 | return requestBuilder; | 174 | return requestBuilder; |
142 | } | 175 | } |
143 | 176 | ||
144 | - private static OkHttpClient buildOkHttpClientForHttps() { | 177 | + private static OkHttpClient buildOkHttpClientForHttps(int timeoutInSecond) { |
145 | OkHttpClient.Builder builder = new OkHttpClient.Builder(); | 178 | OkHttpClient.Builder builder = new OkHttpClient.Builder(); |
146 | - builder.connectTimeout(30, TimeUnit.SECONDS) | ||
147 | - .readTimeout(30, TimeUnit.SECONDS) | ||
148 | - .writeTimeout(30,TimeUnit.SECONDS) | 179 | + builder.connectTimeout(timeoutInSecond, TimeUnit.SECONDS) |
180 | + .readTimeout(timeoutInSecond, TimeUnit.SECONDS) | ||
181 | + .writeTimeout(timeoutInSecond,TimeUnit.SECONDS) | ||
149 | .retryOnConnectionFailure(true) | 182 | .retryOnConnectionFailure(true) |
150 | .sslSocketFactory(getTrustedSSLSocketFactory()) | 183 | .sslSocketFactory(getTrustedSSLSocketFactory()) |
151 | .hostnameVerifier(DO_NOT_VERIFY); | 184 | .hostnameVerifier(DO_NOT_VERIFY); |
@@ -262,7 +295,8 @@ public class UtilHttpByOkHttp { | @@ -262,7 +295,8 @@ public class UtilHttpByOkHttp { | ||
262 | 295 | ||
263 | try { | 296 | try { |
264 | //System.out.println(UtilHttpByOkHttp.sendPostForm(url, headerData, paramData)); | 297 | //System.out.println(UtilHttpByOkHttp.sendPostForm(url, headerData, paramData)); |
265 | - System.out.println(UtilHttpByOkHttp.sendGet("https://ssep.umsapi.com/api/v2/openapi/querycontacts?appid=songshuyun-ifun×tamp=1562239191&signature=967fd77a24cbbce872bc1506a0187983", null)); | 298 | + System.out.println( |
299 | + UtilHttpByOkHttp.sendGet("http://localhost/login/checkLogin.htm?appid=songshuyun-ifun×tamp=1562239191&signature=967fd77a24cbbce872bc1506a0187983", null, 0)); | ||
266 | } catch (Exception e) { | 300 | } catch (Exception e) { |
267 | // TODO Auto-generated catch block | 301 | // TODO Auto-generated catch block |
268 | e.printStackTrace(); | 302 | e.printStackTrace(); |