package com.qianwen.smartman.common.utils; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.List; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.client.CookieStore; 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.methods.HttpRequestBase; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicHeader; import org.apache.http.util.EntityUtils; import org.assertj.core.util.Lists; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.qianwen.smartman.common.constant.DncConstant; public class SimpleHttpClient { private static final Logger log = LoggerFactory.getLogger(SimpleHttpClient.class); private static final SimpleHttpClient simpleHttpClient = new SimpleHttpClient(); private CookieStore cookieStore = new BasicCookieStore(); private final List
DEF_HEADER = Lists.newArrayList(); private CloseableHttpClient http; public SimpleHttpClient() { this.DEF_HEADER.add(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36")); this.DEF_HEADER.add(new BasicHeader("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")); this.DEF_HEADER.add(new BasicHeader("Accept-Language", "zh-CN,zh;q=0.9")); this.http = HttpClients.custom().setDefaultHeaders(this.DEF_HEADER).setDefaultCookieStore(this.cookieStore).build(); } public void setCookieStore(final CookieStore cookieStore) { this.cookieStore = cookieStore; } public void setHttp(final CloseableHttpClient http) { this.http = http; } public boolean equals(final Object o) { if (o == this) { return true; } if (o instanceof SimpleHttpClient) { SimpleHttpClient other = (SimpleHttpClient) o; if (other.canEqual(this)) { Object this$cookieStore = getCookieStore(); Object other$cookieStore = other.getCookieStore(); if (this$cookieStore == null) { if (other$cookieStore != null) { return false; } } else if (!this$cookieStore.equals(other$cookieStore)) { return false; } Object this$DEF_HEADER = getDEF_HEADER(); Object other$DEF_HEADER = other.getDEF_HEADER(); if (this$DEF_HEADER == null) { if (other$DEF_HEADER != null) { return false; } } else if (!this$DEF_HEADER.equals(other$DEF_HEADER)) { return false; } Object this$http = getHttp(); Object other$http = other.getHttp(); return this$http == null ? other$http == null : this$http.equals(other$http); } return false; } return false; } protected boolean canEqual(final Object other) { return other instanceof SimpleHttpClient; } public int hashCode() { Object $cookieStore = getCookieStore(); int result = (1 * 59) + ($cookieStore == null ? 43 : $cookieStore.hashCode()); Object $DEF_HEADER = getDEF_HEADER(); int result2 = (result * 59) + ($DEF_HEADER == null ? 43 : $DEF_HEADER.hashCode()); Object $http = getHttp(); return (result2 * 59) + ($http == null ? 43 : $http.hashCode()); } public String toString() { return "SimpleHttpClient(cookieStore=" + getCookieStore() + ", DEF_HEADER=" + getDEF_HEADER() + ", http=" + getHttp() + ")"; } public static SimpleHttpClient instance() { return simpleHttpClient; } public CookieStore getCookieStore() { return this.cookieStore; } public List
getDEF_HEADER() { return this.DEF_HEADER; } public CloseableHttpClient getHttp() { return this.http; } public static void download(String url, String filepath, int cache) throws Exception { InputStream inputStream = null; FileOutputStream fileOutputStream = null; try { try { HttpEntity entity = simpleHttpClient.execute(new HttpGet(url)).getEntity(); inputStream = entity.getContent(); File file = new File(filepath); file.getParentFile().mkdirs(); fileOutputStream = new FileOutputStream(file); byte[] buffer = new byte[cache]; while (true) { int ch = inputStream.read(buffer); if (ch == -1) { break; } fileOutputStream.write(buffer, 0, ch); } if (inputStream != null) { inputStream.close(); } if (fileOutputStream != null) { fileOutputStream.flush(); fileOutputStream.close(); } } catch (Exception e) { e.printStackTrace(); throw e; } } catch (Throwable th) { if (inputStream != null) { inputStream.close(); } if (fileOutputStream != null) { fileOutputStream.flush(); fileOutputStream.close(); } throw th; } } public String get(String url, List
headers) throws Exception { HttpGet httpget = new HttpGet(url); if (headers != null) { headers.forEach(header -> { httpget.addHeader(header); }); } CloseableHttpResponse response = execute(httpget); try { String res = EntityUtils.toString(response.getEntity(), DncConstant.CHARSET); httpget.abort(); response.close(); return res; } catch (Throwable th) { response.close(); throw th; } } private void addCookies(String host, CookieStore cookieStore) { cookieStore.getCookies().forEach(cookie -> { this.cookieStore.addCookie(cookie); }); } public void close() { try { this.http.close(); } catch (IOException e) { e.printStackTrace(); } } public String post(String url, String json) throws Exception { CloseableHttpResponse response = null; try { HttpPost httppost = new HttpPost(url); System.out.println("执行请求:" + httppost.getURI()); StringEntity s = new StringEntity(json); s.setContentEncoding(DncConstant.CHARSET); s.setContentType("application/json"); httppost.setEntity(s); response = execute(httppost); String entityUtils = EntityUtils.toString(response.getEntity()); if (response != null) { response.close(); } return entityUtils; } catch (Throwable th) { if (response != null) { response.close(); } throw th; } } public CloseableHttpResponse execute(HttpRequestBase url) { try { HttpClientContext context = HttpClientContext.create(); CloseableHttpResponse response = this.http.execute(url, context); addCookies(url.getURI().getHost(), context.getCookieStore()); return response; } catch (IOException ex) { log.error("执行http请求异常",ex); throw new RuntimeException(ex); } } public static class RangeInfo { String acceptRanges; String etag; String lastModified; Long contentLength; public void setAcceptRanges(final String acceptRanges) { this.acceptRanges = acceptRanges; } public void setEtag(final String etag) { this.etag = etag; } public void setLastModified(final String lastModified) { this.lastModified = lastModified; } public void setContentLength(final Long contentLength) { this.contentLength = contentLength; } public boolean equals(final Object o) { if (o == this) { return true; } if (o instanceof RangeInfo) { RangeInfo other = (RangeInfo) o; if (other.canEqual(this)) { Object this$contentLength = getContentLength(); Object other$contentLength = other.getContentLength(); if (this$contentLength == null) { if (other$contentLength != null) { return false; } } else if (!this$contentLength.equals(other$contentLength)) { return false; } Object this$acceptRanges = getAcceptRanges(); Object other$acceptRanges = other.getAcceptRanges(); if (this$acceptRanges == null) { if (other$acceptRanges != null) { return false; } } else if (!this$acceptRanges.equals(other$acceptRanges)) { return false; } Object this$etag = getEtag(); Object other$etag = other.getEtag(); if (this$etag == null) { if (other$etag != null) { return false; } } else if (!this$etag.equals(other$etag)) { return false; } Object this$lastModified = getLastModified(); Object other$lastModified = other.getLastModified(); return this$lastModified == null ? other$lastModified == null : this$lastModified.equals(other$lastModified); } return false; } return false; } protected boolean canEqual(final Object other) { return other instanceof RangeInfo; } public int hashCode() { Object $contentLength = getContentLength(); int result = (1 * 59) + ($contentLength == null ? 43 : $contentLength.hashCode()); Object $acceptRanges = getAcceptRanges(); int result2 = (result * 59) + ($acceptRanges == null ? 43 : $acceptRanges.hashCode()); Object $etag = getEtag(); int result3 = (result2 * 59) + ($etag == null ? 43 : $etag.hashCode()); Object $lastModified = getLastModified(); return (result3 * 59) + ($lastModified == null ? 43 : $lastModified.hashCode()); } public String toString() { return "SimpleHttpClient.RangeInfo(acceptRanges=" + getAcceptRanges() + ", etag=" + getEtag() + ", lastModified=" + getLastModified() + ", contentLength=" + getContentLength() + ")"; } public RangeInfo(final String acceptRanges, final String etag, final String lastModified, final Long contentLength) { this.acceptRanges = acceptRanges; this.etag = etag; this.lastModified = lastModified; this.contentLength = contentLength; } public String getAcceptRanges() { return this.acceptRanges; } public String getEtag() { return this.etag; } public String getLastModified() { return this.lastModified; } public Long getContentLength() { return this.contentLength; } } }