/*     */ package com.picc.grab.util;
/*     */ 
/*     */ import com.alibaba.fastjson.JSONObject;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.InputStreamReader;
/*     */ import java.nio.charset.Charset;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ import javax.servlet.ServletInputStream;
/*     */ import javax.servlet.ServletRequest;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import org.apache.http.HttpEntity;
/*     */ import org.apache.http.HttpResponse;
/*     */ import org.apache.http.client.config.RequestConfig;
/*     */ import org.apache.http.client.methods.HttpGet;
/*     */ import org.apache.http.client.methods.HttpPost;
/*     */ import org.apache.http.client.methods.HttpUriRequest;
/*     */ import org.apache.http.entity.StringEntity;
/*     */ import org.apache.http.impl.client.CloseableHttpClient;
/*     */ import org.apache.http.impl.client.HttpClients;
/*     */ import org.apache.http.util.EntityUtils;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class HttpUtil
/*     */ {
/*  35 */   private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
/*     */   
/*  37 */   private static Charset chartset = Charset.forName("UTF-8");
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static JSONObject doPostJson(String url, String jsonFormString, Integer timeOut) throws Exception {
/*  51 */     logger.info("doPostJson请求内容：{}", jsonFormString);
/*     */     
/*  53 */     CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
/*  54 */     HttpPost post = new HttpPost(url);
/*     */     
/*  56 */     post.addHeader("Content-Type", "application/json");
/*     */ 
/*     */ 
/*     */     
/*  60 */     if (timeOut != null && timeOut.intValue() != 0) {
/*     */ 
/*     */ 
/*     */       
/*  64 */       RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeOut.intValue()).setConnectTimeout(timeOut.intValue()).build();
/*  65 */       post.setConfig(requestConfig);
/*     */     } 
/*     */     
/*  68 */     StringEntity s = new StringEntity(jsonFormString, chartset);
/*  69 */     post.setEntity((HttpEntity)s);
/*  70 */     HttpResponse httpResponse = closeableHttpClient.execute((HttpUriRequest)post);
/*     */     
/*  72 */     JSONObject response = null;
/*  73 */     if (httpResponse.getStatusLine().getStatusCode() == 200) {
/*  74 */       String result = EntityUtils.toString(httpResponse.getEntity());
/*  75 */       response = JSONObject.parseObject(result);
/*     */     } else {
/*  77 */       String errorMsg = EntityUtils.toString(httpResponse.getEntity());
/*  78 */       logger.error("请求响应结果异常: {}", errorMsg);
/*  79 */       return JSONObject.parseObject(errorMsg);
/*     */     } 
/*  81 */     if (response != null);
/*     */ 
/*     */     
/*  84 */     return response;
/*     */   }
/*     */   
/*     */   public static JSONObject doPostJson(String url, String jsonFormString) throws Exception {
/*  88 */     JSONObject jo = doPostJson(url, jsonFormString, Integer.valueOf(0));
/*  89 */     if (jo != null) {
/*  90 */       logger.info("doPost请求返回内容：{}", jo.toJSONString());
/*     */     }
/*  92 */     return jo;
/*     */   }
/*     */   
/*     */   public static JSONObject doPostJson(String url, String jsonString, Map<String, Object> headers) throws Exception {
/*  96 */     JSONObject jo = doPostJson(url, jsonString, null, headers);
/*  97 */     if (jo != null) {
/*  98 */       logger.info("doPost请求返回内容：{}", jo.toJSONString());
/*     */     }
/* 100 */     return jo;
/*     */   }
/*     */ 
/*     */   
/*     */   public static JSONObject doPostJson(String url, String jsonString, Integer timeOut, Map<String, Object> headers) throws Exception {
/* 105 */     logger.info("doPost请求参数内容：{} ，url：{}", jsonString, url);
/*     */     
/* 107 */     CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
/* 108 */     HttpPost post = new HttpPost(url);
/* 109 */     if (headers != null && !headers.isEmpty()) {
/* 110 */       headers.forEach((k, v) -> post.addHeader(k, StringUtil.isBlank(v) ? "" : v.toString()));
/*     */     }
/*     */ 
/*     */ 
/*     */     
/* 115 */     if (timeOut != null && timeOut.intValue() != 0) {
/*     */ 
/*     */ 
/*     */       
/* 119 */       RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeOut.intValue()).setConnectTimeout(timeOut.intValue()).build();
/* 120 */       post.setConfig(requestConfig);
/*     */     } 
/*     */     
/* 123 */     StringEntity s = new StringEntity(jsonString, chartset);
/* 124 */     post.setEntity((HttpEntity)s);
/* 125 */     HttpResponse httpResponse = closeableHttpClient.execute((HttpUriRequest)post);
/*     */     
/* 127 */     JSONObject response = null;
/* 128 */     if (httpResponse.getStatusLine().getStatusCode() == 200) {
/* 129 */       String result = EntityUtils.toString(httpResponse.getEntity());
/* 130 */       response = JSONObject.parseObject(result);
/*     */     } else {
/* 132 */       String errorMsg = EntityUtils.toString(httpResponse.getEntity());
/* 133 */       logger.error("请求响应结果异常: {}", errorMsg);
/* 134 */       return JSONObject.parseObject(errorMsg);
/*     */     } 
/* 136 */     if (response != null);
/*     */     
/* 138 */     return response;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static JSONObject doPostHeaderJson(String url, String jsonFormString, HashMap<String, String> headers) throws Exception {
/* 156 */     logger.info("doPostJson请求内容：{}", jsonFormString);
/*     */     
/* 158 */     CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
/* 159 */     HttpPost post = new HttpPost(url);
/* 160 */     post.addHeader("Content-Type", "application/json");
/*     */     
/* 162 */     if (headers != null && !headers.isEmpty()) {
/* 163 */       headers.entrySet().forEach(m -> post.addHeader(((String)m.getKey()).toString(), ((String)m.getValue()).toString()));
/*     */     }
/*     */ 
/*     */ 
/*     */     
/* 168 */     StringEntity s = new StringEntity(jsonFormString, chartset);
/* 169 */     post.setEntity((HttpEntity)s);
/* 170 */     HttpResponse httpResponse = closeableHttpClient.execute((HttpUriRequest)post);
/*     */     
/* 172 */     JSONObject response = null;
/* 173 */     if (httpResponse.getStatusLine().getStatusCode() == 200) {
/* 174 */       String result = EntityUtils.toString(httpResponse.getEntity());
/* 175 */       response = JSONObject.parseObject(result);
/*     */     } else {
/* 177 */       String errorMsg = EntityUtils.toString(httpResponse.getEntity());
/* 178 */       logger.info("请求响应结果异常: {}", errorMsg);
/* 179 */       response = JSONObject.parseObject(errorMsg);
/*     */     } 
/* 181 */     return response;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static JSONObject doPostForm(String url, String formString) throws Exception {
/* 196 */     logger.info("doPost请求内容：{}", formString);
/*     */     
/* 198 */     CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
/* 199 */     HttpPost post = new HttpPost(url);
/* 200 */     post.addHeader("text/plain", "UTF-8");
/* 201 */     post.addHeader("Content-Type", "application/x-www-form-urlencoded");
/*     */     
/* 203 */     StringEntity s = new StringEntity(formString, chartset);
/* 204 */     post.setEntity((HttpEntity)s);
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     
/* 210 */     HttpResponse httpResponse = closeableHttpClient.execute((HttpUriRequest)post);
/*     */     
/* 212 */     JSONObject response = null;
/* 213 */     if (httpResponse.getStatusLine().getStatusCode() == 200) {
/* 214 */       String result = EntityUtils.toString(httpResponse.getEntity());
/* 215 */       response = JSONObject.parseObject(result);
/*     */     } else {
/* 217 */       String errorMsg = EntityUtils.toString(httpResponse.getEntity());
/* 218 */       throw new RuntimeException(errorMsg);
/*     */     } 
/* 220 */     if (response != null) {
/* 221 */       logger.info("doPost响应结果：{}", response.toJSONString());
/*     */     }
/* 223 */     return response;
/*     */   }
/*     */ 
/*     */   
/*     */   public static JSONObject doGet(String url) throws Exception {
/* 228 */     CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
/* 229 */     HttpGet get = new HttpGet(url);
/*     */ 
/*     */     
/* 232 */     get.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
/* 233 */     get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36");
/*     */     
/* 235 */     JSONObject response = null;
/* 236 */     HttpResponse httpResponse = closeableHttpClient.execute((HttpUriRequest)get);
/* 237 */     if (httpResponse.getStatusLine().getStatusCode() == 200) {
/* 238 */       String result = EntityUtils.toString(httpResponse.getEntity());
/* 239 */       response = JSONObject.parseObject(result);
/*     */     } else {
/* 241 */       String errorMsg = EntityUtils.toString(httpResponse.getEntity());
/* 242 */       throw new RuntimeException(errorMsg);
/*     */     } 
/*     */     
/* 245 */     if (response != null) {
/* 246 */       logger.info("doPost响应结果：{}", response.toJSONString());
/*     */     }
/*     */     
/* 249 */     return response;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static InputStream doGetFile(String url) throws Exception {
/* 257 */     CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
/* 258 */     HttpGet get = new HttpGet(url);
/*     */ 
/*     */     
/* 261 */     get.addHeader("Accept", "textml,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
/* 262 */     get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36");
/*     */     
/* 264 */     InputStream inputStream = null;
/* 265 */     HttpResponse httpResponse = closeableHttpClient.execute((HttpUriRequest)get);
/* 266 */     if (httpResponse.getStatusLine().getStatusCode() == 200) {
/* 267 */       inputStream = httpResponse.getEntity().getContent();
/*     */     } else {
/* 269 */       String errorMsg = EntityUtils.toString(httpResponse.getEntity());
/* 270 */       throw new RuntimeException(errorMsg);
/*     */     } 
/*     */     
/* 273 */     return inputStream;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static boolean isMobileDevice(HttpServletRequest request) {
/* 285 */     String userAgent = request.getHeader("USER-AGENT").toLowerCase();
/* 286 */     if (null == userAgent) {
/* 287 */       userAgent = "";
/*     */     }
/*     */     
/* 290 */     Matcher matcherPhone = phonePat.matcher(userAgent);
/* 291 */     Matcher matcherTable = tablePat.matcher(userAgent);
/* 292 */     if (matcherPhone.find() || matcherTable.find()) {
/* 293 */       return true;
/*     */     }
/* 295 */     return false;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/* 303 */   static String phoneReg = "\\b(ip(hone|od)|android|opera m(ob|in)i|windows (phone|ce)|blackberry|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp|laystation portable)|nokia|fennec|htc[-_]|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
/*     */ 
/*     */ 
/*     */ 
/*     */   
/* 308 */   static String tableReg = "\\b(ipad|tablet|(Nexus 7)|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
/*     */ 
/*     */ 
/*     */   
/* 312 */   static Pattern phonePat = Pattern.compile(phoneReg, 2);
/* 313 */   static Pattern tablePat = Pattern.compile(tableReg, 2);
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static boolean isWeChat(HttpServletRequest request) {
/* 323 */     String userAgent = request.getHeader("user-agent").toLowerCase();
/* 324 */     return !(userAgent == null || userAgent.indexOf("micromessenger") == -1);
/*     */   }
/*     */ 
/*     */   
/*     */   public static JSONObject doGet(String url, Map<String, String> header) throws Exception {
/* 329 */     CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
/* 330 */     HttpGet get = new HttpGet(url);
/* 331 */     if (header != null && !header.isEmpty()) {
/* 332 */       header.forEach((k, v) -> get.setHeader(k, v));
/*     */     }
/*     */ 
/*     */ 
/*     */     
/* 337 */     JSONObject response = null;
/* 338 */     HttpResponse httpResponse = closeableHttpClient.execute((HttpUriRequest)get);
/* 339 */     if (httpResponse.getStatusLine().getStatusCode() == 200) {
/* 340 */       HttpEntity s = httpResponse.getEntity();
/* 341 */       String result = EntityUtils.toString(s);
/* 342 */       response = JSONObject.parseObject(result);
/*     */     } else {
/* 344 */       String errorMsg = EntityUtils.toString(httpResponse.getEntity());
/* 345 */       logger.error("请求响应结果异常: {}", errorMsg);
/* 346 */       return JSONObject.parseObject(errorMsg);
/*     */     } 
/*     */     
/* 349 */     if (response != null) {
/* 350 */       logger.info("doPost响应结果：{}", response.toJSONString());
/*     */     }
/*     */     
/* 353 */     return response;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String getBodyString(ServletRequest request) {
/*     */     ServletInputStream servletInputStream = null;
/* 363 */     StringBuilder sb = new StringBuilder();
/* 364 */     InputStream inputStream = null;
/* 365 */     BufferedReader reader = null;
/*     */     try {
/* 367 */       servletInputStream = request.getInputStream();
/* 368 */       reader = new BufferedReader(new InputStreamReader((InputStream)servletInputStream, Charset.forName("UTF-8")));
/* 369 */       String line = "";
/* 370 */       while ((line = reader.readLine()) != null) {
/* 371 */         sb.append(line);
/*     */       }
/* 373 */     } catch (IOException e) {
/* 374 */       e.printStackTrace();
/*     */     } finally {
/* 376 */       if (servletInputStream != null) {
/*     */         try {
/* 378 */           servletInputStream.close();
/* 379 */         } catch (IOException e) {
/* 380 */           e.printStackTrace();
/*     */         } 
/*     */       }
/* 383 */       if (reader != null) {
/*     */         try {
/* 385 */           reader.close();
/* 386 */         } catch (IOException e) {
/* 387 */           e.printStackTrace();
/*     */         } 
/*     */       }
/*     */     } 
/* 391 */     return sb.toString();
/*     */   }
/*     */ }


/* Location:              C:\Users\Administrator\Desktop\ROOT\WEB-INF\classes\!\com\picc\gra\\util\HttpUtil.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */