0 0

java中的httpclient的SSL出错5

我直接下载最新版的httpclient,直接复制的示例中的代码,只是把网址换成我自己想要的网址.并且,在我电脑中,在办公室服务器里测试都通过,但上传上机房服务器里面就出错了.

java代码

	public void login()
	{
        try
        {
        	//初始化 httpclient
        	DefaultHttpClient httpclient = new DefaultHttpClient();
        	//设置证书
	        httpclient.getConnectionManager().getSchemeRegistry().register(sch);
	        String url = "login.jsp";
	        HttpPost httpost = new HttpPost(url);
	        print("准备登陆网站,网址:"+url);
	        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
	        nvps.add(new BasicNameValuePair("actionName", "login"));
	        nvps.add(new BasicNameValuePair("login_password", "11"));
	        nvps.add(new BasicNameValuePair("login_user", "111"));	      
	        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
	        HttpResponse response = httpclient.execute(httpost);
	        String statusLine = response.getStatusLine().toString();
	        print("得到网页打开状态"+statusLine);
	        if(statusLine.equals("HTTP/1.1 200 OK"))
	        {
                 }  
             }
         }
	/*
	 * 设置SSL证书
	 */
	public void setSSL()
	{
        try
        {
			KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());      
			String p = System.getProperty("user.dir")+"/my.key";
			print(p);
	        FileInputStream instream = new FileInputStream(new File(p)); 
	        try {
	        	trustStore.load(instream, "123456".toCharArray());
	        }catch(Exception e)
	        {
	        	e.printStackTrace();
	        }
	        finally {
	            instream.close();
	        }     	             
	    	SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
	        sch = new Scheme("https", socketFactory, 443);
        }
        catch(Exception e)
        {
        	e.printStackTrace();
        }
	}


程序出错提示
javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
        at sun.security.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:371)
        at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:128)
        at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:399)
        at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
        at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
        at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:108)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:415)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)
        at login(login.java:89)
2011年2月15日 13:40

1个答案 按时间排序 按投票排序

0 0

采纳的答案

referenced this link
http://theskeleton.wordpress.com/2010/07/24/avoiding-the-javax-net-ssl-sslpeerunverifiedexception-peer-not-authenticated-with-httpclient/

The following class takes a HttpClient and returns a new HttpClient that accepts any SSL certificate:
01 public class WebClientDevWrapper {
02
03     public static HttpClient wrapClient(HttpClient base) {
04         try {
05             SSLContext ctx = SSLContext.getInstance("TLS");
06             X509TrustManager tm = new X509TrustManager() {
07
08                 public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
09                 }
10
11                 public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
12                 }
13
14                 public X509Certificate[] getAcceptedIssuers() {
15                     return null;
16                 }
17             };
18             ctx.init(null, new TrustManager[]{tm}, null);
19             SSLSocketFactory ssf = new SSLSocketFactory(ctx);
20             ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
21             ClientConnectionManager ccm = base.getConnectionManager();
22             SchemeRegistry sr = ccm.getSchemeRegistry();
23             sr.register(new Scheme("https", ssf, 443));
24             return new DefaultHttpClient(ccm, base.getParams());
25         } catch (Exception ex) {
26             ex.printStackTrace();
27             return null;
28         }
29     }
30 }

You can then do something like this in the code that creates the HttpClient:
1 this.client = new DefaultHttpClient();
2 if(dev) {
3     this.client = WebClientDevWrapper.wrapClient(client);
4 }


referenced this link
http://theskeleton.wordpress.com/2010/07/24/avoiding-the-javax-net-ssl-sslpeerunverifiedexception-peer-not-authenticated-with-httpclient/

2011年2月15日 17:45

相关推荐

Global site tag (gtag.js) - Google Analytics