OkHttp3 的基本使用

依赖

<dependencies>
	<!-- Jackson核心 -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.13.1</version>
    </dependency>
 
    <!-- Jackson数据绑定 -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.13.1</version>
    </dependency>
    <!-- OKHttp -->
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>3.14.9</version>
    </dependency>
    <!-- OKHttp 所依赖的io包,指定高版本效率会好些 -->
    <dependency>
        <groupId>com.squareup.okio</groupId>
        <artifactId>okio</artifactId>
        <version>2.6.0</version>
    </dependency>
    <!-- jsoup,解析网页dom结构 -->
    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.13.1</version>
    </dependency>
    <!-- commons-io,用来将流保存为文件 -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.8.0</version>
    </dependency>
</dependencies>

工具封装

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.File;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;

public class HttpUtil {
	static class SslIgnore {

        private static final TrustManager[] trustAllCerts = new TrustManager[]{
                new X509TrustManager() {
                    @Override
                    public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
                    }

                    @Override
                    public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
                    }

                    @Override
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        return new java.security.cert.X509Certificate[]{};
                    }
                }
        };

        private static SSLContext sslContext;

        static {
            try {
                sslContext = SSLContext.getInstance("TLS");
                sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
            } catch (NoSuchAlgorithmException | KeyManagementException e) {
                e.printStackTrace();
            }
        }

        public static SSLSocketFactory getSSLSocketFactory() {
            return sslContext.getSocketFactory();
        }

        public static X509TrustManager getX509TrustManager() {
            return (X509TrustManager) trustAllCerts[0];
        }

    }

    public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36";

    public static final String ACCEPT = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7";

    private static final ObjectMapper objectMapper = new ObjectMapper();

    private HttpUtil() {}

    /**
     * 创建okHttp客户端
     */
    private static OkHttpClient client = new OkHttpClient.Builder()
//            .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10809)))
            .connectTimeout(15, TimeUnit.SECONDS)
            .readTimeout(30,TimeUnit.SECONDS)
            .followRedirects(true)
            .hostnameVerifier((s, sslSession) -> true)
            .sslSocketFactory(SslIgnore.getSSLSocketFactory(),SslIgnore.getX509TrustManager())
            .build();

    public static String getString(String url) throws IOException {
        return requestString(url,null,"GET",null);
    }

    public static String requestString(String url, Headers headers, String method, RequestBody requestBody) throws IOException {
        return requestResponse(url,headers,method,requestBody).body().string();
    }

    public static Response requestResponse(String url, Headers headers, String method, RequestBody requestBody) throws IOException {
        Request.Builder requestBuilder = new Request.Builder().url(url);
        if (headers != null) {
            requestBuilder = requestBuilder.headers(headers);
            if (headers.get("User-Agent") == null) {
                requestBuilder = requestBuilder.addHeader("User-Agent",USER_AGENT);
            }
            if (headers.get("Accept") == null) {
                requestBuilder = requestBuilder.addHeader("Accept",ACCEPT);
            }
        }else {
            requestBuilder = requestBuilder.headers(Headers.of("User-Agent",USER_AGENT,"Accept",ACCEPT));
        }
        if (method.equalsIgnoreCase("GET")) {
            requestBuilder = requestBuilder.get();
        }else {
            requestBody = requestBody != null ? requestBody : RequestBody.create(null,"");
            requestBuilder = requestBuilder.method(method,requestBody);
        }
        Request request = requestBuilder.build();
        return client.newCall(request).execute();
    }

    public static InputStream getInputStream(String url) throws IOException {
        return requestResponse(url,null,"GET",null).body().byteStream();
    }

    public static InputStream getInputStream(String url,Headers headers) throws IOException {
        return requestResponse(url,headers,"GET",null).body().byteStream();
    }

    public static InputStream requestInputStream(String url,Headers headers,String method,RequestBody requestBody) throws IOException {
        return requestResponse(url,headers,method,requestBody).body().byteStream();
    }

    public static String mapToQuery(Map<String,String> map) {
        List<String> list = map.entrySet().stream().map(x->x.getKey()+"="+x.getValue()).collect(Collectors.toList());
        return String.join("&",list);
    }

    public static JsonNode getJson(String url) throws IOException {
        return objectMapper.readTree(requestInputStream(url,null,"GET",null));
    }

    public static JsonNode getJson(String url,Headers headers) throws IOException {
        return objectMapper.readTree(requestInputStream(url,headers,"GET",null));
    }

    public static JsonNode postJson(String url,String body,String contentType) throws IOException {
        RequestBody requestBody = RequestBody.create(MediaType.parse(contentType),body);
        return objectMapper.readTree(requestInputStream(url,null,"POST",requestBody));
    }

    public static JsonNode postJson(String url,RequestBody body) throws IOException {
        return objectMapper.readTree(requestInputStream(url,null,"POST",body));
    }

    public static JsonNode postJson(String url,Headers headers,RequestBody body) throws IOException {
        return objectMapper.readTree(requestInputStream(url,headers,"POST",body));
    }

    public static JsonNode postJson(String url,Headers headers,String body,String contentType) throws IOException {
        RequestBody requestBody = RequestBody.create(MediaType.parse(contentType),body);
        return objectMapper.readTree(requestInputStream(url,headers,"POST",requestBody));
    }

    public static Document getDoc(String url, Headers headers) throws IOException {
        return Jsoup.parse(requestString(url,headers,"GET",null),url);
    }

    public static Document getDoc(String url) throws IOException {
        return getDoc(url,"UTF-8");
    }

    public static Document getDoc(String url,String charsetName) throws IOException {
        return Jsoup.parse(requestInputStream(url,null,"GET",null),charsetName,url);
    }

    /**
     * 下载网络文件
     * @param url
     * @param file
     * @throws Exception
     */
    public static void download(String url,File file) throws Exception {
        try(InputStream inputStream = getInputStream(url)) {
            FileUtils.copyInputStreamToFile(inputStream,file);
        }catch (Exception e){
            throw e;
        }
    }
}
posted @ 2022-02-24 16:34  小小爬虫  阅读(635)  评论(0编辑  收藏  举报