package com.hisign.common;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.util.UriUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* @httpClient工具类
*/
public class HttpClientUtil {
private static HttpGet HTTPGET;
private static HttpPut HTTPPUT;
private static HttpPost HTTPPOST;
private static CloseableHttpClient HTTPCLIENT;
private static String EMPTY_STR = null;
private static String UTF_8 = "UTF-8";
private static Logger log = LoggerFactory.getLogger(HttpClientUtil.class);
/**
* @param url
* @return
*/
public static String get(String url) {
try {
HTTPCLIENT = HttpClients.createDefault();
//声明请求方式
HTTPGET = new HttpGet(url);
//获取相应数据,这里可以获取相应的数据
HttpResponse httpResponse = HTTPCLIENT.execute(HTTPGET);
//拿到实体
HttpEntity httpEntity = httpResponse.getEntity();
//获取结果,这里可以正对相应的数据精细字符集的转码
EMPTY_STR = null;
if (httpEntity != null) {
EMPTY_STR = EntityUtils.toString(httpEntity, UTF_8);
}
} catch (Exception e) {
e.printStackTrace();
}
return EMPTY_STR;
}
/**
* @param url
* @param map
* @return
*/
public static String put(String url, Map<String, Object> map) {
try {
HTTPCLIENT = HttpClients.createDefault();
//声明请求方式
HTTPPUT = new HttpPut(url);
//设置消息头
HTTPPUT.setHeader("Content-Type", "application/json;charset=utf-8");
HTTPPUT.setHeader("Accept", "application/json");
//设置发送数据(数据尽量为json),可以设置数据的发送时的字符集
HTTPPUT.setEntity(new StringEntity(JSONObject.toJSONString(map), UTF_8));
//获取相应数据,这里可以获取相应的数据
HttpResponse httpResponse = HTTPCLIENT.execute(HTTPPUT);
//拿到实体
HttpEntity httpEntity = httpResponse.getEntity();
//获取结果,这里可以正对相应的数据精细字符集的转码
EMPTY_STR = null;
if (httpEntity != null) {
EMPTY_STR = EntityUtils.toString(httpEntity, UTF_8);
}
} catch (Exception e) {
e.printStackTrace();
}
return EMPTY_STR;
}
/**
* @param url
* @param map
* @param status
* @return
*/
public static String post(String url, Map<String, Object> map, boolean status) {
try {
HTTPCLIENT = HttpClients.createDefault();
//声明请求方式
HTTPPOST = new HttpPost(url);
//设置消息头
if (status) {
HTTPPOST.setHeader("Content-Type", "application/json;charset=utf-8");
HTTPPOST.setHeader("Accept", "application/json");
String string = JSONObject.toJSONString(map);
HTTPPOST.setEntity(new StringEntity(string, UTF_8));
} else {
HTTPPOST.setEntity(new UrlEncodedFormEntity(covertParams2NVPS(map), UTF_8));
}
//获取相应数据,这里可以获取相应的数据
HttpResponse httpResponse = HTTPCLIENT.execute(HTTPPOST);
//拿到实体
HttpEntity httpEntity = httpResponse.getEntity();
//获取结果,这里可以正对相应的数据精细字符集的转码
EMPTY_STR = null;
if (httpEntity != null) {
EMPTY_STR = EntityUtils.toString(httpEntity, UTF_8);
}
} catch (Exception e) {
e.printStackTrace();
}
return EMPTY_STR;
}
/**
* @param url
* @param jsonStr
* @return
*/
public static String postByJson(String url, String jsonStr) {
try {
HTTPCLIENT = HttpClients.createDefault();
//声明请求方式
HTTPPOST = new HttpPost(url);
//设置消息头
HTTPPOST.setHeader("Content-Type", "application/json;charset=utf-8");
HTTPPOST.setHeader("Accept", "application/json");
HTTPPOST.setEntity(new StringEntity(jsonStr, UTF_8));
//获取相应数据,这里可以获取相应的数据
HttpResponse httpResponse = HTTPCLIENT.execute(HTTPPOST);
//拿到实体
HttpEntity httpEntity = httpResponse.getEntity();
//获取结果,这里可以正对相应的数据精细字符集的转码
EMPTY_STR = null;
if (httpEntity != null) {
EMPTY_STR = EntityUtils.toString(httpEntity, UTF_8);
}
} catch (Exception e) {
e.printStackTrace();
}
return EMPTY_STR;
}
/**
* @param params
* @return
*/
private static ArrayList<NameValuePair> covertParams2NVPS(Map<String, Object> params) {
ArrayList<NameValuePair> pairs = new ArrayList<>();
for (Map.Entry<String, Object> param : params.entrySet()) {
pairs.add(new BasicNameValuePair(param.getKey(), String.valueOf(param.getValue())));
}
return pairs;
}
/**
* 发起Get请求
*
* @param urlStr
* @return
*/
public final static byte[] doGetRequestForFile(String urlStr) {
InputStream is = null;
ByteArrayOutputStream os = null;
byte[] buff = new byte[1024];
int len = 0;
try {
URL url = new URL(UriUtils.encodePath(urlStr, UTF_8));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "plain/text;charset=" + UTF_8);
conn.setRequestProperty("charset", UTF_8);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("GET");
// conn.setReadTimeout(DEFAULT_TIME_OUT);
conn.connect();
is = conn.getInputStream();
os = new ByteArrayOutputStream();
while ((len = is.read(buff)) != -1) {
os.write(buff, 0, len);
}
return os.toByteArray();
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
log.error("【关闭流异常】");
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
log.error("【关闭流异常】");
}
}
}
}
/**
* 测试
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// Map<String, Object> map = new HashMap<String, Object>();
// JSONArray jsonArray = new JSONArray();
jsonArray.add("A1101074000002014120111");
jsonArray.add("A1101086100002013110019");
// System.out.println(jsonArray);
map.put("CASE_NO",jsonArray);
// // true说明case_desc是案情,false说明是逗号分隔的关键信息
// map.put("IDENTIFIER", "0");
// map.put("OCCBEG_DATE", "");
// map.put("OCCEND_DATE", "");
// String caseDesc = "2017年12月2日8时许,祁瑞升在房山区窦店镇下坡店村中国信息大学宿舍内,与QQ号(315048567)联系,通过在携程网购买任我行电子礼品卡后,将卡号及密码发给对方进行刷单,每单返利5%。对方将携程网任我行卡链接发送给祁瑞升,一共5张,每张面值500元。在购买了2500元礼品卡,将卡号及密码发给对方后,对方没有将本金及返利转账给祁瑞升,并称还需继续刷单。";
// map.put("CASE_DESC", "15234364633");
// String url = "http://172.16.0.124:51110/sim";
// String result = post(url, map, false);
// result = Common.unicodeToString(result.trim()).replace("\\", "");
// System.out.println(result);
byte[] bs = HttpClientUtil.doGetRequestForFile("http://192.168.129.207:58080/M00/06/3E/wKiBy1-PqUuAfnxjAAHQLCyDA0s483.png");
log.info("获取视图中的受案登记表:【{}】个",4);
}
}
httpClient -工具类
最新推荐文章于 2024-06-22 23:14:28 发布