JDK11 HttpClient Util

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.util.Map;
 
/**
 * description: 新版http工具 <br>
 * date: 2021/4/8 9:21 <br>
 * author: haichaoyuan1 <br>
 * version: 1.0 <br>
 */
public class HttpClientUtil {
     private static Charset getDefaultCharset() {
        return Charset.forName("utf-8");
    }
 
    /**
     * get请求
     *
     * @param url
     * @param header
     * @return
     */
    public static String doGet(String url, Map<String, String> header) {
        var builder = HttpRequest.newBuilder().uri(URI.create(url)).GET();
        buildHeader(header, builder);
        return execute(builder, getDefaultCharset());
    }
 
    /**
     * post请求
     *
     * @param url
     * @param header
     * @param body
     * @return
     */
    public static String doPost(String url, Map<String, String> header, String body) {
        HttpRequest.BodyPublisher bodyPublisher = HttpRequest.BodyPublishers.ofString(body, getDefaultCharset());
        var builder = HttpRequest.newBuilder().uri(URI.create(url)).POST(bodyPublisher);
        buildHeader(header, builder);
        return execute(builder, getDefaultCharset());
    }
 
    /**
     * form表单
     *
     * @param url
     * @param header
     * @param pd
     * @return
     */
    public static String doPostForm(String url, Map<String, String> header, Map<String, String> pd, Charset charset) {
        if (charset == null) {
            charset = getDefaultCharset();
        }
        StringBuilder sb = new StringBuilder();
        if (pd != null) {
            pd.forEach((k, v) -> {
                sb.append(k);
                sb.append("=");
                sb.append(v);
                sb.append("&");
            });
        }
        sb.append("tmp=tmp");
        HttpRequest.BodyPublisher bodyPublisher = HttpRequest.BodyPublishers.ofString(sb.toString(), charset);
        var builder = HttpRequest.newBuilder().uri(URI.create(url)).POST(bodyPublisher);
        buildHeader(header, builder);
        builder.setHeader("Content-Type", "application/x-www-form-urlencoded");
        return execute(builder, charset);
    }
 
 
    private static void buildHeader(Map<String, String> header, HttpRequest.Builder builder) {
        if (header != null && header.size() > 0) {
            for (String key : header.keySet()) {
                builder.setHeader(key, header.get(key));
            }
        }
        builder.setHeader("Content-Type", "application/json");
    }
 
    private static String execute(HttpRequest.Builder builder, Charset charset) {
        var request = builder.build();
        try {
            var client = HttpClient.newBuilder().build();
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString(charset));
            String result = null;
            if (response.statusCode() == 200) {
                result = response.body();
            } else {
                throw new Exception("异常");
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
 
}

 

posted @ 2022-08-22 15:06  曹丽是我女朋友。  阅读(25)  评论(0编辑  收藏  举报