|
|
|
@ -6,10 +6,12 @@ import org.apache.http.HttpStatus; |
|
|
|
import org.apache.http.NameValuePair; |
|
|
|
import org.apache.http.ParseException; |
|
|
|
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.methods.CloseableHttpResponse; |
|
|
|
import org.apache.http.client.methods.HttpGet; |
|
|
|
import org.apache.http.client.methods.HttpPost; |
|
|
|
import org.apache.http.client.utils.URIBuilder; |
|
|
|
import org.apache.http.client.utils.URLEncodedUtils; |
|
|
|
import org.apache.http.entity.StringEntity; |
|
|
|
import org.apache.http.impl.client.CloseableHttpClient; |
|
|
|
@ -17,6 +19,8 @@ import org.apache.http.message.BasicNameValuePair; |
|
|
|
import org.apache.http.util.EntityUtils; |
|
|
|
import org.apache.logging.log4j.LogManager; |
|
|
|
import org.apache.logging.log4j.Logger; |
|
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.net.URI; |
|
|
|
@ -38,6 +42,15 @@ public class HttpClientUtils { |
|
|
|
|
|
|
|
private CloseableHttpClient client; |
|
|
|
|
|
|
|
private static int connectionTimeout = 5000; |
|
|
|
// 读取数据超时时间,毫秒
|
|
|
|
private static int soTimeout = 45000; |
|
|
|
private static String HEADER_CONTENT_TYPE = "Content-Type"; |
|
|
|
private static String HEADER_APPLICATION_JSON = "application/json;charset=utf-8"; |
|
|
|
private static String UTF8 = "utf-8"; |
|
|
|
private static String HEADER_APPLICATION_FORM_URL_ENCODED = "application/x-www-form-urlencoded;charset=utf-8"; |
|
|
|
|
|
|
|
|
|
|
|
public HttpClientUtils(HttpClientManagerFactoryBen httpClientBean) { |
|
|
|
try { |
|
|
|
this.client = httpClientBean.getObject(); |
|
|
|
@ -169,4 +182,59 @@ public class HttpClientUtils { |
|
|
|
} |
|
|
|
return formparams; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* desc: 发送get请求 |
|
|
|
* param:url, params |
|
|
|
* return: CallResult<String> |
|
|
|
* date: 2019/2/21 9:16 |
|
|
|
* |
|
|
|
* @author: jianjun liu |
|
|
|
*/ |
|
|
|
public ResultDto sendGet(String url, Map<String, Object> params) { |
|
|
|
|
|
|
|
try { |
|
|
|
URIBuilder builder = new URIBuilder(url); |
|
|
|
if (!CollectionUtils.isEmpty(params)) { |
|
|
|
Set<String> set = params.keySet(); |
|
|
|
for (String key : set) { |
|
|
|
builder.setParameter(key, params.get(key) == null ? "" : String.valueOf(params.get(key))); |
|
|
|
} |
|
|
|
} |
|
|
|
HttpGet httpGet = new HttpGet(builder.build()); |
|
|
|
httpGet.setConfig(requestConfig); |
|
|
|
return getResult(client.execute(httpGet)); |
|
|
|
} catch (Exception e) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public ResultDto sendPostByJSONAndHeader(String url, String jsonStrParam,Map<String,String> headerMap) { |
|
|
|
|
|
|
|
try { |
|
|
|
HttpPost httppost = new HttpPost(url); |
|
|
|
httppost.setConfig(requestConfig); |
|
|
|
httppost.addHeader(HEADER_CONTENT_TYPE, HEADER_APPLICATION_JSON); |
|
|
|
if (null != headerMap){ |
|
|
|
headerMap.forEach((k,v) -> { |
|
|
|
httppost.addHeader(k,v); |
|
|
|
}); |
|
|
|
} |
|
|
|
if (!StringUtils.isEmpty(jsonStrParam)) { |
|
|
|
StringEntity se = new StringEntity(jsonStrParam, UTF8); |
|
|
|
httppost.setEntity(se); |
|
|
|
} |
|
|
|
return getResult(client.execute(httppost)); |
|
|
|
} catch (Exception e) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/*** 超时设置 ****/ |
|
|
|
private static RequestConfig requestConfig = RequestConfig.custom() |
|
|
|
.setSocketTimeout(soTimeout) |
|
|
|
.setConnectTimeout(connectionTimeout) |
|
|
|
.setConnectionRequestTimeout(soTimeout) |
|
|
|
.build();//设置请求和传输超时时间
|
|
|
|
} |
|
|
|
|