公司做項目,要調用第三方接口,接口形式為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");
/**
* <p>Title: </p>
* <p>Description: </p>
* @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)
* <p>Title: createSocket</p>
* <p>Description: </p>
* @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)
* <p>Title: createSocket</p>
* <p>Description: </p>
* @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