package com.ycl.smoke.utils;
|
|
import com.ycl.service.redis.RedisService;
|
import com.ycl.utils.redis.RedisKey;
|
import org.apache.http.*;
|
import org.apache.http.client.ResponseHandler;
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.entity.StringEntity;
|
import org.apache.http.impl.client.BasicResponseHandler;
|
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 javax.annotation.Resource;
|
import java.io.BufferedReader;
|
import java.io.IOException;
|
import java.io.InputStreamReader;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.net.URLEncoder;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
|
public class HttpUtil {
|
|
private static final CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
public static String doGet(String urlPath, Map<String, Object> params)
|
throws Exception {
|
StringBuilder sb = new StringBuilder(urlPath);
|
if (params != null && !params.isEmpty()) { // 说明有参数
|
sb.append("?");
|
|
Set<Map.Entry<String, Object>> set = params.entrySet();
|
for (Map.Entry<String, Object> entry : set) { // 遍历map里面的参数
|
String key = entry.getKey();
|
String value = "";
|
if (null != entry.getValue()) {
|
value = entry.getValue().toString();
|
// 转码
|
value = URLEncoder.encode(value, "UTF-8");
|
}
|
sb.append(key).append("=").append(value).append("&");
|
}
|
|
sb.deleteCharAt(sb.length() - 1); // 删除最后一个&
|
}
|
// System.out.println(sb.toString());
|
|
URL url = new URL(sb.toString());
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
conn.setConnectTimeout(5000); // 5s超时
|
conn.setRequestMethod("GET");
|
|
if (conn.getResponseCode() == HttpStatus.SC_OK) {// HttpStatus.SC_OK ==
|
// 200
|
BufferedReader reader = new BufferedReader(new InputStreamReader(
|
conn.getInputStream()));
|
StringBuilder sbs = new StringBuilder();
|
String line;
|
while ((line = reader.readLine()) != null) {
|
sbs.append(line);
|
}
|
// JSONObject jsonObject = new JSONObject(sbs.toString());
|
return sbs.toString();
|
}
|
|
return null;
|
}
|
|
|
/**
|
* 发送HttpPost请求,参数为map
|
* @param url
|
* @param map
|
* @return
|
*/
|
public static String sendPost(String url, Map<String, Object> map) {
|
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
|
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
|
}
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
|
HttpPost httppost = new HttpPost(url);
|
httppost.setEntity(entity);
|
CloseableHttpResponse response = null;
|
try {
|
response = httpclient.execute(httppost);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
HttpEntity entity1 = response.getEntity();
|
String result = null;
|
try {
|
result = EntityUtils.toString(entity1);
|
} catch (ParseException | IOException e) {
|
e.printStackTrace();
|
}
|
return result;
|
}
|
|
public static String HttpPostWithJson(String url, String json,String token) {
|
String returnValue = "这是默认返回值,接口调用失败";
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
ResponseHandler<String> responseHandler = new BasicResponseHandler();
|
try{
|
//第一步:创建HttpClient对象
|
httpClient = HttpClients.createDefault();
|
|
//第二步:创建httpPost对象
|
HttpPost httpPost = new HttpPost(url);
|
|
//第三步:给httpPost设置JSON格式的参数
|
StringEntity requestEntity = new StringEntity(json,"utf-8");
|
requestEntity.setContentEncoding("UTF-8");
|
httpPost.setHeader("Content-type", "application/json");
|
httpPost.setHeader("Auth",token);
|
httpPost.setEntity(requestEntity);
|
|
//第四步:发送HttpPost请求,获取返回值
|
returnValue = httpClient.execute(httpPost,responseHandler); //调接口获取返回值时,必须用此方法
|
|
}
|
catch(Exception e)
|
{
|
e.printStackTrace();
|
}
|
|
finally {
|
try {
|
httpClient.close();
|
} catch (IOException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
}
|
//第五步:处理返回值
|
return returnValue;
|
}
|
}
|