在android中与httpclient建立安全连接

发布于 2024-12-28 13:51:15 字数 194 浏览 0 评论 0原文

我的 defaultHttpClient 有问题。

我必须连接到使用 HTTPS 连接的服务器。我发现 HttpClient 默认使用 TLS 安全协议,而我要连接的服务器不支持它。

我无法更改此设置,我被迫更改应用程序中的安全协议!

我该怎么做?

I have a problem with my defaultHttpClient.

I must do a connection to a server that use an HTTPS connection. I have discovered that HttpClient use by default the TLS security protocol and the server where i want to connect does not support it.

I can't change this and I am forced to change my security protocol in my application!

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗲 2025-01-04 13:51:15

尝试一下这个客户端,它对我有很大帮助。

public class MyHttpClient1 extends DefaultHttpClient {

    final Context context;


    MyHttpClient1 mHttpClient=null;

    public MyHttpClient1(Context context) {
          this.context = context;
    }

    @Override
    protected ClientConnectionManager createClientConnectionManager() {
      SchemeRegistry registry = new SchemeRegistry();
        KeyStore trustStore = null;
        try {
            trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        } catch (KeyStoreException e) {
            e.printStackTrace();  
        }
        try {
            trustStore.load(null, null);
        } catch (IOException e) {
            e.printStackTrace();  
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();  
        } catch (CertificateException e) {
            e.printStackTrace();  
        }
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
      // Register for port 443 our SSLSocketFactory with our keystore
      // to the ConnectionManager
        try {
            registry.register(new Scheme("https",new MySSLSocketFactory(trustStore), 443));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();  
        } catch (KeyManagementException e) {
            e.printStackTrace();  
        } catch (KeyStoreException e) {
            e.printStackTrace();  
        } catch (UnrecoverableKeyException e) {
            e.printStackTrace();  
        }
        return new SingleClientConnManager(getParams(), registry);
    }

    public class MySSLSocketFactory extends SSLSocketFactory {
    SSLContext sslContext = SSLContext.getInstance("SSL");   //or TLS

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

        TrustManager tm = new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

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

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };

        sslContext.init(null, new TrustManager[] { tm }, null);
    }

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

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

}

Try this client it helped me alot once.

public class MyHttpClient1 extends DefaultHttpClient {

    final Context context;


    MyHttpClient1 mHttpClient=null;

    public MyHttpClient1(Context context) {
          this.context = context;
    }

    @Override
    protected ClientConnectionManager createClientConnectionManager() {
      SchemeRegistry registry = new SchemeRegistry();
        KeyStore trustStore = null;
        try {
            trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        } catch (KeyStoreException e) {
            e.printStackTrace();  
        }
        try {
            trustStore.load(null, null);
        } catch (IOException e) {
            e.printStackTrace();  
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();  
        } catch (CertificateException e) {
            e.printStackTrace();  
        }
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
      // Register for port 443 our SSLSocketFactory with our keystore
      // to the ConnectionManager
        try {
            registry.register(new Scheme("https",new MySSLSocketFactory(trustStore), 443));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();  
        } catch (KeyManagementException e) {
            e.printStackTrace();  
        } catch (KeyStoreException e) {
            e.printStackTrace();  
        } catch (UnrecoverableKeyException e) {
            e.printStackTrace();  
        }
        return new SingleClientConnManager(getParams(), registry);
    }

    public class MySSLSocketFactory extends SSLSocketFactory {
    SSLContext sslContext = SSLContext.getInstance("SSL");   //or TLS

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

        TrustManager tm = new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

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

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };

        sslContext.init(null, new TrustManager[] { tm }, null);
    }

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

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

}
陪你到最终 2025-01-04 13:51:15

这似乎不起作用,但我通过添加方法解决了它:

setEnableProtocols(new String[]{"SSLv3"});

在创建我的个人 SSLSocket

现在一切似乎都有效!

This does not seems to work, but I have solved it by adding the method:

setEnableProtocols(new String[]{"SSLv3"});

At the creation of my personal SSLSocket

Now all seems to work!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文