yangys
2024-10-29 d728f14a2f23cb477ebfecd33df5f7e5cb54a178
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
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<Header> 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<Header> 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<Header> 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;
        }
    }
}