使用自签名证书在Android中保护HTTP发布

问题描述

| 我目前正在为我的组织网站开发第一个android应用程序和第一个api。我正在尝试使用安全连接从android应用连接到api。我们的网站在8090上有一个测试端口,我正尝试使用该端口来测试api,但是我遇到的问题是网站上有一个自签名证书,根据我在网上阅读的内容,android apps无法\不喜欢。为了确保api没有问题,我将它与http而不是https连接一起使用,并且效果很好。我已经尝试了一些在线解决方案,包括站点中的一些解决方案,但似乎都没有用。同样,我没有为Android开发的丰富经验,所以我的很多尝试只是从网上找到的解决方案进行复制和粘贴。以下是一些我尝试过的链接: Https连接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();
        }
我还想补充一点,购买证书不是一种选择,因为我们没有可以使用的预算,因此解决自签名证书问题的任何方法都将是不错的选择。提前致谢!     

解决方法

        也许在所有时间都忽略了花序,直到它被签名了? 试试这个:
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;
}