码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA实现https单向认证

时间:2014-09-04 09:45:28      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:http   os   io   java   ar   cti   代码   sp   on   

//关于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));
	        //密匙库的密码
	        trustStore.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();
		}
//下面这段是调用代码,可以有很多种写法,不局限于用HttpPost
HttpPost httpPost = new HttpPost( url );
StringEntity entity = new StringEntity(params);
entity.setContentEncoding("UTF-8");
httpPost.setEntity( entity );
//发送请求
HttpResponse response = httpClient.execute( httpPost );
String jsonStr = EntityUtils.toString( response.getEntity() );



JAVA实现https单向认证

标签:http   os   io   java   ar   cti   代码   sp   on   

原文地址:http://blog.csdn.net/qyf_5445/article/details/39047791

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!