package com.guitu18.common.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* HttpClientUtils
* 该工具类仅为自用,基本上是用一个添加一个,并不全面,且不一定有通用性,仅供参考
*
* @author zhangkuan
* @date 2019/9/18
*/
@Log4j2
public class HttpClientUtils {

 /**
  * Get访问并返回HTML内容
  *
  * @param url    URL
  * @param header 请求头
  * @return HTML文本
  */
 public static String getHtml(String url, Map<String, String> header) {
     return getHtml(url, header, null);
 }

 /**
  * Get访问并返回HTML内容
  *
  * @param url     URL
  * @param header  请求头
  * @param cookies Cookie列表
  * @return HTML文本
  */
 public static String getHtml(String url, Map<String, String> header, List<BasicClientCookie> cookies) {
     CloseableHttpResponse response = null;
     try {
         HttpClientBuilder httpClientBuilder = HttpClients.custom();
         HttpGet httpGet = new HttpGet(url);
         // 添加请求头
         if (header != null && !header.isEmpty()) {
             for (Map.Entry<String, String> entry : header.entrySet()) {
                 httpGet.addHeader(entry.getKey(), entry.getValue());
             }
         }
         // 添加Cookie
         if (cookies != null && !cookies.isEmpty()) {
             // 创建CookieStore对象用来管理cookie
             CookieStore cookieStore = new BasicCookieStore();
             for (BasicClientCookie cookie : cookies) {
                 // 将cookie添加到cookieStore中
                 cookieStore.addCookie(cookie);
             }
             // 创建get方法的执行对象 HttpClient4.X之后是这样创建client对象 设置cookies和header信息
             httpClientBuilder.setDefaultCookieStore(cookieStore);
         }
         // 发起请求
         CloseableHttpClient client = httpClientBuilder.build();
         response = client.execute(httpGet);
         if (response.getStatusLine().getStatusCode() == 200) {
             return EntityUtils.toString(response.getEntity());
         } else {
             log.error("访问出错, Code: {}, Entity: {}", response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity()));
         }
     } catch (Exception e) {
         e.printStackTrace();
     } finally {
         if (response != null) {
             try {
                 response.close();
             } catch (IOException ignored) {
             }
         }
     }
     return null;
 }

 /**
  * Get访问并返回JSON内容
  *
  * @param url    URL
  * @param header 请求头
  * @return JSONObject
  */
 public static JSONObject getJSONObject(String url, Map<String, String> header) {
     CloseableHttpResponse response = null;
     try {
         HttpGet httpGet = new HttpGet(url);
         // 添加请求头
         if (header != null && !header.isEmpty()) {
             for (Map.Entry<String, String> entry : header.entrySet()) {
                 httpGet.addHeader(entry.getKey(), entry.getValue());
             }
         }
         response = HttpClients.custom().build().execute(httpGet);
         if (response.getStatusLine().getStatusCode() == 200) {
             return JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
         } else {
             log.error("访问出错, Code: {}, Entity: {}",
                     response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity()));
         }
     } catch (Exception e) {
         e.printStackTrace();
     } finally {
         if (response != null) {
             try {
                 response.close();
             } catch (IOException ignored) {
             }
         }
     }
     return null;
 }

 /**
  * Chrome浏览器请求头
  *
  * @return header
  */
 public static Map<String, String> chromeHeader() {
     Map<String, String> header = new HashMap<>();
     header.put("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
     header.put("accept-encoding", "gzip, deflate, br");
     header.put("accept-language", "zh-CN,zh;q=0.9");
     header.put("sec-fetch-dest", "document");
     header.put("sec-fetch-mode", "navigate");
     header.put("sec-fetch-site", "none");
     header.put("sec-fetch-user", "?1");
     header.put("upgrade-insecure-requests", "1");
     header.put("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36");
     return header;
 }

 /**
  * HttpClient携带Cookie访问的正确打开方式
  */
 public static void main(String[] args) {
     try {
         // 创建get访问对象
         HttpGet get = new HttpGet("http://www.baidu.com");
         // 创建CookieStore对象用来管理cookie
         CookieStore cookieStore = new BasicCookieStore();
         // new BasicClientCookie对象 用来注入cookie
         BasicClientCookie cookie = new BasicClientCookie("sessionID", "ABCDEFGHIJKLMN");

         //设置cookie的作用域
         cookie.setDomain("www.baidu.com");

         //将cookie添加到cookieStore中
         cookieStore.addCookie(cookie);
         // 创建get方法的执行对象 HttpClient4.X之后是这样创建client对象 设置cookies和header信息
         CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();

         HttpResponse response = client.execute(get);

         // 将response对象转换成String类型
         String responseStr = EntityUtils.toString(response.getEntity(), "utf-8");
         System.out.println(responseStr);

     } catch (Exception e) {
         e.printStackTrace();
     }
 }

}