JAVA实现https单向认证
生活随笔
收集整理的這篇文章主要介紹了
JAVA实现https单向认证
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
//關(guān)于http 須要兩個jar包 httpclient-4.0.jar httpcore-4.0.1.jar
private static final HttpClient httpClient = new DefaultHttpClient();try {//獲得密匙庫KeyStore trustStore = KeyStore.getInstance("jks");String keyStoreFile = "xxxxx.keystore";String keyPwd = "xxxxxxx";FileInputStream instream = new FileInputStream(new File(keyStoreFile));//密匙庫的passwordtrustStore.load(instream, keyPwd.toCharArray());//注冊密匙庫SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);//不校驗域名socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);Scheme sch = new Scheme("https", socketFactory, 443);httpClient.getConnectionManager().getSchemeRegistry().register(sch);} catch (Exception e) {e.printStackTrace();}
//以下這段是調(diào)用代碼。能夠有非常多種寫法。不局限于用HttpPost
HttpPost httpPost = new HttpPost( url );
StringEntity entity = new StringEntity(params);
entity.setContentEncoding("UTF-8");
httpPost.setEntity( entity );
//發(fā)送請求
HttpResponse response = httpClient.execute( httpPost );
String jsonStr = EntityUtils.toString( response.getEntity() );
總結(jié)
以上是生活随笔為你收集整理的JAVA实现https单向认证的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《HTML 5与CSS 3权威指南(第3
- 下一篇: DBI(i80)/DPI(RGB)/DS