Browse Source

推送方法追加

master
zhangyuan 3 years ago
parent
commit
2d5e6b72fe
  1. 116
      spring-boot-httpclient-starter/src/main/java/com/elink/esua/httpclient/HttpClientUtils.java
  2. 27
      spring-boot-httpclient-starter/src/main/java/com/elink/esua/httpclient/HttpDeleteWithBody.java

116
spring-boot-httpclient-starter/src/main/java/com/elink/esua/httpclient/HttpClientUtils.java

@ -6,10 +6,12 @@ import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair; import org.apache.http.NameValuePair;
import org.apache.http.ParseException; import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
@ -17,6 +19,7 @@ import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.springframework.util.StringUtils;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
@ -38,6 +41,17 @@ public class HttpClientUtils {
private CloseableHttpClient client; private CloseableHttpClient client;
private static final int CONNECTION_TIMEOUT = 5000;
/**
* 读取数据超时时间毫秒
*/
private static final int SO_TIMEOUT = 45000;
private static final String HEADER_CONTENT_TYPE = "Content-Type";
private static final String HEADER_APPLICATION_JSON = "application/json;charset=utf-8";
private static final String UTF8 = "utf-8";
private static final String HEADER_APPLICATION_FORM_URL_ENCODED = "application/x-www-form-urlencoded;charset=utf-8";
public HttpClientUtils(HttpClientManagerFactoryBen httpClientBean) { public HttpClientUtils(HttpClientManagerFactoryBen httpClientBean) {
try { try {
this.client = httpClientBean.getObject(); this.client = httpClientBean.getObject();
@ -127,6 +141,96 @@ public class HttpClientUtils {
return getResult(client.execute(httpPost)); return getResult(client.execute(httpPost));
} }
/**
* post json
*
* @param url
* @param jsonStrParam
* @param headerMap
* @return com.elink.esua.httpclient.ResultDto
* @author zhy
* @date 2022/9/6 9:41
*/
public ResultDto sendPostByJSONAndHeader(String url, String jsonStrParam, Map<String, String> headerMap) {
try {
HttpPost httppost = new HttpPost(url);
httppost.setConfig(REQUEST_CONFIG);
httppost.addHeader(HEADER_CONTENT_TYPE, HEADER_APPLICATION_JSON);
if (null != headerMap) {
headerMap.forEach(httppost::addHeader);
}
if (!StringUtils.isEmpty(jsonStrParam)) {
StringEntity se = new StringEntity(jsonStrParam, UTF8);
httppost.setEntity(se);
}
return getResult(client.execute(httppost));
} catch (Exception e) {
return null;
}
}
/**
* put json
*
* @param url
* @param jsonStrParam
* @param headerMap
* @return com.elink.esua.httpclient.ResultDto
* @author zhy
* @date 2022/9/6 9:41
*/
public ResultDto sendPutByJSONAndHeader(String url, String jsonStrParam, Map<String, String> headerMap) {
try {
HttpPut httpPut = new HttpPut(url);
httpPut.setConfig(REQUEST_CONFIG);
httpPut.addHeader(HEADER_CONTENT_TYPE, HEADER_APPLICATION_JSON);
if (null != headerMap) {
headerMap.forEach(httpPut::addHeader);
}
if (!StringUtils.isEmpty(jsonStrParam)) {
StringEntity se = new StringEntity(jsonStrParam, UTF8);
httpPut.setEntity(se);
}
return getResult(client.execute(httpPut));
} catch (Exception e) {
return null;
}
}
/**
* delete json
*
* @param url
* @param jsonStrParam
* @param headerMap
* @return com.elink.esua.httpclient.ResultDto
* @author zhy
* @date 2022/9/6 9:41
*/
public ResultDto sendDeleteByJSONAndHeader(String url, String jsonStrParam, Map<String, String> headerMap) {
try {
HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(url);
httpDelete.setConfig(REQUEST_CONFIG);
httpDelete.addHeader(HEADER_CONTENT_TYPE, HEADER_APPLICATION_JSON);
if (null != headerMap) {
headerMap.forEach(httpDelete::addHeader);
}
if (!StringUtils.isEmpty(jsonStrParam)) {
StringEntity se = new StringEntity(jsonStrParam, UTF8);
httpDelete.setEntity(se);
}
return getResult(client.execute(httpDelete));
} catch (Exception e) {
return null;
}
}
/** /**
* get 提交方式 * get 提交方式
* *
@ -153,6 +257,11 @@ public class HttpClientUtils {
return getResult(client.execute(httpGet)); return getResult(client.execute(httpGet));
} }
public String getParamStr(Map<String, String> paramMap) {
List<NameValuePair> formparams = setHttpParams(paramMap);
return URLEncodedUtils.format(formparams, "UTF-8");
}
/** /**
* 设置请求参数 * 设置请求参数
* *
@ -169,4 +278,11 @@ public class HttpClientUtils {
} }
return formparams; return formparams;
} }
/*** 超时设置 ****/
private static final RequestConfig REQUEST_CONFIG = RequestConfig.custom()
.setSocketTimeout(SO_TIMEOUT)
.setConnectTimeout(CONNECTION_TIMEOUT)
.setConnectionRequestTimeout(SO_TIMEOUT)
.build();//设置请求和传输超时时间
} }

27
spring-boot-httpclient-starter/src/main/java/com/elink/esua/httpclient/HttpDeleteWithBody.java

@ -0,0 +1,27 @@
package com.elink.esua.httpclient;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import java.net.URI;
/**
* 带body的delete方法
*
* @author zhy
* @date 2022/12/7 17:50
*/
public class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
public static final String METHOD_NAME = "DELETE";
public String getMethod() { return METHOD_NAME; }
public HttpDeleteWithBody(final String uri) {
super();
setURI(URI.create(uri));
}
public HttpDeleteWithBody(final URI uri) {
super();
setURI(uri);
}
public HttpDeleteWithBody() { super(); }
}
Loading…
Cancel
Save