基于okhttp3封装的HttpUtils
HttpUtils
package thread.test.utils;
import com.google.common.collect.Maps;
import okhttp3.*;
import okhttp3.Request.Builder;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* @author: liubowen
* @date: 2019-04-17 11:32
* @description:
*/
public class HttpUtils {
private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
private static final Long READ_TIMEOUT = 5 * 60 * 1000L;
private static final MediaType JSON = MediaType.parse("application/json;charset=utf-8");
public static String doGet(String url) {
return doGet(url, null);
}
public static String doGet(String url, long readTimeout) {
return doGet(url, null, readTimeout);
}
public static String doGet(String url, Map<String, String> headers) {
return doGet(url, headers, READ_TIMEOUT);
}
public static String doGet(String url, Map<String, String> headers, long readTimeout) {
try {
OkHttpClient okHttpClient = okHttpClient(readTimeout);
Builder builder = okHttpClientBuilder(url, headers);
final Request request = builder.build();
final Call call = okHttpClient.newCall(request);
Response response = call.execute();
ResponseBody body = response.body();
String string = body.string();
return string;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String doPostByFrom(String url) {
return doPostByFrom(url, null);
}
public static String doPostByFrom(String url, long readTimeout) {
return doPostByFrom(url, null, readTimeout);
}
public static String doPostByFrom(String url, Map<String, String> params) {
return doPostByFrom(url, null, params);
}
public static String doPostByFrom(String url, Map<String, String> params, long readTimeout) {
return doPostByFrom(url, null, params, readTimeout);
}
public static String doPostByFrom(String url, Map<String, String> headers, Map<String, String> params) {
return doPostByFrom(url, headers, params, READ_TIMEOUT);
}
public static String doPostByFrom(String url, Map<String, String> headers, Map<String, String> params,
long readTimeout) {
return doPostByFrom(url, headers, params, null, null, null, readTimeout);
}
public static String doPostByFrom(String url, String fileName, File file, MediaType fileType) {
return doPostByFrom(url, fileName, file, fileType, READ_TIMEOUT);
}
public static String doPostByFrom(String url, String fileName, File file, MediaType fileType, long readTimeout) {
return doPostByFrom(url, null, fileName, file, fileType, readTimeout);
}
public static String doPostByFrom(String url, Map<String, String> headers, String fileName, File file,
MediaType fileType, long readTimeout) {
return doPostByFrom(url, headers, null, fileName, file, fileType, readTimeout);
}
public static String doPostByFrom(String url, Map<String, String> headers, Map<String, String> params,
String fileName, File file, MediaType fileType, long readTimeout) {
try {
OkHttpClient okHttpClient = okHttpClient(readTimeout);
Builder builder = okHttpClientBuilder(url, headers);
if (!MapUtils.isEmpty(params)) {
MultipartBody.Builder requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
params.forEach((name, value) -> {
requestBody.addFormDataPart(name, value);
});
if (!StringUtils.isEmpty(fileName) && file != null && fileType != null) {
RequestBody body = RequestBody.create(fileType, file);
String filename = file.getName();
// 参数分别为, 请求key ,文件名称 , RequestBody
requestBody.addFormDataPart(fileName, filename, body);
}
builder.post(requestBody.build());
}
final Request request = builder.build();
final Call call = okHttpClient.newCall(request);
Response response = call.execute();
ResponseBody body = response.body();
String string = body.string();
return string;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String doPostByJson(String url) {
return doPostByJson(url, Maps.newHashMap(), null, READ_TIMEOUT);
}
public static String doPostByJson(String url, long readTimeout) {
return doPostByJson(url, Maps.newHashMap(), null, readTimeout);
}
public static String doPostByJson(String url, String jsonString) {
return doPostByJson(url, Maps.newHashMap(), jsonString, READ_TIMEOUT);
}
public static String doPostByJson(String url, String jsonString, long readTimeout) {
return doPostByJson(url, Maps.newHashMap(), jsonString, readTimeout);
}
public static String doPostByJson(String url, Map<String, String> headers, String jsonString, long readTimeout) {
try {
OkHttpClient okHttpClient = okHttpClient(readTimeout);
Builder builder = okHttpClientBuilder(url, headers);
if (!StringUtils.isEmpty(jsonString)) {
RequestBody requestBody = RequestBody.create(JSON, jsonString);
builder.post(requestBody);
}
final Request request = builder.build();
final Call call = okHttpClient.newCall(request);
Response response = call.execute();
ResponseBody body = response.body();
String string = body.string();
return string;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static OkHttpClient okHttpClient(long readTimeout) {
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
// okHttpClientBuilder.connectTimeout(60 * 1000, TimeUnit.MILLISECONDS)
// .readTimeout(5 * 60 * 1000, TimeUnit.MILLISECONDS)
// .writeTimeout(5 * 60 * 1000, TimeUnit.MILLISECONDS);
okHttpClientBuilder.readTimeout(readTimeout, TimeUnit.MILLISECONDS);
OkHttpClient okHttpClient = okHttpClientBuilder.build();
return okHttpClient;
}
private static Builder okHttpClientBuilder(String url, Map<String, String> headers) {
Builder builder = new Builder();
builder.url(url);
if (!MapUtils.isEmpty(headers)) {
headers.forEach((name, value) -> {
builder.addHeader(name, value);
});
}
return builder;
}
}
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.1-jre</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
本人第一次发贴 希望大家多多指导, 谢谢🙏