import java.io.IOException;
import java.net.URLDecoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
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 com.alibaba.fastjson.JSONObject;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
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;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
public class HttpsClient {
private static X509TrustManager tm =new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs, String string)throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs, String string)throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
@SuppressWarnings("deprecation")
public static HttpClient getInstance()throws KeyManagementException,NoSuchAlgorithmException {
HttpClient client =new DefaultHttpClient();
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null,new TrustManager[] {tm },null);
SSLSocketFactory ssf =new SSLSocketFactory(ctx);
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = client.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", ssf,443));
client =new DefaultHttpClient(ccm, client.getParams());
return client;
}
public static void main(String[] args) {
String url = "https://cmdb-dev.ac.com/third/cmdb/api/resource/ControlM/list?pageSize=10&page=1&resource_name=test";
JSONObject jsonResult =null;
// CloseableHttpClient httpClient = HttpClients.createDefault();
// 在这里获取HttpsClient的一个实例作为httpclient的一个对象
try {
HttpClient httpsClient = null;
httpsClient = HttpsClient.getInstance();
Header[] headers = {
new BasicHeader("Accept","application/json;charset=utf-8")
,new BasicHeader("Content-Type","application/json;charset=utf-8")
,new BasicHeader("Authorization", "6125b004e4b04176b0a9eba7")};
HttpGet request =new HttpGet(url);
request.setHeaders(headers);
HttpResponse response = httpsClient.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String strResult = EntityUtils.toString(response.getEntity());
jsonResult = JSONObject.parseObject(strResult);
url = URLDecoder.decode(url,"UTF-8");
} else {
System.out.println("通信异常:" + url);
}
} catch (IOException e) {
System.out.println("通信异常:" + url+e);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
System.out.println("++++++++++++++++++++++:" + jsonResult);
}
}