diff --git a/build.gradle b/build.gradle index 7ed1720..c536ea1 100644 --- a/build.gradle +++ b/build.gradle @@ -54,7 +54,7 @@ uploadArchives { authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) } pom.project { - version '1.1.12' + version '1.1.13' artifactId ARTIFACT_Id groupId GROUP_ID packaging TYPE diff --git a/src/main/java/com/taover/util/UtilHttpByOkHttp.java b/src/main/java/com/taover/util/UtilHttpByOkHttp.java index 6b41fd3..7831a58 100644 --- a/src/main/java/com/taover/util/UtilHttpByOkHttp.java +++ b/src/main/java/com/taover/util/UtilHttpByOkHttp.java @@ -34,72 +34,105 @@ public class UtilHttpByOkHttp { public static final String METHOD_POST = "POST"; public static final String METHOD_DELETE = "DELETE"; public static final String METHOD_PUT = "PUT"; + + public static String sendGet(String url, final Map headers) throws Exception { + return sendGet(url, headers, 60); + } + + public static String sendPostForm(String url, final Map headers, final Map params) throws Exception { + return sendPostForm(url, headers, params, 60); + } + + public static String sendPutForm(String url, final Map headers, final Map params) throws Exception { + return sendPutForm(url, headers, params, 60); + } + + public static String sendPostString(String url, final Map headers, final String content) throws Exception{ + return sendPostString(url, headers, content, 60); + } + + public static String sendPostJson(String url, final Map headers, String jsonStr) throws Exception{ + return sendPostJson(url, headers, jsonStr, 60); + } + + public static String sendDelete(String url, final Map headers) throws Exception { + return sendDelete(url, headers, 60); + } + + public static byte[] downloadFile(String url, final Map headers) throws Exception{ + return downloadFile(url, headers, 60); + } - public static String sendGet(String url, final Map headers) throws Exception { + public static String sendGet(String url, final Map headers, int timeoutInSecond) throws Exception { //http头信息拼装 Request request = getRequestBuilder(url, headers).get().build(); - return getHttpClient(url).newCall(request).execute().body().string(); + return getHttpClient(url, timeoutInSecond).newCall(request).execute().body().string(); } - public static String sendPostForm(String url, final Map headers, final Map params) throws Exception { + public static String sendPostForm(String url, final Map headers, final Map params, int timeoutInSecond) throws Exception { //请求体信息 RequestBody requestBody = getReqeustBody(params); //http头信息拼装 Request request = getRequestBuilder(url, headers).post(requestBody).build(); - return getHttpClient(url).newCall(request).execute().body().string(); + return getHttpClient(url, timeoutInSecond).newCall(request).execute().body().string(); } - public static String sendPutForm(String url, final Map headers, final Map params) throws Exception { + public static String sendPutForm(String url, final Map headers, final Map params, int timeoutInSecond) throws Exception { //请求体信息 RequestBody requestBody = getReqeustBody(params); //http头信息拼装 Request request = getRequestBuilder(url, headers).put(requestBody).build(); - return getHttpClient(url).newCall(request).execute().body().string(); + return getHttpClient(url, timeoutInSecond).newCall(request).execute().body().string(); } - public static String sendPostString(String url, final Map headers, final String content) throws Exception{ + public static String sendPostString(String url, final Map headers, final String content, int timeoutInSecond) throws Exception{ //请求体 - RequestBody requestBody = getReqeustBody(content); + RequestBody requestBody = getReqeustBodyTextPlain(content); //http头信息拼装 Request request = getRequestBuilder(url, headers).post(requestBody).build(); - return getHttpClient(url).newCall(request).execute().body().string(); + return getHttpClient(url, timeoutInSecond).newCall(request).execute().body().string(); } - public static String sendPostJson(String url, final Map headers, String jsonStr) throws Exception{ + public static String sendPostJson(String url, final Map headers, String jsonStr, int timeoutInSecond) throws Exception{ //请求体 - RequestBody requestBody = getReqeustBodyJson(jsonStr); + RequestBody requestBody = getReqeustBodyApplicationJson(jsonStr); //http头信息拼装 Request request = getRequestBuilder(url, headers).post(requestBody).build(); - return getHttpClient(url).newCall(request).execute().body().string(); + return getHttpClient(url, timeoutInSecond).newCall(request).execute().body().string(); } - public static String sendDelete(String url, final Map headers) throws Exception { + public static String sendDelete(String url, final Map headers, int timeoutInSecond) throws Exception { //http头信息拼装 Request request = getRequestBuilder(url, headers).delete().build(); - return getHttpClient(url).newCall(request).execute().body().string(); + return getHttpClient(url, timeoutInSecond).newCall(request).execute().body().string(); } - public static byte[] downloadFile(String url, final Map headers) throws Exception{ + public static byte[] downloadFile(String url, final Map headers, int timeoutInSecond) throws Exception{ Request request = getRequestBuilder(url, headers).get().build(); - return getHttpClient(url).newCall(request).execute().body().bytes(); + return getHttpClient(url, timeoutInSecond).newCall(request).execute().body().bytes(); } - private static OkHttpClient getHttpClient(String url){ + private static OkHttpClient getHttpClient(String url, int timeoutInSecond){ if(url.trim().toLowerCase().startsWith("https")){ - return buildOkHttpClientForHttps(); + return buildOkHttpClientForHttps(timeoutInSecond); }else{ - return new OkHttpClient(); + OkHttpClient.Builder builder = new OkHttpClient.Builder(); + builder.connectTimeout(timeoutInSecond, TimeUnit.SECONDS) + .readTimeout(timeoutInSecond, TimeUnit.SECONDS) + .writeTimeout(timeoutInSecond,TimeUnit.SECONDS) + .retryOnConnectionFailure(true); + return builder.build(); } } @@ -122,11 +155,11 @@ public class UtilHttpByOkHttp { return builder.build(); } - private static RequestBody getReqeustBody(String content){ + private static RequestBody getReqeustBodyTextPlain(String content){ return RequestBody.create(MediaType.parse("text/plain;charse=utf-8"), content); } - private static RequestBody getReqeustBodyJson(String jsonStr){ + private static RequestBody getReqeustBodyApplicationJson(String jsonStr){ return RequestBody.create(MediaType.parse("application/json;charse=utf-8"), jsonStr); } @@ -141,11 +174,11 @@ public class UtilHttpByOkHttp { return requestBuilder; } - private static OkHttpClient buildOkHttpClientForHttps() { + private static OkHttpClient buildOkHttpClientForHttps(int timeoutInSecond) { OkHttpClient.Builder builder = new OkHttpClient.Builder(); - builder.connectTimeout(30, TimeUnit.SECONDS) - .readTimeout(30, TimeUnit.SECONDS) - .writeTimeout(30,TimeUnit.SECONDS) + builder.connectTimeout(timeoutInSecond, TimeUnit.SECONDS) + .readTimeout(timeoutInSecond, TimeUnit.SECONDS) + .writeTimeout(timeoutInSecond,TimeUnit.SECONDS) .retryOnConnectionFailure(true) .sslSocketFactory(getTrustedSSLSocketFactory()) .hostnameVerifier(DO_NOT_VERIFY); @@ -262,7 +295,8 @@ public class UtilHttpByOkHttp { try { //System.out.println(UtilHttpByOkHttp.sendPostForm(url, headerData, paramData)); - System.out.println(UtilHttpByOkHttp.sendGet("https://ssep.umsapi.com/api/v2/openapi/querycontacts?appid=songshuyun-ifun×tamp=1562239191&signature=967fd77a24cbbce872bc1506a0187983", null)); + System.out.println( + UtilHttpByOkHttp.sendGet("http://localhost/login/checkLogin.htm?appid=songshuyun-ifun×tamp=1562239191&signature=967fd77a24cbbce872bc1506a0187983", null, 0)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); -- libgit2 0.21.2