/*     */ package com.picc.grab.util;
/*     */ 
/*     */ import com.alibaba.fastjson.JSONObject;
/*     */ import com.picc.grab.entity.common.dto.MyX509TrustManager;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.OutputStream;
/*     */ import java.net.URL;
/*     */ import java.security.SecureRandom;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.net.ssl.HttpsURLConnection;
/*     */ import javax.net.ssl.SSLContext;
/*     */ import javax.net.ssl.SSLSocketFactory;
/*     */ import javax.net.ssl.TrustManager;
/*     */ 
/*     */ public class GrabUtil
/*     */ {
/*  20 */   public String cookieStore = "";
/*     */   
/*     */   public InputStream getHttpsConn(String urlStr, String requestMethod, String params, Integer type) {
/*     */     try {
/*  24 */       SSLContext sslContext = SSLContext.getInstance("SSL");
/*  25 */       TrustManager[] tm = { (TrustManager)new MyX509TrustManager() };
/*     */       
/*  27 */       sslContext.init(null, tm, new SecureRandom());
/*     */       
/*  29 */       SSLSocketFactory ssf = sslContext.getSocketFactory();
/*  30 */       URL url = new URL(urlStr);
/*  31 */       HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
/*  32 */       connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36");
/*  33 */       if (type == null) {
/*  34 */         connection.setRequestProperty("Content-Type", "text/html;charset=UTF-8");
/*     */       } else {
/*  36 */         connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
/*     */       } 
/*  38 */       connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
/*  39 */       connection.setInstanceFollowRedirects(false);
/*  40 */       connection.setDoOutput(true);
/*  41 */       connection.setSSLSocketFactory(ssf);
/*  42 */       connection.setRequestMethod(requestMethod);
/*  43 */       connection.connect();
/*     */       
/*  45 */       if (null != params) {
/*  46 */         OutputStream os = connection.getOutputStream();
/*  47 */         os.write(params.getBytes("utf-8"));
/*  48 */         os.close();
/*     */       } 
/*  50 */       Map<String, List<String>> respone_headers = connection.getHeaderFields();
/*  51 */       for (String key : respone_headers.keySet()) {
/*  52 */         if (key != null && key.equals("Set-Cookie")) {
/*  53 */           List<String> string = respone_headers.get(key);
/*  54 */           for (String string2 : string) {
/*  55 */             string2 = string2.replace("path=/;", "");
/*  56 */             string2 = string2.replace("Httponly;", "");
/*  57 */             string2 = string2.replace("Secure", "");
/*  58 */             string2 = string2.replace("path=/", "");
/*  59 */             string2 = string2.replace("Path=/;", "");
/*  60 */             string2 = string2.replace("HttpOnly;", "");
/*  61 */             string2 = string2.replace("SameSite=Lax", "");
/*  62 */             this.cookieStore += string2;
/*     */           } 
/*  64 */           this.cookieStore = this.cookieStore.substring(0, this.cookieStore.length() - 1);
/*     */         } 
/*     */       } 
/*  67 */       InputStream input = connection.getInputStream();
/*  68 */       return input;
/*  69 */     } catch (Exception ex) {
/*  70 */       return null;
/*     */     } 
/*     */   }
/*     */ 
/*     */   
/*     */   public String getHttpsByCookieConn(String urlStr, Map params, String cookies, String token) {
/*     */     try {
/*  77 */       SSLContext sslContext = SSLContext.getInstance("SSL");
/*  78 */       TrustManager[] tm = { (TrustManager)new MyX509TrustManager() };
/*     */       
/*  80 */       sslContext.init(null, tm, new SecureRandom());
/*     */       
/*  82 */       SSLSocketFactory ssf = sslContext.getSocketFactory();
/*  83 */       URL url = new URL(urlStr);
/*  84 */       HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
/*  85 */       connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36");
/*  86 */       connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
/*  87 */       connection.setRequestProperty("Accept", "application/json, text/plain, */*");
/*  88 */       if (!"".equals(cookies) && cookies != null) {
/*  89 */         connection.setRequestProperty("Cookie", cookies);
/*     */       }
/*  91 */       if (!"".equals(token) && token != null) {
/*  92 */         connection.setRequestProperty("token", token);
/*     */       }
/*  94 */       connection.setInstanceFollowRedirects(false);
/*  95 */       connection.setSSLSocketFactory(ssf);
/*  96 */       connection.setRequestMethod("POST");
/*     */       
/*  98 */       if (null != params && params.size() > 0) {
/*  99 */         connection.setDoOutput(true);
/* 100 */         JSONObject jsonObj = new JSONObject(params);
/* 101 */         OutputStream os = connection.getOutputStream();
/* 102 */         String jsonData = jsonObj.toString();
/* 103 */         os.write(jsonData.getBytes("utf-8"));
/* 104 */         os.close();
/*     */       } 
/* 106 */       connection.connect();
/* 107 */       InputStream input = connection.getInputStream();
/* 108 */       ByteArrayOutputStream bitstream = cloneInputStream(input);
/* 109 */       String str = new String(bitstream.toByteArray(), "utf-8");
/* 110 */       return str;
/* 111 */     } catch (Exception ex) {
/* 112 */       return null;
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public ByteArrayOutputStream cloneInputStream(InputStream input) {
/*     */     try {
/* 121 */       ByteArrayOutputStream baos = new ByteArrayOutputStream();
/* 122 */       byte[] buffer = new byte[1024];
/*     */       int len;
/* 124 */       while ((len = input.read(buffer)) > -1) {
/* 125 */         baos.write(buffer, 0, len);
/*     */       }
/* 127 */       baos.flush();
/* 128 */       return baos;
/* 129 */     } catch (IOException e) {
/* 130 */       e.printStackTrace();
/* 131 */       return null;
/*     */     } 
/*     */   }
/*     */ }


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