?在項(xiàng)目開發(fā)過程中,我想大部分系統(tǒng)都需要對(duì)接另外的系統(tǒng)粥帚。對(duì)接方式有很多種芒涡,現(xiàn)在最常見的就是https請(qǐng)求了。現(xiàn)將Java發(fā)送https請(qǐng)求的工具類整理一下费尽,有需要用到的同學(xué)拿走不謝。
? 該方法使用apache的httpclient實(shí)現(xiàn)
第一步:創(chuàng)建SSLClient
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;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
public class SSLClient extends DefaultHttpClient{
public SSLClient() throws Exception{
super();
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
}
}
第二步:實(shí)現(xiàn)自己的https工具類
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.util.Map;
public class HttpsClientUtil {
private static final String CHAREST = "utf-8";
/**
* 發(fā)送post請(qǐng)求
* @param url
* @return
*/
public static String doPost(String url,String mapParam){
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = new SSLClient();
httpPost = new HttpPost(url);
//設(shè)置參數(shù)
httpPost.setHeader("Content-Type", "application/json");
httpPost.addHeader("Authorization", "Basic YWRtaW46");
StringEntity s = new StringEntity(mapParam, CHAREST);
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
httpPost.setEntity(s);
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,CHAREST);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
/**
* 發(fā)送get請(qǐng)求
* @param url? ? ? 鏈接地址
* @return
*/
public static String doGet(String url){
HttpClient httpClient = null;
HttpGet httpGet= null;
String result = null;
try {
httpClient = new SSLClient();
httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,CHAREST);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 發(fā)送get請(qǐng)求,并設(shè)置get的請(qǐng)求頭
* @param url? ? ? 鏈接地址
* @return
*/
public static String setHeadDoGet(String url,Map headers){
HttpClient httpClient = null;
HttpGet httpGet= null;
String result = null;
try {
httpClient = new SSLClient();
httpGet = new HttpGet(url);
for (Map.Entry e : headers.entrySet()) {
httpGet.addHeader(e.getKey(), e.getValue());
}
HttpResponse response = httpClient.execute(httpGet);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,CHAREST);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
ps:有需要設(shè)置post請(qǐng)求頭的請(qǐng)求冬三,同學(xué)們可以參考setHeadDoGet方法進(jìn)行編寫。需要用到的apache的httpclient-4.5.9.jar包长豁。