静态化一个okhttp实例 @author zsq @version 1.0 @date 2021/3/31 10:58
| 13 | * @date 2021/3/31 10:58 |
| 14 | */ |
| 15 | public class HttpClient { |
| 16 | |
| 17 | /**okhttp实例**/ |
| 18 | public static Builder client; |
| 19 | |
| 20 | private static String host = "cat-match.easygame2021.com"; |
| 21 | private static String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.33"; |
| 22 | private static String xRequestedWith = "XMLHttpRequest"; |
| 23 | |
| 24 | static { |
| 25 | //初始化一个okhttp实例 |
| 26 | client = new OkHttpClient.Builder() |
| 27 | .connectTimeout(15, TimeUnit.SECONDS) |
| 28 | .connectionPool(new ConnectionPool(5, 20, TimeUnit.SECONDS)) |
| 29 | .readTimeout(15, TimeUnit.SECONDS) |
| 30 | //添加请求头 |
| 31 | .addInterceptor(new Interceptor() { |
| 32 | @Override |
| 33 | public Response intercept(Chain chain) throws IOException { |
| 34 | //添加请求头 |
| 35 | Request request = chain.request().newBuilder() |
| 36 | .addHeader("Host", host) |
| 37 | .addHeader("User-Agent", userAgent) |
| 38 | .addHeader("xRequestedWith", xRequestedWith) |
| 39 | .build(); |
| 40 | |
| 41 | return chain.proceed(request); |
| 42 | } |
| 43 | }); |
| 44 | } |
| 45 | |
| 46 | } |
nothing calls this directly
no outgoing calls
no test coverage detected