• 大小: 14KB
    文件类型: .java
    金币: 1
    下载: 0 次
    发布日期: 2021-05-10
  • 语言: Java
  • 标签: HttpClient  

资源简介

使用HttpClient4.5实现https请求忽略SSL证书验证工具类

资源截图

代码片段和文件信息

package com.space.utils.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONobject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * http请求工具类
 *
 * @author admin
 */
public class HttpUtils {

    /**
     * get
     *
     * @param host
     * @param path
     * @param headers
     * @param queryMap
     * @return
     * @throws Exception
     */
    public static HttpResponse doGet(String host String path
                                     Map headers
                                     Map queryMap)
            throws Exception {
        HttpClient httpClient = wrapClient(host path);
        HttpGet request = new HttpGet(buildUrl(host path queryMap));
        for (Map.Entry e : headers.entrySet()) {
            request.addHeader(e.getKey() e.getValue());
        }
        request.setConfig(setTimeOutConfig(request.getConfig()));
        return httpClient.execute(request);
    }

    /**
     * post form
     *
     * @param host
     * @param path
     * @param headers
     * @param queryMap
     * @param bodyMap
     * @return
     * @throws Exception
     */
    public static HttpResponse doPost(String host String path
                                      Ma

评论

共有 条评论