123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- package com.simuwang.daq.util;
- import cn.hutool.core.util.StrUtil;
- import com.huawei.shade.org.apache.http.HttpResponse;
- import com.huawei.shade.org.apache.http.client.methods.HttpGet;
- import com.huawei.shade.org.apache.http.client.methods.HttpPost;
- import com.huawei.shade.org.apache.http.client.methods.HttpRequestBase;
- import com.huawei.shade.org.apache.http.entity.StringEntity;
- import com.huawei.shade.org.apache.http.impl.client.CloseableHttpClient;
- import com.huawei.shade.org.apache.http.impl.client.HttpClients;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- import java.nio.charset.StandardCharsets;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- public class HttpClientUtil {
- static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
- public static String get(String url, Map<String, String> header) {
- HttpGet httpget = new HttpGet(url);
- addHeader(httpget, header);
- return getResult(httpget);
- }
- public static String get(String url, Map<String, String> header, String charsets) {
- HttpGet httpget = new HttpGet(url);
- header.put("EMAIL-PARSE-TOKEN", "EMAIL-PARSE-TOKEN");
- addHeader(httpget, header);
- return getResult(httpget, charsets);
- }
- private static HttpPost getPost(String url, Map<String, String> header, String json) {
- HttpPost httpPost = new HttpPost(url);
- if (StrUtil.isNotBlank(json)) {
- StringEntity entity = getEntity(json, StandardCharsets.UTF_8.name(), "application/json; charset=utf-8");
- httpPost.setEntity(entity);
- }
- addHeader(httpPost, header);
- return httpPost;
- }
- public static String post(String url, Map<String, String> header, String json) {
- // header.put("EMAIL-PARSE-TOKEN", "EMAIL-PARSE-TOKEN");
- HttpPost httpPost = getPost(url, header, json);
- return getResult(httpPost);
- }
- private static StringEntity getEntity(String content, String charset, String contentType) {
- StringEntity entity = new StringEntity(content, charset);
- entity.setContentEncoding(charset);
- entity.setContentType(contentType);
- return entity;
- }
- private static void addHeader(HttpRequestBase httpType, Map<String, String> header) {
- if (header != null && header.size() > 0) {
- Set<String> headerKey = header.keySet();
- Iterator var4 = headerKey.iterator();
- while (var4.hasNext()) {
- String key = (String) var4.next();
- httpType.setHeader(key, (String) header.get(key));
- }
- }
- }
- public static String getResult(HttpRequestBase httpType) {
- return getResult(httpType, StandardCharsets.UTF_8.name());
- }
- public static String getResult(HttpRequestBase httpType, String charsets) {
- CloseableHttpClient httpClient = null;
- String var8;
- try {
- httpClient = HttpClients.createDefault();
- HttpResponse response = httpClient.execute(httpType);
- int status = response.getStatusLine().getStatusCode();
- if (status != 200) {
- logger.error("请求失败url:{},错误的HTTP Status:{}", httpType.getURI().toString(), status);
- return null;
- }
- InputStream is = response.getEntity().getContent();
- String retString = InputStreamTOString(is, charsets);
- var8 = retString;
- } catch (Exception var17) {
- logger.error(httpType.getURI().toString(), var17);
- return null;
- } finally {
- try {
- if (httpClient != null) {
- httpClient.close();
- }
- } catch (IOException var16) {
- var16.printStackTrace();
- }
- }
- return var8;
- }
- private static String InputStreamTOString(InputStream in, String encoding) {
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- byte[] data = new byte[4096];
- boolean var4 = true;
- try {
- int count;
- while ((count = in.read(data, 0, 4096)) != -1) {
- outStream.write(data, 0, count);
- }
- String result = new String(outStream.toByteArray(), encoding);
- in.close();
- outStream.flush();
- outStream.close();
- return result;
- } catch (UnsupportedEncodingException var6) {
- var6.printStackTrace();
- return null;
- } catch (IOException var7) {
- var7.printStackTrace();
- return null;
- }
- }
- }
|