|
@ -0,0 +1,694 @@
|
|
|
package com.yihu.jw.care.util;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yihu.jw.entity.base.dict.BaseExceptionDictDO;
|
|
|
import com.yihu.jw.entity.base.dict.BaseExceptionLogDO;
|
|
|
import com.yihu.jw.entity.base.dict.BaseExceptionServerDictDO;
|
|
|
import com.yihu.jw.util.dao.BaseExceptionDictUtilDao;
|
|
|
import com.yihu.jw.util.dao.BaseExceptionLogUtilDao;
|
|
|
import com.yihu.jw.util.dao.BaseExceptionServerDictUtilDao;
|
|
|
import org.apache.http.Consts;
|
|
|
import org.apache.http.HttpEntity;
|
|
|
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.*;
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.transaction.annotation.Propagation;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
import java.net.URLConnection;
|
|
|
import java.util.*;
|
|
|
|
|
|
@Component
|
|
|
public class HttpClientUtilCare {
|
|
|
|
|
|
/**
|
|
|
* 发送post请求
|
|
|
*
|
|
|
* @param url 请求地址
|
|
|
* @param params 请求参数
|
|
|
* @param chatSet 编码格式
|
|
|
* @return
|
|
|
*/
|
|
|
public String post(String url, List<NameValuePair> params, String chatSet) {
|
|
|
// 创建默认的httpClient实例.
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
// 创建httppost
|
|
|
HttpPost httppost = new HttpPost(url);
|
|
|
UrlEncodedFormEntity uefEntity;
|
|
|
try {
|
|
|
uefEntity = new UrlEncodedFormEntity(params, chatSet);
|
|
|
httppost.setEntity(uefEntity);
|
|
|
CloseableHttpResponse response = httpclient.execute(httppost);
|
|
|
try {
|
|
|
HttpEntity entity = response.getEntity();
|
|
|
if (entity != null) {
|
|
|
return EntityUtils.toString(entity, chatSet);
|
|
|
}
|
|
|
} finally {
|
|
|
response.close();
|
|
|
}
|
|
|
} catch (ClientProtocolException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (UnsupportedEncodingException e1) {
|
|
|
e1.printStackTrace();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
// 关闭连接,释放资源
|
|
|
try {
|
|
|
httpclient.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 发送get请求
|
|
|
*
|
|
|
* @param url 请求地址
|
|
|
* @param chatSet 编码格式
|
|
|
* @return
|
|
|
*/
|
|
|
public String get(String url, String chatSet) {
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
try {
|
|
|
// 创建httpget.
|
|
|
HttpGet httpget = new HttpGet(url);
|
|
|
// 执行get请求.
|
|
|
CloseableHttpResponse response = httpclient.execute(httpget);
|
|
|
try {
|
|
|
// 获取响应实体
|
|
|
HttpEntity entity = response.getEntity();
|
|
|
if (entity != null) {
|
|
|
return EntityUtils.toString(entity, chatSet);
|
|
|
}
|
|
|
} finally {
|
|
|
response.close();
|
|
|
}
|
|
|
} catch (ClientProtocolException e) {
|
|
|
System.out.println(e.getClass()+"1");
|
|
|
e.printStackTrace();
|
|
|
} catch (ParseException e) {
|
|
|
System.out.println(e.getClass()+"2");
|
|
|
e.printStackTrace();
|
|
|
} catch (IOException e) {
|
|
|
System.out.println(e.getClass()+"3");
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
// 关闭连接,释放资源
|
|
|
try {
|
|
|
httpclient.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
public String get(String url,List<NameValuePair> params, String chatSet) {
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
try {
|
|
|
// 创建httpget.
|
|
|
String entityString = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));
|
|
|
HttpGet httpget = new HttpGet(url+"?"+entityString);
|
|
|
// 执行get请求.
|
|
|
CloseableHttpResponse response = httpclient.execute(httpget);
|
|
|
try {
|
|
|
// 获取响应实体
|
|
|
HttpEntity entity = response.getEntity();
|
|
|
if (entity != null) {
|
|
|
return EntityUtils.toString(entity, chatSet);
|
|
|
}
|
|
|
} finally {
|
|
|
response.close();
|
|
|
}
|
|
|
} catch (ClientProtocolException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (ParseException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
// 关闭连接,释放资源
|
|
|
try {
|
|
|
httpclient.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* http调用方法,(健康之路开放平台)
|
|
|
*
|
|
|
* @param url
|
|
|
* @param params
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public static String httpPost(String url, Map<String, String> params) throws Exception {
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
try {
|
|
|
HttpPost httpPost = new HttpPost(url);
|
|
|
if (params != null && params.size() > 0) {
|
|
|
List<NameValuePair> valuePairs = new ArrayList<NameValuePair>(params.size());
|
|
|
for (Map.Entry<String, String> entry : params.entrySet()) {
|
|
|
NameValuePair nameValuePair = new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue()));
|
|
|
valuePairs.add(nameValuePair);
|
|
|
}
|
|
|
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(valuePairs, "UTF-8");
|
|
|
httpPost.setEntity(formEntity);
|
|
|
}
|
|
|
CloseableHttpResponse resp = httpclient.execute(httpPost);
|
|
|
try {
|
|
|
HttpEntity entity = resp.getEntity();
|
|
|
String respContent = EntityUtils.toString(entity, "UTF-8").trim();
|
|
|
return respContent;
|
|
|
} finally {
|
|
|
resp.close();
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return null;
|
|
|
} finally {
|
|
|
httpclient.close();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取加密后参数集合(健康之路开放平台)
|
|
|
*
|
|
|
* @param params
|
|
|
* @return
|
|
|
*/
|
|
|
public Map<String, String> getSecretParams(Map<String, String> params, String appId, String secret) {
|
|
|
String timestamp = Long.toString(System.currentTimeMillis());
|
|
|
params.put("timestamp", timestamp);
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
|
|
// 对参数名进行字典排序
|
|
|
String[] keyArray = params.keySet().toArray(new String[0]);
|
|
|
Arrays.sort(keyArray);
|
|
|
// 拼接有序的参数名-值串
|
|
|
stringBuilder.append(appId);
|
|
|
for (String key : keyArray) {
|
|
|
stringBuilder.append(key).append(params.get(key));
|
|
|
}
|
|
|
String codes = stringBuilder.append(secret).toString();
|
|
|
String sign = org.apache.commons.codec.digest.DigestUtils.shaHex(codes).toUpperCase();
|
|
|
// 添加签名,并发送请求
|
|
|
params.put("appId", appId);
|
|
|
params.put("sign", sign);
|
|
|
|
|
|
return params;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 向指定 URL 发送POST方法的请求
|
|
|
*
|
|
|
* @param url
|
|
|
* 发送请求的 URL带上参数
|
|
|
* @param param
|
|
|
* POST参数。
|
|
|
* @return 所代表远程资源的响应结果
|
|
|
*/
|
|
|
public static String sendPost(String url, String param) {
|
|
|
StringBuffer buffer = new StringBuffer();
|
|
|
PrintWriter out = null;
|
|
|
BufferedReader in = null;
|
|
|
HttpURLConnection conn = null;
|
|
|
try {
|
|
|
URL realUrl = new URL(url);
|
|
|
// 打开和URL之间的连接
|
|
|
conn = (HttpURLConnection) realUrl.openConnection();
|
|
|
conn.setRequestMethod("POST");
|
|
|
conn.setDoOutput(true);
|
|
|
conn.setDoInput(true);
|
|
|
conn.setUseCaches(false);
|
|
|
conn.setRequestProperty("Content-Type", "application/json");
|
|
|
conn.setRequestProperty("accept", "*/*");
|
|
|
conn.setRequestProperty("connection", "Keep-Alive");
|
|
|
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
|
|
|
osw.write(param.toString());
|
|
|
osw.flush();
|
|
|
|
|
|
// 读取返回内容
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
|
|
String temp;
|
|
|
while ((temp = br.readLine()) != null) {
|
|
|
buffer.append(temp);
|
|
|
buffer.append("\n");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
try {
|
|
|
if (out != null) {
|
|
|
out.close();
|
|
|
}
|
|
|
if (in != null) {
|
|
|
in.close();
|
|
|
}
|
|
|
} catch (IOException ex) {
|
|
|
ex.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return buffer.toString();
|
|
|
}
|
|
|
|
|
|
public static String postBody(String url, JSONObject params) {
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
headers.setContentType(type);
|
|
|
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
|
|
|
org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
|
|
|
String ret = restTemplate.postForObject(url, formEntity, String.class);
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
|
|
|
public static String postBodyHead(String url, JSONObject params, Map<String, Object> headerMap) {
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
for(String str:headerMap.keySet()){
|
|
|
headers.add(str,headerMap.get(str).toString());
|
|
|
}
|
|
|
org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
|
|
|
String ret = restTemplate.postForObject(url, formEntity, String.class);
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
public void putBody(String url, JSONObject params) {
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
headers.setContentType(type);
|
|
|
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
|
|
|
org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
|
|
|
restTemplate.put(url, formEntity, String.class);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送post请求
|
|
|
*
|
|
|
* @param url 请求地址
|
|
|
* @param params 请求参数
|
|
|
* @param chatSet 编码格式
|
|
|
* @param headerMap 请求头
|
|
|
* @return
|
|
|
*/
|
|
|
public String headerPost(String url, List<NameValuePair> params, String chatSet, Map<String,Object> headerMap) {
|
|
|
// 创建默认的httpClient实例.
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
// 创建httppost
|
|
|
HttpPost httppost = new HttpPost(url);
|
|
|
UrlEncodedFormEntity uefEntity;
|
|
|
try {
|
|
|
uefEntity = new UrlEncodedFormEntity(params, chatSet);
|
|
|
httppost.setEntity(uefEntity);
|
|
|
for(String str:headerMap.keySet()){
|
|
|
httppost.addHeader(str,headerMap.get(str).toString());
|
|
|
}
|
|
|
CloseableHttpResponse response = httpclient.execute(httppost);
|
|
|
try {
|
|
|
HttpEntity entity = response.getEntity();
|
|
|
if (entity != null) {
|
|
|
return EntityUtils.toString(entity, chatSet);
|
|
|
}
|
|
|
} finally {
|
|
|
response.close();
|
|
|
}
|
|
|
} catch (ClientProtocolException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (UnsupportedEncodingException e1) {
|
|
|
e1.printStackTrace();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
// 关闭连接,释放资源
|
|
|
try {
|
|
|
httpclient.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
|
|
|
public String get(String url, String chatSet,Map<String,Object> headerMap) {
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
try {
|
|
|
// 创建httpget.
|
|
|
url= url.replaceAll(" ", "%20");
|
|
|
HttpGet httpget = new HttpGet(url);
|
|
|
for(String str:headerMap.keySet()){
|
|
|
httpget.addHeader(str,headerMap.get(str).toString());
|
|
|
}
|
|
|
// 执行get请求.
|
|
|
CloseableHttpResponse response = httpclient.execute(httpget);
|
|
|
try {
|
|
|
// 获取响应实体
|
|
|
HttpEntity entity = response.getEntity();
|
|
|
if (entity != null) {
|
|
|
return EntityUtils.toString(entity, chatSet);
|
|
|
}
|
|
|
} finally {
|
|
|
response.close();
|
|
|
}
|
|
|
} catch (ClientProtocolException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (ParseException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
// 关闭连接,释放资源
|
|
|
try {
|
|
|
httpclient.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 原生字符串发送put请求
|
|
|
*
|
|
|
* @param url
|
|
|
* @param token
|
|
|
* @param jsonStr
|
|
|
* @return
|
|
|
* @throws ClientProtocolException
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
public static String doPut(String url, String token, String jsonStr) {
|
|
|
|
|
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
HttpPut httpPut = new HttpPut(url);
|
|
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
|
|
|
httpPut.setConfig(requestConfig);
|
|
|
httpPut.setHeader("Content-type", "application/json");
|
|
|
httpPut.setHeader("DataEncoding", "UTF-8");
|
|
|
httpPut.setHeader("token", token);
|
|
|
|
|
|
CloseableHttpResponse httpResponse = null;
|
|
|
try {
|
|
|
httpPut.setEntity(new StringEntity(jsonStr));
|
|
|
httpResponse = httpClient.execute(httpPut);
|
|
|
HttpEntity entity = httpResponse.getEntity();
|
|
|
String result = EntityUtils.toString(entity);
|
|
|
return result;
|
|
|
} catch (ClientProtocolException e) {
|
|
|
// TODO Auto-generated catch block
|
|
|
e.printStackTrace();
|
|
|
} catch (IOException e) {
|
|
|
// TODO Auto-generated catch block
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
if (httpResponse != null) {
|
|
|
try {
|
|
|
httpResponse.close();
|
|
|
} catch (IOException e) {
|
|
|
// TODO Auto-generated catch block
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
if (null != httpClient) {
|
|
|
try {
|
|
|
httpClient.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送delete请求
|
|
|
*
|
|
|
* @param url
|
|
|
* @param token
|
|
|
* @param jsonStr
|
|
|
* @return
|
|
|
* @throws ClientProtocolException
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
public static String doDelete(String url, String token, String jsonStr) {
|
|
|
|
|
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
HttpDelete httpDelete = new HttpDelete(url);
|
|
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
|
|
|
httpDelete.setConfig(requestConfig);
|
|
|
httpDelete.setHeader("Content-type", "application/json");
|
|
|
httpDelete.setHeader("DataEncoding", "UTF-8");
|
|
|
httpDelete.setHeader("token", token);
|
|
|
|
|
|
CloseableHttpResponse httpResponse = null;
|
|
|
try {
|
|
|
httpResponse = httpClient.execute(httpDelete);
|
|
|
HttpEntity entity = httpResponse.getEntity();
|
|
|
String result = EntityUtils.toString(entity);
|
|
|
return result;
|
|
|
} catch (ClientProtocolException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
if (httpResponse != null) {
|
|
|
try {
|
|
|
httpResponse.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
if (null != httpClient) {
|
|
|
try {
|
|
|
httpClient.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
public String postBodyRawForm(String url, String params) throws IOException {
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
List<NameValuePair> pairs = new ArrayList<>(0);
|
|
|
pairs.add(new BasicNameValuePair("object",params));
|
|
|
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs);
|
|
|
HttpPost httpPost = new HttpPost(url);
|
|
|
httpPost.setEntity(formEntity);
|
|
|
CloseableHttpResponse response = httpclient.execute(httpPost);
|
|
|
HttpEntity entity = response.getEntity();
|
|
|
String ret = EntityUtils.toString(entity);
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
public String getBodyRawForm(String url, String params) throws IOException {
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
List<NameValuePair> pairs = new ArrayList<>(0);
|
|
|
pairs.add(new BasicNameValuePair("object",params));
|
|
|
String entityString = EntityUtils.toString(new UrlEncodedFormEntity(pairs,Consts.UTF_8));
|
|
|
HttpGet httpGet = new HttpGet(url+"?"+entityString);
|
|
|
CloseableHttpResponse response = httpclient.execute(httpGet);
|
|
|
HttpEntity entity = response.getEntity();
|
|
|
String ret = EntityUtils.toString(entity);
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 爱牵挂请求
|
|
|
* @param url
|
|
|
* @param params
|
|
|
* @return
|
|
|
*/
|
|
|
public org.springframework.http.HttpEntity<JSONObject> cookiePostHttp(String url,MultiValueMap<String, String> params){
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
MediaType type = MediaType.parseMediaType("application/x-www-form-urlencoded");
|
|
|
headers.setContentType(type);
|
|
|
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
|
|
|
org.springframework.http.HttpEntity<MultiValueMap<String, String>> request = new org.springframework.http.HttpEntity<>(params, headers);
|
|
|
org.springframework.http.HttpEntity<JSONObject> response = restTemplate.exchange(url, HttpMethod.POST, request, JSONObject.class);
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
public org.springframework.http.HttpEntity<JSONObject> aqgCookieHttp(String url, MultiValueMap<String, String> params,HttpMethod method,String cookie) {
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
MediaType type = MediaType.parseMediaType("application/x-www-form-urlencoded");
|
|
|
headers.setContentType(type);
|
|
|
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
|
|
|
headers.add("Cookie",cookie);
|
|
|
org.springframework.http.HttpEntity<MultiValueMap<String, String>> request = new org.springframework.http.HttpEntity<>(params, headers);
|
|
|
org.springframework.http.HttpEntity<JSONObject> response = restTemplate.exchange(url, method, request, JSONObject.class);
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
public org.springframework.http.HttpEntity<JSONObject> assesTokenPostHttp(String url, JSONObject params,HttpMethod method){
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
MediaType type = MediaType.parseMediaType(MediaType.APPLICATION_JSON_VALUE);
|
|
|
headers.setContentType(type);
|
|
|
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
|
|
|
org.springframework.http.HttpEntity<String> request = new org.springframework.http.HttpEntity<>(params.toString(), headers);
|
|
|
org.springframework.http.HttpEntity<JSONObject> response = restTemplate.exchange(url, method, request, JSONObject.class);
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
public org.springframework.http.HttpEntity<JSONObject> iotAssesTokenPostHttp(String url, MultiValueMap<String, String> params,HttpMethod method){
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
MediaType type = MediaType.parseMediaType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
|
|
|
headers.setContentType(type);
|
|
|
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
|
|
|
org.springframework.http.HttpEntity<MultiValueMap<String, String>> request = new org.springframework.http.HttpEntity<>(params, headers);
|
|
|
org.springframework.http.HttpEntity<JSONObject> response = restTemplate.exchange(url, method, request, JSONObject.class);
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 向指定URL发送GET方法的请求
|
|
|
*
|
|
|
* @param url
|
|
|
* 发送请求的URL
|
|
|
* @return URL 所代表远程资源的响应结果
|
|
|
*/
|
|
|
public String sendGet(String url,Map<String,String> headerMap) {
|
|
|
String result = "";
|
|
|
BufferedReader in = null;
|
|
|
try {
|
|
|
URL realUrl = new URL(url);
|
|
|
// 打开和URL之间的连接
|
|
|
URLConnection connection = realUrl.openConnection();
|
|
|
// 设置通用的请求属性
|
|
|
connection.setRequestProperty("accept", "*/*");
|
|
|
connection.setRequestProperty("connection", "Keep-Alive");
|
|
|
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
if(headerMap!=null){
|
|
|
for (String key:headerMap.keySet()){
|
|
|
connection.setRequestProperty(key, headerMap.get(key));
|
|
|
}
|
|
|
}
|
|
|
// 建立实际的连接
|
|
|
connection.connect();
|
|
|
// 定义 BufferedReader输入流来读取URL的响应
|
|
|
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
String line;
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
result += line;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
try {
|
|
|
if (in != null) {
|
|
|
in.close();
|
|
|
}
|
|
|
} catch (Exception e2) {
|
|
|
e2.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 向指定 URL 发送POST方法的请求
|
|
|
*
|
|
|
* @param url
|
|
|
* 发送请求的 URL带上参数
|
|
|
* @param param
|
|
|
* POST参数。
|
|
|
* @return 所代表远程资源的响应结果
|
|
|
*/
|
|
|
public String sendPost(String url, String param,Map<String,String> headerMap) {
|
|
|
StringBuffer buffer = new StringBuffer();
|
|
|
PrintWriter out = null;
|
|
|
BufferedReader in = null;
|
|
|
HttpURLConnection conn = null;
|
|
|
try {
|
|
|
URL realUrl = new URL(url);
|
|
|
// 打开和URL之间的连接
|
|
|
conn = (HttpURLConnection) realUrl.openConnection();
|
|
|
conn.setRequestMethod("POST");
|
|
|
conn.setDoOutput(true);
|
|
|
conn.setDoInput(true);
|
|
|
conn.setUseCaches(false);
|
|
|
conn.setRequestProperty("Content-Type", "application/json");
|
|
|
conn.setRequestProperty("accept", "*/*");
|
|
|
conn.setRequestProperty("connection", "Keep-Alive");
|
|
|
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
if(headerMap!=null){
|
|
|
for (String key:headerMap.keySet()){
|
|
|
conn.setRequestProperty(key, headerMap.get(key));
|
|
|
}
|
|
|
}
|
|
|
OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
|
|
|
osw.write(param.toString());
|
|
|
osw.flush();
|
|
|
|
|
|
// 读取返回内容
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
|
|
String temp;
|
|
|
while ((temp = br.readLine()) != null) {
|
|
|
buffer.append(temp);
|
|
|
buffer.append("\n");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
try {
|
|
|
if (out != null) {
|
|
|
out.close();
|
|
|
}
|
|
|
if (in != null) {
|
|
|
in.close();
|
|
|
}
|
|
|
} catch (IOException ex) {
|
|
|
ex.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return buffer.toString();
|
|
|
}
|
|
|
|
|
|
public String postBodyAuthorization(String url, String token) {
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.add("Authorization", "Basic "+token);
|
|
|
headers.add("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
|
|
|
headers.add("Accept",MediaType.ALL_VALUE);
|
|
|
String str = "grant_type=client_credentials";
|
|
|
org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(str, headers);
|
|
|
String ret = restTemplate.postForObject(url, formEntity, String.class);
|
|
|
return ret;
|
|
|
}
|
|
|
}
|