|
@ -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();//设置请求和传输超时时间
|
|
|
} |
|
|
} |
|
|