package com.coderbbb.blogv2.utils;import okhttp3.*;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import javax.net.ssl.HostnameVerifier;import javax.net.ssl.SSLSession;import java.util.Collections;import java.util.HashMap;import java.util.Map;import java.util.concurrent.TimeUnit;public class OkhttpUtil {private static OkHttpClient client = null;private static final Logger logger = LoggerFactory.getLogger(OkhttpUtil.class);private synchronized static void createClient() {if (client == null) {OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();okHttpBuilder.protocols(Collections.singletonList(Protocol.HTTP_1_1));okHttpBuilder.connectTimeout(60, TimeUnit.SECONDS);okHttpBuilder.readTimeout(60, TimeUnit.SECONDS);okHttpBuilder.writeTimeout(60, TimeUnit.SECONDS);okHttpBuilder.hostnameVerifier(new HostnameVerifier() {@Overridepublic boolean verify(String s, SSLSession sslSession) {//支持所有类型https请求return true;}});ConnectionPool pool = new ConnectionPool(200, 1, TimeUnit.SECONDS);okHttpBuilder.connectionPool(pool);client = okHttpBuilder.build();client.dispatcher().setMaxRequests(2000);client.dispatcher().setMaxRequestsPerHost(1000);}}public static OkHttpClient getClient() {if (client == null) {createClient();}return client;}public static String get(String url, HashMap<String, String> headerMap) {Request.Builder builder = new Request.Builder().url(url);if (headerMap != null) {for (Map.Entry<String, String> entry : headerMap.entrySet()) {builder.addHeader(entry.getKey(), entry.getValue());}}Request request = builder.build();String result = null;try {result = excute(request);} catch (Exception e) {logger.warn("http get fail:" + url + "###" + e.getMessage());}return result;}public static String postRaw(String url, String contentType, String json, HashMap<String, String> headerMap) throws Exception {RequestBody requestBody = RequestBody.create(json,MediaType.parse(contentType));Request.Builder builder = new Request.Builder().url(url).post(requestBody);if (headerMap != null) {for (Map.Entry<String, String> entry : headerMap.entrySet()) {builder.addHeader(entry.getKey(), entry.getValue());}}Request request = builder.build();String result = null;try {result = excute(request);} catch (Exception e) {logger.error("http post raw fail:" + url + "###" + e.getMessage());}return result;}public static String post(String url, HashMap<String, String> data, HashMap<String, String> headerMap) throws Exception {FormBody.Builder formBodyBuilder = new FormBody.Builder();for (Map.Entry<String, String> entry : data.entrySet()) {formBodyBuilder.add(entry.getKey(), entry.getValue());}RequestBody requestBody = formBodyBuilder.build();Request.Builder builder = new Request.Builder().url(url).post(requestBody);for (Map.Entry<String, String> entry : headerMap.entrySet()) {builder.addHeader(entry.getKey(), entry.getValue());}Request request = builder.build();String result = null;try {result = excute(request);} catch (Exception e) {logger.error("http post fail:" + url + "###" + e.getMessage());}return result;}private static String excute(Request request) throws Exception {Response response = getClient().newCall(request).execute();ResponseBody body = response.body();if (body != null) {String str = body.string();body.close();response.close();return str;}return null;}}
推荐阅读
- RabbitMQ入门使用教程
- Java业务开发常见错误
- MySQL 使用 Mysqldump 备份导入数据导致主从异常
- 容器如何使用tini作为启动进程
- 冬天使用蚕丝被的好处
- 6款超好用的macOS工具软件,提高Mac使用体验
- 内网穿透工具使用总结
- 使用 Python 将 MP4视频 转换为GIF动画
- Centos7使用ping命令对普通用户提权
- Hive安装和使用