2012-01-12 132 views
2

如何解决“SSLHANDSHAKE异常”以及如何在httpClient post方法中维护“Cookies”。我将一些会话参数“nameValuePair”和cookie传递给http客户端的post方法。 在“httpClient.executeMethod(postMethod)”中发布所有参数后,它不会为下一个“postMethod.getResponseBodyAsString();”之后显示出一些例外。如何解决SSLHandshake异常?

try { 
      initialState = new HttpState(); 
      httpClient = new HttpClient(); 
      httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(25000); 
      httpClient.getParams().setParameter("http.socket.timeout", new Integer(25000)); 
      httpClient.getHostConfiguration().setProxy("127.0.0.1",8888); 
     // Properties systemProperties = System.getProperties(); 
     // systemProperties.put("proxySet", "true"); 
     // systemProperties.put("http.proxyHost", "127.0.0.1"); 
     // systemProperties.put("http.proxyPort", 8888); 
      try { 
       httpClient.getParams().setAuthenticationPreemptive(true); 
       // httpClient.getState().setProxyCredentials(null, null, new UsernamePasswordCredentials(null, null)); 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } 
      //httpClient.getParams().setCookiePolicy(CookiePolicy.ACCEPT_ALL); 

      cookies = httpClient.getState().getCookies(); 
     } catch (Exception ex) { 
     } 
     try { 
      postMethod = new PostMethod(pageUrl); 
      postMethod.addParameter("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3"); 
      postMethod.addRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 
      postMethod.addRequestHeader("Accept-Language", "en-us,en;q=0.5"); 
      postMethod.addRequestHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); 
      postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
      postMethod.addRequestHeader("KeepAlive", "true"); 
      postMethod.setFollowRedirects(FollowRedirect); 
      postMethod.setDoAuthentication(DoAuthentication); 

      if (referer != null) { 
       postMethod.addRequestHeader("referer", referer); 
      } 
      if (cookieArr != null) { 
       cookies = new Cookie[cookieArr.length]; 
       for (int i = 0; i < cookieArr.length; i++) { 
        cookies[i] = new Cookie(".linkedin.com", cookieArr[i][0], cookieArr[i][0], "/", null, true); 
       } 
      } 
      initialState.addCookies(cookies); 
      int postDataLength = postData.length; 
      NameValuePair[] nameValuePair = new NameValuePair[postDataLength]; 
      for (int i = 0; i < postDataLength; i++) { 
       nameValuePair[i] = new NameValuePair(postData[i][0], postData[i][1]); 
      } 

      postMethod.addParameters(nameValuePair); 
      httpClient.executeMethod(postMethod); 
      cookies = httpClient.getState().getCookies(); 
      pageSouce = postMethod.getResponseBodyAsString(); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 

     } finally { 
      postMethod.releaseConnection(); 
     } 

但它显示了以下异常运行时:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: 
sun.security.provider.certpath.SunCertPathBuilderException: unable to 
find valid certification path to requested target 

sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: 
unable to find valid certification path to requested target 

sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 

回答