【问题标题】:how to call https secure rest service from android using httpclient如何使用httpclient从android调用https安全休息服务
【发布时间】:2013-03-26 03:58:31
【问题描述】:

我已经在 Android 中通过 http 成功调用了 rest 服务。

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

请任何人都可以帮助我在android中调用安全休息服务

【问题讨论】:

  • 你有没有遇到什么我想你不应该遇到的问题,如果有,那怎么办?
  • 我试过这个链接stackoverflow.com/questions/2864016/…,但我不明白如何使用这个SSL和HttpClient来调用我的服务

标签: android rest https


【解决方案1】:

这里是示例代码,您可以使用它来忽略 SSL 证书错误,创建一个名为 SimpleSSLSocketFactory 的新 java 类

  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();
    }
}

并使用以下 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);

干杯!!

【讨论】:

  • Sourabh 感谢您的回复.....但现在给出“500 internal server error” html 作为回复
  • 您是否在本地服务器上运行此服务?
猜你喜欢
  • 2012-03-24
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-13
  • 2011-12-16
  • 1970-01-01
  • 2014-10-20
相关资源
最近更新 更多