当前位置:   article > 正文

java实现Https 请求

java实现Https 请求

这里写了一个工具类  我 没有用证书  而是用忽略证书得方法。  里面包含get 与post请求

package com.hgzh.ulanqab.visualization.utils;

import org.apache.commons.codec.CharEncoding;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
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.message.BasicHeader;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * HttpClientUtil
 * 增加方法post 文本
 */

public class HttpClientUtil {


    public static final Charset UTF_8 = Charset.forName(CharEncoding.UTF_8);
    /**
     * 连接超时时间
     * 20s
     */
    private static final int CONNECT_TIMEOUT = 20000;

    /**
     * 请求超时时间
     * <p>
     * <p>
     * 20s
     */
    private static final int CONNECT_REQUEST_TIMEOUT = 20000;

    /**
     * socket超时时间
     * 20s
     */
    private static final int SOCKET_TIMEOUT = 20000;

    /**
     * 连接超时时间
     * 20s
     */
    private int connectTimeout = CONNECT_TIMEOUT;

    /**
     * 请求超时时间
     * 20s
     */
    private int connectionRequestTimeout = CONNECT_REQUEST_TIMEOUT;

    /**
     * socket超时时间
     * 20s
     */
    private int socketTimeout = SOCKET_TIMEOUT;

    private List<Header> headers;

    private Integer responseCode = -1;

    private static volatile HttpClientUtil instance = null;

    protected static Random random = new Random();

    public static HttpClientUtil getInstance() {
        if (instance == null) {
            synchronized (HttpClientUtil.class) {
                if (instance == null) {
                    instance = new HttpClientUtil();
                }
            }
        }
        return instance;
    }

    /**
     * 关闭ssl验证
     * @return
     */
    private CloseableHttpClient getHttpClientNoSsl() {
        try {
            HttpClientBuilder b = HttpClientBuilder.create();
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                    return true;
                }
            }).build();
            SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnect
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/256940
推荐阅读
相关标签
  

闽ICP备14008679号