import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class OkHttpUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(OkHttpUtils.class);
private static final OkHttpClient okHttpClient;
private OkHttpUtils() {
}
/**
* 初始化okHttpClient,并且允许https访问
*/
static {
TrustManager[] trustManagers = buildTrustManagers();
okHttpClient = new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.sslSocketFactory(createSSLSocketFactory(trustManagers), (X509TrustManager) trustManagers[0])
.hostnameVerifier((hostName, session) -> true)
.retryOnConnectionFailure(true)
.build();
}
/**
* 表单请求
*
* @param paramMapObj 参数
* @return string
*/
public static String postForm(String url, Map<String, String> paramMapObj) {
FormBody.Builder formBody = new FormBody.Builder();
if (paramMapObj != null) {
paramMapObj.forEach(formBody::add);
}
RequestBody requestBody = formBody.build();
Request.Builder request = new Request.Builder()
.addHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8")
.post(requestBody).url(url);
try (Response response = okHttpClient.newCall(request.build()).execute()) {
boolean successful = response.isSuccessful();
Assert.isTrue(successful, "请求返回失败");
assert response.body() != null;
return response.body().string();
} catch (Exception e) {
LOGGER.info("请求异常{},详情{}", url, e.getStackTrace());
throw new RuntimeException(e);
// return "请求失败:" + e.getMessage();
}
}
/**
* json参数请求
*
* @param paramJson 参数值
* @return
*/
public static String postJson(String url, String paramJson) {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=UTF-8"), paramJson);
Request.Builder request = new Request.Builder().post(requestBody).url(url);
try (Response response = okHttpClient.newCall(request.build()).execute()) {
boolean successful = response.isSuccessful();
assert response.body() != null;
return response.body().string();
} catch (Exception e) {
LOGGER.info("请求异常{},详情{}", e.getMessage(), e.getStackTrace());
return "请求失败:" + e.getMessage();
}
}
/**
* 异步json请求
*
* @param url
* @param paramJson
* @param callBack
*/
public static void postJsonAsync(String url, String paramJson, ICallBack callBack) {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=UTF-8"), paramJson);
Request.Builder request = new Request.Builder().post(requestBody).url(url);
okHttpClient.newCall(request.build()).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
callBack.onFailure(call, e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
assert response.body() != null;
callBack.onSuccessful(call, response.body().string());
}
});
}
/**
* 生成安全套接字工厂,用于https请求的证书跳过
*
* @return
*/
private static SSLSocketFactory createSSLSocketFactory(TrustManager[] trustAllCerts) {
SSLSocketFactory ssfFactory = null;
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
ssfFactory = sc.getSocketFactory();
} catch (Exception e) {
e.printStackTrace();
}
return ssfFactory;
}
private static TrustManager[] buildTrustManagers() {
return new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
};
}
/**
* 自定义一个接口回调
*/
public interface ICallBack {
void onSuccessful(Call call, String data);
void onFailure(Call call, String errorMsg);
}
OkHttpUtils 版本2
原创
©著作权归作者所有:来自51CTO博客作者wx64fad51b9ed9b的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:URL工具类
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
okhttputils 操作cookie
本文只记录于鸿洋的okhttputils,其他框架只具有参考价值。cookie持久化 CookieJarImpl cookieJar =
okhttputils 操作cookie okhttputils cookie持久化 获取cookie 清除cookie -
OkHttpUtils请求回调封装
【代码】OkHttpUtils请求回调封装。
java servlet http ide json -
OkhttpUtils的https请求 ssl证书验证
设置可访问所有的https网站 HttpsUtils
android 简书 ide -
OkHttpUtils 请求及解析 需要Gson依赖 ResultCallback
【代码】OkHttpUtils 请求及解析 需要Gson依赖。
android java 开发语言 ide Data -
RIP 协议(版本2)-2
实验 6:RIPv2 认证和触发更新 1.实验目的 通过本实验可以掌握: (1)RIPv2 明文认证的配置和匹配原则 (2)RIP
路由器 认证 chain interface 实验目的