forked from zengge96/CatVodSpiderNew
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
105 additions
and
0 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
app/src/main/java/com/github/catvod/spider/OkHttpWraper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package com.github.catvod.net; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import okhttp3.Dns; | ||
import okhttp3.Headers; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
public class OkHttpWraper { | ||
|
||
public static final String POST = "POST"; | ||
public static final String GET = "GET"; | ||
|
||
private OkHttpClient client; | ||
|
||
private static class Loader { | ||
static volatile OkHttpWraper INSTANCE = new OkHttp(); | ||
} | ||
|
||
private static OkHttpWraper get() { | ||
return Loader.INSTANCE; | ||
} | ||
|
||
public static Response newCall(Request request) throws IOException { | ||
return client().newCall(request).execute(); | ||
} | ||
|
||
public static Response newCall(String url) throws IOException { | ||
return client().newCall(new Request.Builder().url(url).build()).execute(); | ||
} | ||
|
||
public static Response newCall(String url, Map<String, String> header) throws IOException { | ||
return client().newCall(new Request.Builder().url(url).headers(Headers.of(header)).build()).execute(); | ||
} | ||
|
||
public static String string(String url) { | ||
return string(url, null); | ||
} | ||
|
||
public static String string(String url, Map<String, String> header) { | ||
return string(url, null, header); | ||
} | ||
|
||
public static String string(String url, Map<String, String> params, Map<String, String> header) { | ||
return url.startsWith("http") ? new OkRequest(GET, url, params, header).execute(client()).getBody() : ""; | ||
} | ||
|
||
public static String post(String url, Map<String, String> params) { | ||
return post(url, params, null).getBody(); | ||
} | ||
|
||
public static OkResult post(String url, Map<String, String> params, Map<String, String> header) { | ||
return new OkRequest(POST, url, params, header).execute(client()); | ||
} | ||
|
||
public static String post(String url, String json) { | ||
return post(url, json, null).getBody(); | ||
} | ||
|
||
public static OkResult post(String url, String json, Map<String, String> header) { | ||
return new OkRequest(POST, url, json, header).execute(client()); | ||
} | ||
|
||
public static String getLocation(String url, Map<String, String> header) throws IOException { | ||
return getLocation(client().newBuilder().followRedirects(false).followSslRedirects(false).build().newCall(new Request.Builder().url(url).headers(Headers.of(header)).build()).execute().headers().toMultimap()); | ||
} | ||
|
||
public static String getLocation(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; | ||
} | ||
|
||
private static OkHttpClient build() { | ||
if (get().client != null) return get().client; | ||
return get().client = getBuilder().build(); | ||
} | ||
|
||
private static OkHttpClient.Builder getBuilder() { | ||
return new OkHttpClient.Builder().dns(safeDns()).connectTimeout(30, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS).hostnameVerifier((hostname, session) -> true).sslSocketFactory(new SSLCompat(), SSLCompat.TM); | ||
} | ||
|
||
public static OkHttpClient client() { | ||
try { | ||
return Objects.requireNonNull(Spider.client()); | ||
} catch (Throwable e) { | ||
return build(); | ||
} | ||
} | ||
|
||
private static Dns safeDns() { | ||
try { | ||
return Objects.requireNonNull(Spider.safeDns()); | ||
} catch (Throwable e) { | ||
return Dns.SYSTEM; | ||
} | ||
} | ||
} |