|
@ -13,11 +13,16 @@ import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.json.JSONObject;
|
|
import org.json.JSONObject;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.MediaType;
|
|
|
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
|
|
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.client.RestTemplate;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
@ -27,6 +32,16 @@ import java.util.Map;
|
|
|
|
|
|
@Component
|
|
@Component
|
|
public class HttpClientUtil {
|
|
public class HttpClientUtil {
|
|
|
|
private Logger logger= LoggerFactory.getLogger(HttpClientUtil.class);
|
|
|
|
private HttpComponentsClientHttpRequestFactory requestFactory;
|
|
|
|
@PostConstruct
|
|
|
|
private void init() {
|
|
|
|
requestFactory = new HttpComponentsClientHttpRequestFactory ();
|
|
|
|
requestFactory.setConnectionRequestTimeout(60*1000);
|
|
|
|
requestFactory.setConnectTimeout(60*1000);
|
|
|
|
requestFactory.setReadTimeout(60*1000);
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 发送post请求
|
|
* 发送post请求
|
|
*
|
|
*
|
|
@ -35,7 +50,7 @@ public class HttpClientUtil {
|
|
* @param chatSet 编码格式
|
|
* @param chatSet 编码格式
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public String post(String url, List<NameValuePair> params, String chatSet) {
|
|
|
|
|
|
public String post(String url, List<NameValuePair> params, String chatSet) {
|
|
// 创建默认的httpClient实例.
|
|
// 创建默认的httpClient实例.
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
// 创建httppost
|
|
// 创建httppost
|
|
@ -78,7 +93,7 @@ public class HttpClientUtil {
|
|
* @param chatSet 编码格式
|
|
* @param chatSet 编码格式
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public String get(String url, String chatSet) {
|
|
|
|
|
|
public String get(String url, String chatSet) {
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
try {
|
|
try {
|
|
// 创建httpget.
|
|
// 创建httpget.
|
|
@ -119,7 +134,7 @@ public class HttpClientUtil {
|
|
* @return
|
|
* @return
|
|
* @throws Exception
|
|
* @throws Exception
|
|
*/
|
|
*/
|
|
public String httpPost(String url, Map<String, String> params) throws Exception {
|
|
|
|
|
|
public String httpPost(String url, Map<String, String> params) throws Exception {
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
try {
|
|
try {
|
|
HttpPost httpPost = new HttpPost(url);
|
|
HttpPost httpPost = new HttpPost(url);
|
|
@ -154,7 +169,7 @@ public class HttpClientUtil {
|
|
* @param params
|
|
* @param params
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public Map<String, String> getSecretParams(Map<String, String> params, String appId, String secret) {
|
|
|
|
|
|
public Map<String, String> getSecretParams(Map<String, String> params, String appId, String secret) {
|
|
String timestamp = Long.toString(System.currentTimeMillis());
|
|
String timestamp = Long.toString(System.currentTimeMillis());
|
|
params.put("timestamp", timestamp);
|
|
params.put("timestamp", timestamp);
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
@ -177,10 +192,12 @@ public class HttpClientUtil {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public String postBody(String url, JSONObject params) {
|
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
|
public String postBody(String url, JSONObject params) {
|
|
|
|
logger.info("url:"+url);
|
|
|
|
logger.info("params:"+params);
|
|
|
|
RestTemplate restTemplate = new RestTemplate(requestFactory);
|
|
HttpHeaders headers = new HttpHeaders();
|
|
HttpHeaders headers = new HttpHeaders();
|
|
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
|
|
|
MediaType type = MediaType.parseMediaType("application/json");
|
|
headers.setContentType(type);
|
|
headers.setContentType(type);
|
|
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
|
|
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
|
|
org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
|
|
org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
|
|
@ -188,10 +205,13 @@ public class HttpClientUtil {
|
|
return ret;
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
|
|
public void putBody(String url, JSONObject params) {
|
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
|
public void putBody(String url, JSONObject params) {
|
|
|
|
logger.info("url:"+url);
|
|
|
|
logger.info("params:"+params);
|
|
|
|
|
|
|
|
RestTemplate restTemplate = new RestTemplate(requestFactory);
|
|
HttpHeaders headers = new HttpHeaders();
|
|
HttpHeaders headers = new HttpHeaders();
|
|
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
|
|
|
MediaType type = MediaType.parseMediaType("application/json");
|
|
headers.setContentType(type);
|
|
headers.setContentType(type);
|
|
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
|
|
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
|
|
org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
|
|
org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
|
|
@ -202,17 +222,20 @@ public class HttpClientUtil {
|
|
/**
|
|
/**
|
|
* 发送post请求
|
|
* 发送post请求
|
|
*
|
|
*
|
|
* @param url 请求地址
|
|
|
|
* @param params 请求参数
|
|
|
|
|
|
* @param url 请求地址
|
|
|
|
* @param params 请求参数
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public String iotPostBody(String url, String params) {
|
|
public String iotPostBody(String url, String params) {
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
|
logger.info("url:"+url);
|
|
|
|
logger.info("params:"+params);
|
|
|
|
RestTemplate restTemplate = new RestTemplate(requestFactory);
|
|
HttpHeaders headers = new HttpHeaders();
|
|
HttpHeaders headers = new HttpHeaders();
|
|
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
|
|
|
MediaType type = MediaType.parseMediaType("application/json");
|
|
headers.setContentType(type);
|
|
headers.setContentType(type);
|
|
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
|
|
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
|
|
org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params, headers);
|
|
org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params, headers);
|
|
|
|
|
|
String ret = restTemplate.postForObject(url, formEntity, String.class);
|
|
String ret = restTemplate.postForObject(url, formEntity, String.class);
|
|
return ret;
|
|
return ret;
|
|
}
|
|
}
|