@Slf4j
public class OkHttpUtils {
public static final MediaType JSON = MediaType.parse("application/json;charset=utf-8");
public static final MediaType TEXT = MediaType.parse("text/plain; charset=utf-8");
public static final MediaType X_WWW_FORM = MediaType.parse("application/x-www-form-urlencoded");
public static final MediaType MIXED = MediaType.parse("multipart/mixed");
public static final MediaType ALTERNATIVE = MediaType.parse("multipart/alternative");
public static final MediaType DIGEST = MediaType.parse("multipart/digest");
public static final MediaType PARALLEL = MediaType.parse("multipart/parallel");
public static final MediaType FORM = MediaType.parse("multipart/form-data");
private static OkHttpClient client = null;
/**
* volatile 保证了不同线程对这个变量进行操作时的可见性,即一个线程修改了某个变量的值,这新值对其他线程来说是立即可见的
*/
private static volatile OkHttpUtils okHttpRequest = null;
private OkHttpUtils() {
}
/**
* 双检锁 双检锁,又叫双重校验锁,综合了懒汉式和饿汉式两者的优缺点整合而成。看上面代码实现中, 特点是在synchronized关键字内外都加了一层
* if 条件判断,这样既保证了线程安全, 又比直接上锁提高了执行效率,还节省了内存空间
*/
public static OkHttpUtils getInstance() {
if (okHttpRequest == null) {
synchronized (OkHttpUtils.class) {
if (okHttpRequest == null) {
client = new OkHttpClient.Builder()
//设置连接超时时间
.connectTimeout(50, TimeUnit.SECONDS)
//设置读取超时时间
.readTimeout(100, TimeUnit.SECONDS)
//写入超时
.writeTimeout(50, TimeUnit.SECONDS)
.build();
okHttpRequest = new OkHttpUtils();
}
}
}
return okHttpRequest;
}
public String getHeadParam(String url, Map<String, String> headerMap) throws IOException {
Builder builder = new Builder();
headerParam(builder, headerMap);
Request request = builder.url(url).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public String getHeadParamObject(String url, Map<String, Object> headerMap) throws IOException {
Builder builder = new Builder();
headerParamObject(builder, headerMap);
Request request = builder.url(url).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
/**
* @param url
* @param headerMap
* @return
* @throws IOException
*/
public String getHeadForm(String url, Map<String, String> headerMap, Map<String, String> fromParam) throws IOException {
Builder builder = new Builder();
headerParam(builder, headerMap);
FormBody.Builder fromBodyBuilder = new FormBody.Builder();
if (fromParam != null && !fromParam.isEmpty()) {
// Lambda表达式,代码简单易懂
fromParam.forEach((k, v) -> {
fromBodyBuilder.add(k, v);
});
}
Request request = builder.url(url).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
/**
* @param url
* @param json
* @return
* @throws IOException json类型请求参数
*/
public String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Builder().url(url).post(body).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
/**
* @param url
* @param bodyJson
* @param headerMap
* @return
* @throws IOException json类型请求参数 头部参数
*/
public String postHeaderParamObject(String url, String bodyJson, Map<String, Object> headerMap) throws IOException {
RequestBody body = RequestBody.create(JSON, bodyJson);
Builder builder = new Builder();
headerParamObject(builder, headerMap);
Request request = builder.url(url).post(body).build();
/*
* Call call = client.newCall(request); call.
*/
Response response = client.newCall(request).execute();
return response.body().string();
}
public String postHeaderTextParamObject(String url, String text, Map<String, Object> headerMap) throws IOException {
RequestBody body = RequestBody.create(TEXT, text);
Builder builder = new Builder();
headerParamObject(builder, headerMap);
Request request = builder.url(url).post(body).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
/**
* @param url
* @param bodyJson
* @param headerMap
* @return
* @throws IOException json类型请求参数 头部参数
*/
public String postHeaderParam(String url, String bodyJson, Map<String, String> headerMap) throws IOException {
RequestBody body = RequestBody.create(JSON, bodyJson);
Builder builder = new Builder();
headerParam(builder, headerMap);
Request request = builder.url(url).post(body).build();
/*
* Call call = client.newCall(request); call.
*/
Response response = client.newCall(request).execute();
log.info("response:{}", response);
return response.body().string();
}
public String postForm(String url, Map<String, String> fromParam) throws IOException {
FormBody.Builder fromBodyBuilder = new FormBody.Builder();
if (fromParam != null && !fromParam.isEmpty()) {
// Lambda表达式,代码简单易懂
fromParam.forEach((k, v) -> {
fromBodyBuilder.add(k, v);
});
}
RequestBody body = fromBodyBuilder.build();
Request request = new Builder().url(url).post(body).build();
/*
* Call call = client.newCall(request); call.
*/
Response response = client.newCall(request).execute();
return response.body().string();
}
public String postHeaderForm(String url, Map<String, String> headerMap, Map<String, String> fromParam) throws IOException {
FormBody.Builder fromBodyBuilder = new FormBody.Builder();
formParam(fromBodyBuilder, fromParam);
RequestBody body = fromBodyBuilder.build();
Builder builder = new Builder();
headerParam(builder, headerMap);
Request request = builder.url(url).post(body).build();
/*
* Call call = client.newCall(request); call.
*/
Response response = client.newCall(request).execute();
return response.body().string();
}
public String postHeaderObjectForm(String url, Map<String, Object> headerMap, Map<String, String> fromParam) throws IOException {
FormBody.Builder fromBodyBuilder = new FormBody.Builder();
formParam(fromBodyBuilder, fromParam);
RequestBody body = fromBodyBuilder.build();
Builder builder = new Builder();
headerParamObject(builder, headerMap);
Request request = builder.url(url).post(body).build();
/*
* Call call = client.newCall(request); call.
*/
Response response = client.newCall(request).execute();
return response.body().string();
}
/**
* @param builder
* @param headerMap header 参数初始化
*/
private void headerParam(Builder builder, Map<String, String> headerMap) {
if (headerMap != null && !headerMap.isEmpty()) {
// Lambda表达式,代码简单易懂
headerMap.forEach(builder::addHeader);
}
}
/**
* @param builder
* @param headerMap header 参数初始化
*/
private void headerParamObject(Builder builder, Map<String, Object> headerMap) {
if (headerMap != null && !headerMap.isEmpty()) {
// Lambda表达式,代码简单易懂
headerMap.forEach((k, v) -> {
builder.addHeader(k, v.toString());
});
}
}
/**
* @param
* @param fromParam 参数初始化
*/
private void formParam(FormBody.Builder fromBodyBuilder, Map<String, String> fromParam) {
if (fromParam != null && !fromParam.isEmpty()) {
// Lambda表达式,代码简单易懂
fromParam.forEach((k, v) -> {
if (StrUtil.isNotEmpty(v)) {
fromBodyBuilder.add(k, v);
}
});
}
}
/**
* @param url
* @param bodyJson
* @param headerMap
* @return
* @throws IOException json类型请求参数 头部参数
*/
public String deleteHeaderParamObject(String url, String bodyJson, Map<String, Object> headerMap) throws IOException {
RequestBody body = RequestBody.create(JSON, bodyJson);
Builder builder = new Builder();
headerParamObject(builder, headerMap);
Request request = builder.url(url).delete(body).build();
/*
* Call call = client.newCall(request); call.
*/
Response response = client.newCall(request).execute();
return response.body().string();
}
public String deleteHeaderParam(String url, Map<String, Object> headerMap) throws IOException {
Builder builder = new Builder();
headerParamObject(builder, headerMap);
Request request = builder.url(url).delete().build();
/*
* Call call = client.newCall(request); call.
*/
Response response = client.newCall(request).execute();
return response.body().string();
}
public static void main(String[] args) {
try {
OkHttpUtils instance = OkHttpUtils.getInstance();
//构建头信息
Map<String, String> headerMap = new HashMap<>(16);
//body信息
Map<String, Object> bodyMap = new HashMap<>(16);
String response = instance.postHeaderParam("http://localhost:8080", JSONObject.toJSONString(bodyMap), headerMap);
} catch (IOException e) {
e.printStackTrace();
}
}
}