【问题标题】:Secure HTTP Post in Android with self-signed certificate使用自签名证书在 Android 中保护 HTTP Post
【发布时间】:2011-09-04 13:51:51
【问题描述】:

我目前正在为我的组织网站开发我的第一个 android 应用和第一个 api。我正在尝试使用安全连接从 android 应用程序连接到 api。我们的网站在 8090 上有一个测试端口,我试图用它来测试 api 但我遇到的问题是我在网站上有一个自签名证书,从我在线阅读的内容来看,android 应用程序没有'不喜欢。为了确保 api 没有问题,我将它与 http 而不是 https 连接一起使用,并且效果很好。我已经尝试了一些我在网上找到的解决方案,包括来自这个网站的一些解决方案,但似乎没有一个有效。同样,我在为 Android 开发方面没有太多经验,所以我的很多尝试只是从我在网上找到的解决方案中复制和粘贴。以下是我尝试过的一些链接:

Https Connection Android

http://yekmer.posterous.com/how-to-accept-self-signed-certificates-in-and

我现在找不到其他页面的链接,但下面是我目前用来连接的代码:

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("https://website.edu:8090/api.php?");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("method", "login"));
            nameValuePairs.add(new BasicNameValuePair("user", username.getText().toString()));
            nameValuePairs.add(new BasicNameValuePair("pass", md5(password.getText().toString())));
            nameValuePairs.add(new BasicNameValuePair("submitLogin", "1"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
            HttpParams params = httppost.getParams();
            HttpConnectionParams.setConnectionTimeout(params, 45000);
            HttpConnectionParams.setSoTimeout(params, 45000);

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
        } catch (ClientProtocolException e) {
            e.printStackTrace();                
        } catch (IOException e) {               
            e.printStackTrace();
        }

我还想补充一点,购买证书不是一种选择,因为我们没有预算可以使用,所以任何可以解决自签名证书问题的东西都会很棒。提前致谢!

【问题讨论】:

标签: java android post https certificate


【解决方案1】:

也许在签名之前完全忽略序列化?

试试这个:

public static javax.net.ssl.TrustManager getTrustManager()
{
    javax.net.ssl.TrustManager tm = new javax.net.ssl.X509TrustManager() {

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
        return null;
        }

        @Override
        public void checkClientTrusted(
                java.security.cert.X509Certificate[] chain, String authType)
                throws java.security.cert.CertificateException {

        }

        @Override
        public void checkServerTrusted(
                java.security.cert.X509Certificate[] chain, String authType)
                throws java.security.cert.CertificateException {        
        }
        };
        return tm;
}



public static DefaultHttpClient getThreadSafeClient() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, KeyManagementException, UnrecoverableKeyException {
    DefaultHttpClient client = new DefaultHttpClient();
    ClientConnectionManager mgr = client.getConnectionManager();
    HttpParams cleintParams = client.getParams();

    cleintParams.setBooleanParameter("http.protocol.expect-continue", true);
    cleintParams.setBooleanParameter("http.protocol.warn-extra-input", true);
    // params.setIntParameter("http.socket.receivebuffer", 999999);

    //---->> SSL
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    trustStore.load(null, null);

    SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
    sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

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

    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    registry.register(new Scheme("https", sf, 443));

    //<<------


client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, registry), cleintParams);

    return client;
}

【讨论】:

  • 对我不起作用,仍然收到 SSLPeerUnverifiedException。在 Nexus One 2.3.6 上。
猜你喜欢
  • 1970-01-01
  • 2019-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-26
  • 1970-01-01
  • 1970-01-01
  • 2014-08-22
相关资源
最近更新 更多