123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- package com.tld.util;
- import java.io.IOException;
- import java.net.URI;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.tld.mapper.ErrorMapper;
- import com.tld.model.Error;
- import org.apache.http.Header;
- import org.apache.http.NameValuePair;
- 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.entity.ContentType;
- 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.apache.http.HttpStatus;
- import org.springframework.beans.factory.annotation.Autowired;
- import javax.annotation.Resource;
- import javax.swing.text.html.parser.Entity;
- public class HttpClientUtil {
- @Autowired
- private ErrorMapper errorMapper;
- public static String doGet(String url, Map<String, String> param) {
- // 创建Httpclient对象
- CloseableHttpClient httpclient = HttpClients.createDefault();
- //设置请求超时时间(各项超时参数具体含义链接)
- RequestConfig requestConfig = RequestConfig.custom()
- .setConnectTimeout(10000)
- .setConnectionRequestTimeout(10000)
- .setSocketTimeout(10000)
- .build();
- String resultString = "";
- CloseableHttpResponse response = null;
- try {
- // 创建uri
- URIBuilder builder = new URIBuilder(url);
- if (param != null) {
- for (String key : param.keySet()) {
- builder.addParameter(key, param.get(key));
- }
- }
- URI uri = builder.build();
- // 创建http GET请求
- HttpGet httpGet = new HttpGet(uri);
- //给这个请求设置请求配置
- httpGet.setConfig(requestConfig);
- httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2");
- // 执行请求
- response = httpclient.execute(httpGet);
- // 判断返回状态是否为200
- if (response.getStatusLine().getStatusCode() == 200) {
- resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (response != null) {
- response.close();
- }
- httpclient.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return resultString;
- }
- public Map<String, Object> doPost(String url, JSONObject param) {
- // 创建Httpclient对象
- CloseableHttpClient httpClient = HttpClients.createDefault();
- //设置请求超时时间
- RequestConfig requestConfig = RequestConfig.custom()
- .setConnectTimeout(10000)
- .setConnectionRequestTimeout(10000)
- .setSocketTimeout(10000)
- .build();
- CloseableHttpResponse response = null;
- Map<String, Object> map = new HashMap<>();
- String resultString = null;
- try {
- // 创建Http Post请求
- HttpPost httpPost = new HttpPost(url);
- httpPost.setConfig(requestConfig);
- httpPost.setHeader("AuthType", "Anonymous");
- httpPost.setHeader("GSAppId", "BI");
- httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
- // 创建参数列表
- if (param != null) {
- //请求参数转JOSN字符串
- StringEntity entity = new StringEntity(param.toString(), "UTF-8");
- entity.setContentEncoding("UTF-8");
- entity.setContentType("application/json");
- httpPost.setEntity(entity);
- }
- response = httpClient.execute(httpPost);
- // 执行http请求
- String responseVal = EntityUtils.toString(response.getEntity(), "utf-8");
- JSONObject jsonObject = JSON.parseObject(responseVal);
- if(JSON.parseObject(jsonObject.get("result").toString()).get("State").equals("0")){
- //如果失败存入报错信息跟数据
- Error error = new Error()
- .setErrorInfo(resultString)
- .setUrl(url)
- .setDataVal(param.toString());
- errorMapper.addError(error);
- }
- resultString = EntityUtils.toString(response.getEntity(), "utf-8");
- map.put("msg", response.getStatusLine().getStatusCode());
- map.put("data", resultString);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- response.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return map;
- }
- public static String doPostJson(String url, String json) {
- // 创建Httpclient对象
- CloseableHttpClient httpClient = HttpClients.createDefault();
- //设置请求超时时间
- RequestConfig requestConfig = RequestConfig.custom()
- .setConnectTimeout(10000)
- .setConnectionRequestTimeout(10000)
- .setSocketTimeout(10000)
- .build();
- CloseableHttpResponse response = null;
- String resultString = "";
- try {
- // 创建Http Post请求
- HttpPost httpPost = new HttpPost(url);
- httpPost.setConfig(requestConfig);
- // 创建请求内容 ,发送json数据需要设置contentType
- StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
- httpPost.setEntity(entity);
- // 执行http请求
- response = httpClient.execute(httpPost);
- if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
- resultString = EntityUtils.toString(response.getEntity(), "utf-8");
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- response.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return resultString;
- }
- }
|