You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
322 lines
12 KiB
322 lines
12 KiB
package com.ynxbd.common.helper.http;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.aliyun.tea.okhttp.OkHttpClientBuilder;
|
|
import com.ynxbd.common.helper.common.ErrorHelper;
|
|
import com.ynxbd.common.helper.common.JsonHelper;
|
|
import com.ynxbd.common.result.JsonResult;
|
|
import com.ynxbd.common.result.JsonResultEnum;
|
|
import com.ynxbd.wx.utils.DesEncryptHelper;
|
|
import com.ynxbd.wx.wxfactory.utils.XmlHelper;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import okhttp3.*;
|
|
|
|
import java.io.IOException;
|
|
import java.net.URL;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
@Slf4j
|
|
public class OkHttpHelper {
|
|
|
|
@FunctionalInterface
|
|
public interface Header {
|
|
void setHeaders(Headers.Builder headers);
|
|
}
|
|
|
|
@FunctionalInterface
|
|
public interface MapParams {
|
|
void setParams(Map<String, Object> map);
|
|
}
|
|
|
|
public static final MediaType TYPE_XML = MediaType.parse("application/xml;encoding=utf-8");
|
|
public static final MediaType TYPE_JSON = MediaType.parse("application/json;encoding=utf-8");
|
|
public static final MediaType TYPE_FORM = MediaType.parse("application/x-www-form-urlencoded;encoding=utf-8");
|
|
|
|
private static final ConcurrentHashMap<String, OkHttpClient> clients = new ConcurrentHashMap<>();
|
|
|
|
public static OkHttpClient getOkHttpClient(String requestUrl) {
|
|
if (requestUrl == null) {
|
|
return null;
|
|
}
|
|
try {
|
|
URL url = new URL(requestUrl);
|
|
String key;
|
|
key = getClientKey(url.getHost(), url.getPort());
|
|
OkHttpClient client = clients.get(key);
|
|
if (null == client) {
|
|
client = creatClient();
|
|
clients.put(key, client);
|
|
}
|
|
return client;
|
|
} catch (Exception e) {
|
|
ErrorHelper.println(e);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static OkHttpClient creatClient() {
|
|
OkHttpClientBuilder builder = new OkHttpClientBuilder();
|
|
Map<String, Object> runtime = new HashMap<>();
|
|
runtime.put("ignoreSSL", true);
|
|
runtime.put("httpProxy", null);
|
|
runtime.put("connectTimeout", 15000);
|
|
runtime.put("readTimeout", 15000);
|
|
builder = builder
|
|
.connectTimeout(runtime)
|
|
.readTimeout(runtime)
|
|
.connectionPool(runtime)
|
|
.certificate(runtime)
|
|
.proxy(runtime)
|
|
.proxyAuthenticator(runtime);
|
|
return builder.buildOkHttpClient();
|
|
}
|
|
|
|
private static String getClientKey(String host, int port) {
|
|
return String.format("%s:%d", host, port);
|
|
}
|
|
|
|
|
|
public static String get(String url, MapParams mapParams) {
|
|
OkHttpClient client = getOkHttpClient(url);
|
|
if (client == null) {
|
|
return null;
|
|
}
|
|
Map<String, Object> requestParams = new HashMap<>();
|
|
if (mapParams != null) {
|
|
mapParams.setParams(requestParams);
|
|
}
|
|
try (Response response = client.newCall(new Request.Builder().get()
|
|
.url(mackParamsUrl(url, requestParams)).build()).execute()) {
|
|
if (!response.isSuccessful()) {
|
|
return null;
|
|
}
|
|
|
|
try (ResponseBody body = response.body()) {
|
|
if (body == null) {
|
|
return null;
|
|
}
|
|
return body.string();
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
public static void getAsync(String url, MapParams mapParams) {
|
|
OkHttpClient client = getOkHttpClient(url);
|
|
if (client == null) {
|
|
return;
|
|
}
|
|
Map<String, Object> requestParams = new HashMap<>();
|
|
if (mapParams != null) {
|
|
mapParams.setParams(requestParams);
|
|
}
|
|
|
|
client.newCall(new Request.Builder().get().url(mackParamsUrl(url, requestParams)).build()).enqueue(new Callback() {
|
|
@Override
|
|
public void onFailure(Call call, IOException e) {
|
|
log.error(e.getMessage());
|
|
}
|
|
|
|
@Override
|
|
public void onResponse(Call call, Response response) throws IOException {
|
|
try (ResponseBody responseBody = response.body()) {
|
|
if (!response.isSuccessful()) {
|
|
if (responseBody != null) {
|
|
log.error(responseBody.string());
|
|
}
|
|
} else {
|
|
if (responseBody != null) {
|
|
log.info(responseBody.string());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
public static String post(String url, RequestBody requestBody, Header header) {
|
|
OkHttpClient client = getOkHttpClient(url);
|
|
if (client == null) {
|
|
return null;
|
|
}
|
|
Headers.Builder headersBuilder = new Headers.Builder();
|
|
if (header != null) {
|
|
header.setHeaders(headersBuilder);
|
|
}
|
|
try (Response response = client.newCall(new Request.Builder().url(url).post(requestBody).headers(headersBuilder.build()).build()).execute()) {
|
|
if (!response.isSuccessful()) {
|
|
return null;
|
|
}
|
|
try (ResponseBody body = response.body()) {
|
|
if (body == null) {
|
|
return null;
|
|
}
|
|
String respBody = body.string();
|
|
log.info("接口响应数据resp={}", respBody);
|
|
return respBody;
|
|
}
|
|
} catch (Exception e) {
|
|
ErrorHelper.println(e);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static String post(String url, RequestBody requestBody) {
|
|
return post(url, requestBody, null);
|
|
}
|
|
|
|
public static String postJsonStr(String url, OkHttpHelper.MapParams params, Header header) {
|
|
return post(url, OkHttpHelper.createJson(params), header);
|
|
}
|
|
|
|
public static String postFormStr(String url, OkHttpHelper.MapParams params, Header header) {
|
|
return post(url, OkHttpHelper.createForm(params), header);
|
|
}
|
|
|
|
public static String postXmlStr(String url, OkHttpHelper.MapParams params, Header header) {
|
|
return post(url, OkHttpHelper.createXml(params), header);
|
|
}
|
|
|
|
public static JSONObject postJson(String url, OkHttpHelper.MapParams params, Header header) {
|
|
return JsonHelper.parseObject(post(url, OkHttpHelper.createJson(params), header));
|
|
}
|
|
|
|
public static JSONObject postForm(String url, OkHttpHelper.MapParams params, Header header) {
|
|
return JsonHelper.parseObject(post(url, OkHttpHelper.createForm(params), header));
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------------------------------------------
|
|
public static JsonResult post(String url, RequestBody requestBody, Header header, JsonResultEnum jsonResultEnum) {
|
|
return JsonResult.jsonToBean(post(url, requestBody, header), jsonResultEnum);
|
|
}
|
|
|
|
public static JsonResult postJson(String url, OkHttpHelper.MapParams params, Header header, JsonResultEnum jsonResultEnum) {
|
|
return JsonResult.jsonToBean(post(url, OkHttpHelper.createJson(params), header), jsonResultEnum);
|
|
}
|
|
|
|
public static JsonResult postForm(String url, OkHttpHelper.MapParams params, Header header, JsonResultEnum jsonResultEnum) {
|
|
return JsonResult.jsonToBean(post(url, OkHttpHelper.createForm(params), header), jsonResultEnum);
|
|
}
|
|
|
|
public static JsonResult postXml(String url, OkHttpHelper.MapParams params, Header header, JsonResultEnum jsonResultEnum) {
|
|
return JsonResult.xmlToBean(post(url, OkHttpHelper.createXml(params), header), jsonResultEnum);
|
|
}
|
|
|
|
public static RequestBody createForm(OkHttpHelper.MapParams method) {
|
|
Map<String, Object> params = new HashMap<>();
|
|
if (method != null) {
|
|
method.setParams(params);
|
|
}
|
|
FormBody.Builder builder = new FormBody.Builder();
|
|
if (params.size() > 0) {
|
|
Object value;
|
|
for (Map.Entry<String, Object> item : params.entrySet()) {
|
|
value = item.getValue();
|
|
if (value != null) {
|
|
builder.add(item.getKey(), String.valueOf(value));
|
|
}
|
|
}
|
|
}
|
|
return builder.build();
|
|
}
|
|
|
|
public static RequestBody createJson(OkHttpHelper.MapParams jsonMethod) {
|
|
Map<String, Object> params = new HashMap<>();
|
|
if (jsonMethod != null) {
|
|
jsonMethod.setParams(params);
|
|
}
|
|
return RequestBody.create(TYPE_JSON, JsonHelper.toJsonString(params));
|
|
}
|
|
|
|
public static RequestBody createXml(MapParams mapParams) {
|
|
Map<String, Object> requestMap = new HashMap<>();
|
|
if (mapParams != null) {
|
|
mapParams.setParams(requestMap);
|
|
}
|
|
// log.info("请求数据 reqXml={}", XmlHelper.mapToXml(requestMap));
|
|
return RequestBody.create(TYPE_JSON, XmlHelper.mapToXml(requestMap));
|
|
}
|
|
|
|
public static void test() {
|
|
getAsync("http://10.20.10.60:8123/MessagePlatform/AppointmentSuccess", params -> {
|
|
params.put("openId", DesEncryptHelper.enCode("oeso-t62kkoRwLVVkSkwmmjPfUXk"));
|
|
params.put("patientName", "123");
|
|
params.put("deptName", "测试");
|
|
params.put("doctor", "测试"); // 医生姓名
|
|
params.put("sex", "男"); //
|
|
params.put("seq", "0"); // HIS交易流水号
|
|
});
|
|
|
|
// Map<String, Object> runtime = new HashMap<>();
|
|
// runtime.put("ignoreSSL", true);
|
|
// runtime.put("httpProxy", null);
|
|
// runtime.put("connectTimeout", 15000);
|
|
// runtime.put("readTimeout", 15000);
|
|
}
|
|
|
|
//
|
|
// public static void main(String[] args) {
|
|
// test();
|
|
// System.out.println("333333333333333333333");
|
|
// getAsync("http://wx.hhzyy.com/api/ip.do", params -> {
|
|
// });
|
|
//
|
|
// System.out.println(clients);
|
|
//
|
|
//// Map<String, Object> runtime = new HashMap<>();
|
|
//// runtime.put("ignoreSSL", true);
|
|
//// runtime.put("httpProxy", null);
|
|
//// runtime.put("connectTimeout", 15000);
|
|
//// runtime.put("readTimeout", 15000);
|
|
// }
|
|
|
|
/**
|
|
* 生成携带参数的链接
|
|
*
|
|
* @param url 请求地址
|
|
* @param params 参数
|
|
* @return 携带参数的链接 / null
|
|
*/
|
|
public static String mackParamsUrl(String url, Map<String, Object> params) {
|
|
if (url == null || "".equals(url)) {
|
|
return null;
|
|
}
|
|
|
|
if (params == null || params.size() == 0) {
|
|
return url;
|
|
}
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.append(url);
|
|
int index = url.indexOf("?");
|
|
if (index == -1) {
|
|
sb.append("?");
|
|
} else {
|
|
sb.append("&");
|
|
}
|
|
|
|
boolean isFirst = false;
|
|
Object value;
|
|
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
|
if (isFirst) {
|
|
sb.append("&");
|
|
}
|
|
|
|
value = entry.getValue();
|
|
if (value != null) {
|
|
sb.append(entry.getKey()).append("=");
|
|
sb.append(value);
|
|
}
|
|
isFirst = true;
|
|
}
|
|
return sb.toString();
|
|
}
|
|
|
|
|
|
}
|
|
|