package com.sx.common.utils.http;
import com.alibaba.fastjson.JSON;
import com.sx.common.utils.FileUtils;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
/**
* @author Dj
* @Description: 基于okhttp3封装的http请求工具类, 下方有各种请求示例
* @date 2020年04月25日
*/
public class OkHttpUtil {
Logger logger = LoggerFactory.getLogger(OkHttpUtil.class);
// http 对象
private static volatile OkHttpClient okHttpClient = null;
// 控制访问线程数量
private static volatile Semaphore semaphore = null;
// 请求头map
private Map<String, String> headerMap;
// 请求数据参数map
private Map<String, String> paramMap;
// 请求url
private String url;
private Request.Builder request;
/**
* 初始化okHttpClient,并且允许https访问
*/
public OkHttpUtil() {
if (okHttpClient == null) {
synchronized (OkHttpUtil.class) {
if (okHttpClient == null) {
TrustManager[] trustManagers = buildTrustManagers();
okHttpClient = new OkHttpClient.Builder()
.connectTimeout(180, TimeUnit.SECONDS)
.writeTimeout(180, TimeUnit.SECONDS)
.readTimeout(180, TimeUnit.SECONDS)
.sslSocketFactory(createSSLSocketFactory(trustManagers), (X509TrustManager) trustManagers[0])
.hostnameVerifier((hostName, session) -> true)
.retryOnConnectionFailure(true)
.build();
addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
}
}
}
}
/**
* 用于异步请求时,控制访问线程数,返回结果
*
* @return
*/
private static Semaphore getSemaphoreInstance() {
//只能1个线程同时访问
synchronized (OkHttpUtil.class) {
if (semaphore == null) {
semaphore = new Semaphore(0);
}
}
return semaphore;
}
/**
* 创建OkHttpUtils 对象
*
* @return
*/
public static OkHttpUtil builder() {
return new OkHttpUtil();
}
/**
* 添加url
*
* @param url
* @return
*/
public OkHttpUtil url(String url) {
this.url = url;
return this;
}
/**
* 添加参数
*
* @param key 参数名
* @param value 参数值
* @return
*/
public OkHttpUtil addParam(String key, String value) {
if (paramMap == null) {
paramMap = new LinkedHashMap<>(16);
}
paramMap.put(key, value);
return this;
}
/**
* 添加请求头
*
* @param key 参数名
* @param value 参数值
* @return
*/
public OkHttpUtil addHeader(String key, String value) {
if (headerMap == null) {
headerMap = new LinkedHashMap<>(16);
}
headerMap.put(key, value);
return this;
}
/**
* 初始化get方法
* 并转化paramMap数据格式为 url?Key1=xxx&Key2=xxx&Key3=xxx
*
* @return
*/
public OkHttpUtil get() {
request = new Request.Builder().get();
StringBuilder urlBuilder = new StringBuilder(url);
if (paramMap != null) {
urlBuilder.append("?");
try {
for (Map.Entry<String, String
基于okhttp3封装的http请求工具类
最新推荐文章于 2024-08-12 11:26:24 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)