jdk11自带的HttpClient

jdk11新增了很多api,这里使用一下jdk11提供的HttpClient替代apache-httpClient,使用方式如下:
新建 HttpUtil.java

package com.example.demo;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.net.ProxySelector;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.Charset;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.time.Duration;

/** HttpUtil
 * @author xiaochi
 * @date 2022/5/10 14:24
 */
public class HttpUtil {

    private final Duration timeout = Duration.ofSeconds(5);
    private final byte[] lock = new byte[0];
    private volatile HttpClient httpClient = null;

    private HttpUtil(){
        if (httpClient == null){
            synchronized (lock){
                if (httpClient == null){
                    httpClient = HttpClient.newBuilder()
                            .version(HttpClient.Version.HTTP_2)
                            .connectTimeout(timeout)
                            .followRedirects(HttpClient.Redirect.NEVER)
                            .sslContext(sslContext())
                            .proxy(ProxySelector.getDefault())
                            .build();
                }
            }
        }
    }

    /**
     * 创建 HttpUtil
     * @return
     */
    public static HttpUtil builder(){
        return new HttpUtil();
    }

    /**
     * get请求
     * @param url 地址
     * @return
     */
    public String get(String url){
        String body = "";
        try {
            HttpRequest request = HttpRequest.newBuilder()
                .header("Content-Type", "application/json")
                .version(HttpClient.Version.HTTP_2)
                .uri(URI.create(url))
                .GET()
                .timeout(timeout)
                .build();
            HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
            if (response.statusCode() == 200){
                body = response.body();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return body;
    }

    /**
     * post请求
     * @param url 地址
     * @return
     */
    public String post(String url){
        return post(url,"");
    }

    /**
     * post请求
     * @param url 地址
     * @param data json字符串
     * @return
     */
    public String post(String url,String data){
        String body = "";
        try {
            HttpRequest request = HttpRequest.newBuilder()
                .header("Content-Type", "application/json")
                .version(HttpClient.Version.HTTP_2)
                .uri(URI.create(url))
                .POST(HttpRequest.BodyPublishers.ofString(data, Charset.defaultCharset()))
                .timeout(timeout)
                .build();
            HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
            if (response.statusCode() == 200){
                body = response.body();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return body;
    }

    /**
     * 生成安全套接字工厂,用于https请求的证书跳过
     * @return
     */
    private SSLContext sslContext(){
        TrustManager[] trustManagers = new TrustManager[]{
            new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {}
                @Override
                public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {}
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
            }
        };
        SSLContext sc = null;
        try {
            sc = SSLContext.getInstance("SSL");
            sc.init(null, trustManagers, new SecureRandom());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sc;
    }
}

调用

public static void main(String[] args) throws IOException, InterruptedException {
    System.out.println(HttpUtil.builder().get("http://www.baidu.com"));
}

你可能感兴趣的:(jdk11自带的HttpClient)