2013-04-04 103 views
5

我已成功地在http中使用Android调用了休息服务。如何使用httpclient从android调用https安全休息服务

但是现在我想在android 中调用HTTPS宁静服务我正在使用apache的HTTPmime API来调用rest服务。

请任何人能帮助我打电话给在android系统

+0

你有没有遇到任何问题我想你不应该,如果是的话,那么是什么? – Prateek 2013-04-04 07:33:27

+0

你有什么尝试? – 2013-04-04 07:34:56

+0

我已经试过此链接 http://stackoverflow.com/questions/2864016/httpclient-ssl-certificate-on-android 但我不知道如何使用这个SSL与HttpClient的打电话给我服务 – 2013-04-04 07:38:29

回答

0

这里安全的休息服务的示例代码,你可以用忽略SSL证书错误使名为SimpleSSLSocketFactory

public class SimpleSSLSocketFactory extends org.apache.http.conn.ssl.SSLSocketFactory { 
    private SSLSocketFactory sslFactory = HttpsURLConnection.getDefaultSSLSocketFactory(); 

    public SimpleSSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, 
      UnrecoverableKeyException { 
     super(null); 

     try { 
      SSLContext context = SSLContext.getInstance("TLS"); 

      // Create a trust manager that does not validate certificate chains and simply 
      // accept all type of certificates 
      TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { 
       public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
        return new java.security.cert.X509Certificate[] {}; 
       } 

       public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
       } 

       public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
       } 
      } }; 

      // Initialize the socket factory 
      context.init(null, trustAllCerts, new SecureRandom()); 
      sslFactory = context.getSocketFactory(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException { 
     return sslFactory.createSocket(socket, host, port, autoClose); 
    } 

    @Override 
    public Socket createSocket() throws IOException { 
     return sslFactory.createSocket(); 
    } 
} 

,并使用新的Java类继httpclient而不是默认客户端

// Setup a custom SSL Factory object which simply ignore the certificates 
    // validation and accept all type of self signed certificates 
    SSLSocketFactory sslFactory = new SimpleSSLSocketFactory(null); 
    sslFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 

    // Enable HTTP parameters 
    HttpParams paramsSecure = new BasicHttpParams(); 
    HttpProtocolParams.setVersion(paramsSecure, HttpVersion.HTTP_1_1); 
    HttpProtocolParams.setContentCharset(paramsSecure, HTTP.UTF_8); 

    // Register the HTTP and HTTPS Protocols. For HTTPS, register our custom SSL Factory object. 
    SchemeRegistry registry = new SchemeRegistry(); 
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
    registry.register(new Scheme("https", sslFactory, 443)); 

    // Create a new connection manager using the newly created registry and then create a new HTTP client 
    // using this connection manager 
    ClientConnectionManager ccm = new ThreadSafeClientConnManager(paramsSecure, registry); 
    HttpClient httpclient = new DefaultHttpClient(ccm, paramsSecure); 

干杯!

+0

Sourabh感谢名单为您的答复..... 但现在是“500内部服务器错误”的HTML作为回复 – 2013-04-04 08:10:01

+0

这上面的代码不适合我。 – 2013-04-04 08:33:02

+0

您是否在本地服务器上运行此服务? – Prateek 2013-04-04 09:41:51

相关问题