米德

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

之前业务调接口都是HTTP的URL

这次碰到接口地址是HTTPS

以前用的HTTPClient就不能使用了

会报一个证书错误

找了个绕开证书的方法 可以调通了

记录一下

 1 //https必要复写类,通过这个类绕开证书
 2 public class SSLSocketFactoryEx extends SSLSocketFactory {
 3 
 4     SSLContext sslContext = SSLContext.getInstance("TLS");
 5 
 6     public SSLSocketFactoryEx(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException,
 7             UnrecoverableKeyException {
 8         super(truststore);
 9 
10         TrustManager tm = new X509TrustManager() {
11 
12             public java.security.cert.X509Certificate[] getAcceptedIssuers() {
13                 return null;
14             }
15 
16             public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
17                 // TODO Auto-generated method stub
18                 
19             }
20 
21             public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
22                 // TODO Auto-generated method stub
23                 
24             }
25 
26             
27         };
28 
29         sslContext.init(null, new TrustManager[] { tm }, null);
30     }
31 
32     @Override
33     public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
34         return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
35     }
36 
37     @Override
38     public Socket createSocket() throws IOException {
39         return sslContext.getSocketFactory().createSocket();
40     }
41 }
 1 //获取HttpClient
 2 public static DefaultHttpClient getNewHttpsClient() {
 3             try {
 4                 KeyStore trustStore = KeyStore.getInstance(KeyStore
 5                         .getDefaultType());
 6                 trustStore.load(null, null);
 7 
 8                 SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore);
 9                 sf
10                         .setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
11 
12                 HttpParams params = new BasicHttpParams();
13                 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
14                 HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
15                 HttpConnectionParams.setConnectionTimeout(params, 50000);
16                 HttpConnectionParams.setSoTimeout(params, 50000);
17                 SchemeRegistry registry = new SchemeRegistry();
18                 registry.register(new Scheme("http", PlainSocketFactory
19                         .getSocketFactory(), 80));
20                 registry.register(new Scheme("https", sf, 443));
21 
22                 ClientConnectionManager ccm = new ThreadSafeClientConnManager(
23                         params, registry);
24 
25                 return new DefaultHttpClient(ccm, params);
26             } catch (Exception e) {
27                 return new DefaultHttpClient();
28             }
29         }
 1        //调用接口 
 2             HttpClient httpclient = HttpClientUtil.getNewHttpsClient();
 3             HttpPost requestHttps = new HttpPost("https://192.168.1.1:8080/test");
 4             StringEntity param = new StringEntity(setJson.toString(), "UTF-8");
 5             requestHttps.addHeader("content-type", "application/json");
 6             requestHttps.setEntity(param);
 7             HttpResponse responseHttps = httpclient.execute(requestHttps);
 8             InputStream is = responseHttps.getEntity().getContent();
 9             BufferedReader in = new BufferedReader(new InputStreamReader(is,"UTF-8"));
10             StringBuffer sb = new StringBuffer();
11             String data = null;
12             while ((data = in.readLine()) != null) {
13                 sb.append(data);
14             }
15             System.out.println(sb.toString() + "---**");
16             if (in != null)
17                 in.close();

 

posted on 2018-04-18 15:46  米德  阅读(1578)  评论(0编辑  收藏  举报