springboot发送https请求

公司做项目,要调用第三方接口,接口形式为https (Basic Auth)验证 post body体:xml格式
话不多少,直接列方法

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
 * @author 
 * @data 2019/4/14 0014 -下午 2:54
 */
public class SSLSocketFactory extends org.apache.http.conn.ssl.SSLSocketFactory {

    /**
     * @Fields sslContext
     * @Description: Field Description
     */
    SSLContext sslContext = SSLContext.getInstance("TLS");

    /**
     * 

Title:

*

Description:

* @param truststore * @throws NoSuchAlgorithmException * @throws KeyManagementException * @throws KeyStoreException * @throws UnrecoverableKeyException */ public SSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException { super(truststore); TrustManager tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return null; } }; sslContext.init(null, new TrustManager[] { tm }, null); } /* (non-Javadoc) *

Title: createSocket

*

Description:

* @param socket * @param host * @param port * @param autoClose * @return * @throws IOException * @throws UnknownHostException * @see org.apache.http.conn.ssl.SSLSocketFactory#createSocket(java.net.Socket, java.lang.String, int, boolean) */ public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException { return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose); } /* (non-Javadoc) *

Title: createSocket

*

Description:

* @return * @throws IOException * @see org.apache.http.conn.ssl.SSLSocketFactory#createSocket() */ public Socket createSocket() throws IOException { return sslContext.getSocketFactory().createSocket(); } }

此方法没有特殊性,复制粘贴及用

import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.security.KeyStore;

/**
 * @author 
 * @data 2019/4/14 0014 -下午 3:51
 */
public class HttpClientUtils {

    /**
     * @Title: getNewHttpClient
     * @Description: Methods Description
     * @param @return
     * @return HttpClient
     * @throws
     */

    private static HttpClient getNewHttpClient() {
        try {
            KeyStore trustStore = KeyStore.getInstance(KeyStore
                    .getDefaultType());
            trustStore.load(null, null);
            SSLSocketFactory sf = new SSLSocketFactory(trustStore);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory
                    .getSocketFactory(), 80));
            registry.register(new Scheme("https", sf, 443));
            ClientConnectionManager ccm = new ThreadSafeClientConnManager(
                    params, registry);
            return new DefaultHttpClient(ccm, params);
        } catch (Exception e) {
            return new DefaultHttpClient();
        }
    }

    // TODO 此处验证暂时写死后期可能会有所改动
    public static String runStart(String url,String xml) throws IOException {
        HttpClient httpclient = getNewHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type","text/xml");
        httpPost.addHeader("Authorization","Basic T2JpeFVzZXI6T2JpeDEyMzQ1Ng==");
        httpPost.setEntity(new StringEntity(xml));
        HttpResponse response = httpclient.execute(httpPost);
        String retSrc = EntityUtils.toString(response.getEntity());
        httpPost.releaseConnection();
        return retSrc;
    }

}

代码说明下,两个头都是通过postman自动生成(此处无广告嫌疑) 用的时候只需调用runStart方法,新手一定要耐下心来看runStart方法,就会找到自己适用的地方,稍微改动就ok

你可能感兴趣的:(springboot发送https请求)