Commit 4285bdaa authored by zhangc's avatar zhangc

优化httpclients请求工具类

parent 5600389e
package com.jz.dm.common.util; package com.jz.dm.common.enums;
/** /**
* 签名类型 * 签名类型
......
package com.jz.dm.common.util;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.CharacterIterator;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.StringCharacterIterator;
import java.util.*;
public class JSONWriter {
/**
* 默认时间格式
**/
public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
/**
* Date默认时区
**/
public static final String DATE_TIMEZONE = "GMT+8";
private StringBuffer buf = new StringBuffer();
private Stack<Object> calls = new Stack<Object>();
private boolean emitClassName = true;
private DateFormat format;
public JSONWriter(boolean emitClassName) {
this.emitClassName = emitClassName;
}
public JSONWriter() {
this(false);
}
public JSONWriter(DateFormat format) {
this(false);
this.format = format;
}
public String write(Object object) {
buf.setLength(0);
value(object);
return buf.toString();
}
public String write(long n) {
return String.valueOf(n);
}
public String write(double d) {
return String.valueOf(d);
}
public String write(char c) {
return "\"" + c + "\"";
}
public String write(boolean b) {
return String.valueOf(b);
}
private void value(Object object) {
if (object == null || cyclic(object)) {
add(null);
} else {
calls.push(object);
if (object instanceof Class<?>) string(object);
else if (object instanceof Boolean) bool(((Boolean) object).booleanValue());
else if (object instanceof Number) add(object);
else if (object instanceof String) string(object);
else if (object instanceof Character) string(object);
else if (object instanceof Map<?, ?>) map((Map<?, ?>)object);
else if (object.getClass().isArray()) array(object);
else if (object instanceof Iterator<?>) array((Iterator<?>)object);
else if (object instanceof Collection<?>) array(((Collection<?>)object).iterator());
else if (object instanceof Date) date((Date)object);
else bean(object);
calls.pop();
}
}
private boolean cyclic(Object object) {
Iterator<Object> it = calls.iterator();
while (it.hasNext()) {
Object called = it.next();
if (object == called) return true;
}
return false;
}
private void bean(Object object) {
add("{");
BeanInfo info;
boolean addedSomething = false;
try {
info = Introspector.getBeanInfo(object.getClass());
PropertyDescriptor[] props = info.getPropertyDescriptors();
for (int i = 0; i < props.length; ++i) {
PropertyDescriptor prop = props[i];
String name = prop.getName();
Method accessor = prop.getReadMethod();
if ((emitClassName || !"class".equals(name)) && accessor != null) {
if (!accessor.isAccessible()) accessor.setAccessible(true);
Object value = accessor.invoke(object, (Object[])null);
if (value == null) continue;
if (addedSomething) add(',');
add(name, value);
addedSomething = true;
}
}
} catch (IllegalAccessException iae) {
} catch (InvocationTargetException ite) {
} catch (IntrospectionException ie) {
}
add("}");
}
private void add(String name, Object value) {
add('"');
add(name);
add("\":");
value(value);
}
private void map(Map<?, ?> map) {
add("{");
Iterator<?> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<?, ?> e = (Map.Entry<?, ?>) it.next();
value(e.getKey());
add(":");
value(e.getValue());
if (it.hasNext()) add(',');
}
add("}");
}
private void array(Iterator<?> it) {
add("[");
while (it.hasNext()) {
value(it.next());
if (it.hasNext()) add(",");
}
add("]");
}
private void array(Object object) {
add("[");
int length = Array.getLength(object);
for (int i = 0; i < length; ++i) {
value(Array.get(object, i));
if (i < length - 1) add(',');
}
add("]");
}
private void bool(boolean b) {
add(b ? "true" : "false");
}
private void date(Date date) {
if (this.format == null) {
this.format = new SimpleDateFormat(DATE_TIME_FORMAT);
this.format.setTimeZone(TimeZone.getTimeZone(DATE_TIMEZONE));
}
add("\"");
add(format.format(date));
add("\"");
}
private void string(Object obj) {
add('"');
CharacterIterator it = new StringCharacterIterator(obj.toString());
for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
if (c == '"') add("\\\"");
else if (c == '\\') add("\\\\");
else if (c == '/') add("\\/");
else if (c == '\b') add("\\b");
else if (c == '\f') add("\\f");
else if (c == '\n') add("\\n");
else if (c == '\r') add("\\r");
else if (c == '\t') add("\\t");
else if (Character.isISOControl(c)) {
unicode(c);
} else {
add(c);
}
}
add('"');
}
private void add(Object obj) {
buf.append(obj);
}
private void add(char c) {
buf.append(c);
}
static char[] hex = "0123456789ABCDEF".toCharArray();
private void unicode(char c) {
add("\\u");
int n = c;
for (int i = 0; i < 4; ++i) {
int digit = (n & 0xf000) >> 12;
add(hex[digit]);
n <<= 4;
}
}
}
package com.jz.dm.common.util;
import java.io.*;
/**
* 流处理工具
*
*/
public class StreamUtil {
private static final int DEFAULT_BUFFER_SIZE = 8192;
public static void io(InputStream in, OutputStream out) throws IOException {
io(in, out, -1);
}
public static void io(InputStream in, OutputStream out, int bufferSize) throws IOException {
if (bufferSize == -1) {
bufferSize = DEFAULT_BUFFER_SIZE;
}
byte[] buffer = new byte[bufferSize];
int amount;
while ((amount = in.read(buffer)) >= 0) {
out.write(buffer, 0, amount);
}
}
public static void io(Reader in, Writer out) throws IOException {
io(in, out, -1);
}
public static void io(Reader in, Writer out, int bufferSize) throws IOException {
if (bufferSize == -1) {
bufferSize = DEFAULT_BUFFER_SIZE >> 1;
}
char[] buffer = new char[bufferSize];
int amount;
while ((amount = in.read(buffer)) >= 0) {
out.write(buffer, 0, amount);
}
}
public static OutputStream synchronizedOutputStream(OutputStream out) {
return new SynchronizedOutputStream(out);
}
public static OutputStream synchronizedOutputStream(OutputStream out, Object lock) {
return new SynchronizedOutputStream(out, lock);
}
public static String readText(InputStream in) throws IOException {
return readText(in, null, -1);
}
public static String readText(InputStream in, String encoding) throws IOException {
return readText(in, encoding, -1);
}
public static String readText(InputStream in, String encoding,
int bufferSize) throws IOException {
Reader reader = (encoding == null) ? new InputStreamReader(in)
: new InputStreamReader(in, encoding);
return readText(reader, bufferSize);
}
public static String readText(Reader reader) throws IOException {
return readText(reader, -1);
}
public static String readText(Reader reader, int bufferSize) throws IOException {
StringWriter writer = new StringWriter();
io(reader, writer, bufferSize);
return writer.toString();
}
private static class SynchronizedOutputStream extends OutputStream {
private OutputStream out;
private Object lock;
SynchronizedOutputStream(OutputStream out) {
this(out, out);
}
SynchronizedOutputStream(OutputStream out, Object lock) {
this.out = out;
this.lock = lock;
}
public void write(int datum) throws IOException {
synchronized (lock) {
out.write(datum);
}
}
public void write(byte[] data) throws IOException {
synchronized (lock) {
out.write(data);
}
}
public void write(byte[] data, int offset, int length) throws IOException {
synchronized (lock) {
out.write(data, offset, length);
}
}
public void flush() throws IOException {
synchronized (lock) {
out.flush();
}
}
public void close() throws IOException {
synchronized (lock) {
out.close();
}
}
}
}
...@@ -4,7 +4,7 @@ import com.jz.dm.common.constant.Constants; ...@@ -4,7 +4,7 @@ import com.jz.dm.common.constant.Constants;
import com.jz.dm.common.enums.Format; import com.jz.dm.common.enums.Format;
import com.jz.dm.common.enums.GatewayResultCode; import com.jz.dm.common.enums.GatewayResultCode;
import com.jz.dm.common.exception.GatewayException; import com.jz.dm.common.exception.GatewayException;
import com.jz.dm.common.util.SignType; import com.jz.dm.common.enums.SignType;
import com.jz.dm.common.util.StringUtil; import com.jz.dm.common.util.StringUtil;
import com.jz.dm.models.enity.GatewayRequest; import com.jz.dm.models.enity.GatewayRequest;
import com.jz.dm.models.enity.GatewayResponse; import com.jz.dm.models.enity.GatewayResponse;
......
...@@ -480,7 +480,7 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -480,7 +480,7 @@ public class ProducerServiceImpl implements ProducerService {
param.put("type", type); param.put("type", type);
String resp = null; String resp = null;
try { try {
resp = httpsUtils.submitPost(datasource.getJdbcUrl() + "/es/get/type/fields", param.toString(),herders); resp = httpsUtils.submitPost(datasource.getJdbcUrl() + "/es/get/type/fields", param,herders);
log.info("索引下type的fields信息集合:" + resp); log.info("索引下type的fields信息集合:" + resp);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
......
...@@ -4,7 +4,7 @@ package com.jz.dm.web.aspect; ...@@ -4,7 +4,7 @@ package com.jz.dm.web.aspect;
import com.jz.common.utils.IpUtils; import com.jz.common.utils.IpUtils;
import com.jz.common.utils.JsonUtils; import com.jz.common.utils.JsonUtils;
import com.jz.common.utils.UrlUtil; import com.jz.common.utils.UrlUtil;
import com.jz.dm.common.util.SignType; import com.jz.dm.common.enums.SignType;
import com.jz.dm.mapper.ApiReqLogMapper; import com.jz.dm.mapper.ApiReqLogMapper;
import com.jz.dm.models.domian.ApiReqLog; import com.jz.dm.models.domian.ApiReqLog;
import com.jz.dm.service.ApiLogService; import com.jz.dm.service.ApiLogService;
......
...@@ -74,15 +74,13 @@ spring: ...@@ -74,15 +74,13 @@ spring:
proxy-target-class: true proxy-target-class: true
auto: true auto: true
#rePrefix redis存储的前缀
#ignoreRedis true存入 false不存 api.timeout.default #ignoreRedis true存入 false不存 api.timeout.default
dmp: dmp:
ignoreRedis: true ignoreRedis: true
rePrefix: test
openapi: openapi:
timeout: timeout:
default: 5000 default: 5000
max: 5000 max: 5000
request: request: #api请求次数
limit: limit:
max: 10 max: 10
...@@ -13,8 +13,10 @@ logging: ...@@ -13,8 +13,10 @@ logging:
spring: spring:
application: application:
name: 九章数据平台 name: 九章数据平台
aop: servlet:
proxy-target-class: true multipart:
max-file-size: 10MB
max-request-size: 50MB
profiles: profiles:
active: test #默认使用的配置文件 active: test #默认使用的配置文件
......
package com.jz.dm.gateway.orther;
import com.alibaba.fastjson.JSONObject;
import com.jz.common.utils.HttpsUtils;
import com.jz.dm.gateway.SpringTestCase;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.gateway.orther
* @PROJECT_NAME: jz-dm-parent
* @NAME: TestHttpReq
* @DATE: 2021-1-3/10:15
* @DAY_NAME_SHORT: 周日
* @Description:
**/
public class TestHttpReq extends SpringTestCase {
private static final String baseUrl ="http://localhost:8081/logInfo/getMallLogDetail/1";
private static final String baseUrlPost ="http://localhost:8081/logInfo/getMallLogInfo";
@Autowired
private HttpsUtils httpsUtils;
@Test
public void getReqTest(){
String response = httpsUtils.doGet(baseUrl, new HashMap<>());
System.out.println(response);
}
@Test
public void postReqTest(){
JSONObject jsonObject = new JSONObject();
jsonObject.put("pageNum",1);
jsonObject.put("pageSize",20);
HashMap<String, String> headers = new HashMap<>();
headers.put("connection", "keep-alive");
headers.put("Charsert", "UTF-8");
headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
String response = httpsUtils.submitPost(baseUrlPost, jsonObject,headers);
System.out.println(response);
}
}
...@@ -45,7 +45,7 @@ public class HttpClientPool { ...@@ -45,7 +45,7 @@ public class HttpClientPool {
public static synchronized CloseableHttpClient getHttpClient() { public static synchronized CloseableHttpClient getHttpClient() {
if (httpClient == null) { if (httpClient == null) {
System.out.println("---------------------------------------------------------创建"); log.info("------------------------创建HttpClient---------------------------------");
//注册访问协议相关的Socket工厂 //注册访问协议相关的Socket工厂
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
......
package com.jz.common.utils; package com.jz.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
...@@ -34,27 +35,30 @@ import static com.jz.common.utils.HttpClientPool.getHttpClient; ...@@ -34,27 +35,30 @@ import static com.jz.common.utils.HttpClientPool.getHttpClient;
@Component @Component
public class HttpsUtils { public class HttpsUtils {
public static final String JSON_CONTENT_FORM = "application/json; charset=utf-8";
public static final String CONTENT_FORM = "application/x-www-form-urlencoded;charset=UTF-8";
/** /**
* Post请求表单提交 * Post请求表单提交
*
* @param url 请求路径 * @param url 请求路径
* @param params 请求参数 * @param params 请求参数
* @param headers 请求头 * @param headers 请求头
* @return * @return
*/ */
public String submitPost(String url, String params, Map<String, String> headers) { public String submitPost(String url, JSONObject params, Map<String, String> headers) {
CloseableHttpClient httpClient = getHttpClient(); CloseableHttpClient httpClient = getHttpClient();
String body = null; String body = null;
CloseableHttpResponse response = null; CloseableHttpResponse response = null;
try { try {
HttpPost httpPost = new HttpPost(url); HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-type",JSON_CONTENT_FORM );
if (null != headers && headers.size() > 0) { if (null != headers && headers.size() > 0) {
for (Map.Entry<String, String> e : headers.entrySet()) { for (Map.Entry<String, String> e : headers.entrySet()) {
httpPost.addHeader(e.getKey(), e.getValue()); httpPost.addHeader(e.getKey(), e.getValue());
} }
} }
if (StringUtils.isNotBlank(params)) { if (StringUtils.isNotBlank(params.toString())) {
httpPost.setEntity(new StringEntity(params, Consts.UTF_8)); httpPost.setEntity(new StringEntity(params.toString(), Consts.UTF_8));
} }
response = httpClient.execute(httpPost); response = httpClient.execute(httpPost);
body = getBody(response.getEntity()); body = getBody(response.getEntity());
...@@ -84,6 +88,7 @@ public class HttpsUtils { ...@@ -84,6 +88,7 @@ public class HttpsUtils {
CloseableHttpResponse response = null; CloseableHttpResponse response = null;
try { try {
HttpPost httpPost = new HttpPost(url); HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-type",JSON_CONTENT_FORM );
if (StringUtils.isNotBlank(params)) { if (StringUtils.isNotBlank(params)) {
httpPost.setEntity(new StringEntity(params, Consts.UTF_8)); httpPost.setEntity(new StringEntity(params, Consts.UTF_8));
} }
......
...@@ -24,7 +24,7 @@ public class UrlUtil { ...@@ -24,7 +24,7 @@ public class UrlUtil {
int port = request.getServerPort(); int port = request.getServerPort();
// 访问项目名 // 访问项目名
String contextPath = request.getContextPath(); String contextPath = request.getContextPath();
String url = "%s://%s%s%s"; String url = "%s://%s%s%s%s";
String portStr = ""; String portStr = "";
if (port != 80) { if (port != 80) {
portStr += ":" + port; portStr += ":" + port;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment