forked from CatVodTVOfficial/CatVodTVSpider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OkHttpUtil.java
200 lines (168 loc) · 7.29 KB
/
OkHttpUtil.java
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
package com.github.catvod.utils.okhttp;
import com.github.catvod.crawler.SpiderDebug;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Response;
public class OkHttpUtil {
public static final String METHOD_GET = "GET";
public static final String METHOD_POST = "POST";
private static final int DEFAULT_TIMEOUT = 15;
private static final Object lockO = new Object();
private static OkHttpClient defaultClient = null;
/**
* 不自动重定向
*/
private static OkHttpClient noRedirectClient = null;
public static OkHttpClient defaultClient() {
synchronized (lockO) {
if (defaultClient == null) {
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.sslSocketFactory(new SSLSocketFactoryCompat(SSLSocketFactoryCompat.trustAllCert), SSLSocketFactoryCompat.trustAllCert);
defaultClient = builder.build();
}
return defaultClient;
}
}
public static OkHttpClient noRedirectClient() {
synchronized (lockO) {
if (noRedirectClient == null) {
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.followRedirects(false)
.followSslRedirects(false)
.retryOnConnectionFailure(true)
.sslSocketFactory(new SSLSocketFactoryCompat(SSLSocketFactoryCompat.trustAllCert), SSLSocketFactoryCompat.trustAllCert);
noRedirectClient = builder.build();
}
return noRedirectClient;
}
}
public static String string(OkHttpClient client, String url, String tag, Map<String, String> paramsMap, Map<String, String> headerMap, Map<String, List<String>> respHeaderMap) {
OKCallBack<String> stringCallback = new OKCallBack<String>() {
@Override
public String onParseResponse(Call call, Response response) {
try {
if (respHeaderMap != null) {
respHeaderMap.clear();
respHeaderMap.putAll(response.headers().toMultimap());
}
return response.body().string();
} catch (IOException e) {
return "";
}
}
@Override
public void onFailure(Call call, Exception e) {
setResult("");
SpiderDebug.log(e);
}
@Override
public void onResponse(String response) {
}
};
OKRequest req = new OKRequest(METHOD_GET, url, paramsMap, headerMap, stringCallback);
req.setTag(tag);
req.execute(client);
return stringCallback.getResult();
}
public static String stringNoRedirect(String url, Map<String, String> headerMap, Map<String, List<String>> respHeaderMap) {
return string(noRedirectClient(), url, null, null, headerMap, respHeaderMap);
}
public static String string(String url, Map<String, String> headerMap, Map<String, List<String>> respHeaderMap) {
return string(defaultClient(), url, null, null, headerMap, respHeaderMap);
}
public static String string(String url, Map<String, String> headerMap) {
return string(defaultClient(), url, null, null, headerMap, null);
}
public static String string(String url, String tag, Map<String, String> headerMap) {
return string(defaultClient(), url, tag, null, headerMap, null);
}
public static void get(OkHttpClient client, String url, OKCallBack callBack) {
get(client, url, null, null, callBack);
}
public static void get(OkHttpClient client, String url, Map<String, String> paramsMap, OKCallBack callBack) {
get(client, url, paramsMap, null, callBack);
}
public static void get(OkHttpClient client, String url, Map<String, String> paramsMap, Map<String, String> headerMap, OKCallBack callBack) {
new OKRequest(METHOD_GET, url, paramsMap, headerMap, callBack).execute(client);
}
public static void post(OkHttpClient client, String url, OKCallBack callBack) {
post(client, url, null, callBack);
}
public static void post(OkHttpClient client, String url, Map<String, String> paramsMap, OKCallBack callBack) {
post(client, url, paramsMap, null, callBack);
}
public static void post(OkHttpClient client, String url, Map<String, String> paramsMap, Map<String, String> headerMap, OKCallBack callBack) {
new OKRequest(METHOD_POST, url, paramsMap, headerMap, callBack).execute(client);
}
public static void post(OkHttpClient client, String url, String tag, Map<String, String> paramsMap, Map<String, String> headerMap, OKCallBack callBack) {
OKRequest req = new OKRequest(METHOD_POST, url, paramsMap, headerMap, callBack);
req.setTag(tag);
req.execute(client);
}
public static void postJson(OkHttpClient client, String url, String jsonStr, OKCallBack callBack) {
postJson(client, url, jsonStr, null, callBack);
}
public static void postJson(OkHttpClient client, String url, String jsonStr, Map<String, String> headerMap, OKCallBack callBack) {
new OKRequest(METHOD_POST, url, jsonStr, headerMap, callBack).execute(client);
}
/**
* 根据Tag取消请求
*/
public static void cancel(OkHttpClient client, Object tag) {
if (client == null || tag == null) return;
for (Call call : client.dispatcher().queuedCalls()) {
if (tag.equals(call.request().tag())) {
call.cancel();
}
}
for (Call call : client.dispatcher().runningCalls()) {
if (tag.equals(call.request().tag())) {
call.cancel();
}
}
}
public static void cancel(Object tag) {
cancel(defaultClient(), tag);
}
public static void cancelAll() {
cancelAll(defaultClient());
}
/**
* 取消所有请求请求
*/
public static void cancelAll(OkHttpClient client) {
if (client == null) return;
for (Call call : client.dispatcher().queuedCalls()) {
call.cancel();
}
for (Call call : client.dispatcher().runningCalls()) {
call.cancel();
}
}
/**
* 获取重定向地址
*
* @param headers
* @return
*/
public static String getRedirectLocation(Map<String, List<String>> headers) {
if (headers == null)
return null;
if (headers.containsKey("location"))
return headers.get("location").get(0);
if (headers.containsKey("Location"))
return headers.get("Location").get(0);
return null;
}
}