在android中的https://上连接到Web服务

时间:2014-11-07 14:44:05

标签: android web-services ssl https connection

处理事件管理器应用程序,连接到http:// Web服务以检索在本地sqlite DB中存储事件的JSON响应等等一切正常,直到我们将Web服务移动到https://,i每次我尝试连接到服务时都开始收到ssl证书错误。 使用DefaultHttpClient,
我查找堆栈溢出的解决方案,我发现有些人要编写自定义类来解决问题。 我无法相信没有直接的解决方案可以从https://连接到web服务而无需编写自定义

如果有人可以帮我解决这个问题,我会很感激

这里是代码

package com.xxxxx.xxxxxxxxx.utility;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;  
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {
    // input stream to buffer data
    static InputStream is = null;
    //json object 

    static JSONObject jObj = new JSONObject();
    // json string
    static String json = "";
    private String socketResult = null;

    // constructor
    public JSONParser() {

    }

    // function get json from url

    public JSONObject makeHttpRequest(String url, List<NameValuePair> params) {

        // Making HTTP request
        try {

            final HttpParams httpParameters = new BasicHttpParams();
            // Set the timeout in milliseconds until a connection is established.
              HttpConnectionParams.setConnectionTimeout(httpParameters, 240000);

            // Set the default socket timeout (SO_TIMEOUT)
            // in milliseconds which is the timeout for waiting for data.
           HttpConnectionParams.setSoTimeout(httpParameters, 180000);
            // request method is POST
            // defaultHttpClient

           DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
            // HTTP POST method
            HttpPost httpPost = new HttpPost(url);
            // Hands the entity to the request. // thr paramters to pass to the request in the form of name value pairs  
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            // handels the response form the http request 
            HttpResponse httpResponse = httpClient.execute(httpPost);

StatusLine status = httpResponse.getStatusLine();
if (status.getStatusCode() == HttpStatus.SC_OK) {


    // An entity that can be sent or received with an HTTP message. Entities can be found in some    requests and in responses, where they are optional.
    HttpEntity httpEntity = httpResponse.getEntity();
      // store the content / data of the entity in an input stream 
    is = httpEntity.getContent();
}else{
    // Do something else, if wanted.
}

           // An entity that can be sent or received with an HTTP message. Entities can be found in some requests and in responses, where they are optional.
         // HttpEntity httpEntity = httpResponse.getEntity();
           // store the content / data of the entity in an input stream 
         //is = httpEntity.getContent();



        } catch (UnsupportedEncodingException e) {
            Log.d("UnsupportedEncodingException", "HTTP Error", e);

        } catch (ClientProtocolException e) {
            Log.d("ClientProtocolException", "HTTP Error", e);

        } catch (IOException e) {
            Log.d("IOException", "Connection Error", e);

        }

        try {

            // read the data in the input stream entity to buffer 
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            // construct a string builder object the buffer data 
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            // close the input stream 
            is.close();


            //  create string from the json data 
            json = sb.toString();
        } catch (Exception e) {
            Log.d("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            //----------------------------------------------------------------------
            //Log.d(" creating json object " , json);


            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.d("JSON Parser", "Error parsing data " + e.toString());
            //----------------------------------------------------------------------
             System.out.println("JSON Parserexception:" + e);
        }

        // return JSON String
        return jObj;

    }
}

3 个答案:

答案 0 :(得分:0)

证书必须由识别设备的受信任第三方证书颁发机构签名。如果不是这种情况,那么您的代码就会出现一些问题,例如在其中包含证书并对其进行验证。

答案 1 :(得分:0)

您需要做的第一件事是设置验证级别。这样的水平并不是那么多:

ALLOW_ALL_HOSTNAME_VERIFIER BROWSER_COMPATIBLE_HOSTNAME_VERIFIER STRICT_HOSTNAME_VERIFIER 虽然方法setHostnameVerifier()对于新的库apache已经过时,但对于Android SDK中的版本是正常的。所以我们采用ALLOW_ALL_HOSTNAME_VERIFIER并在方法工厂SSLSocketFactory.setHostnameVerifier()中设置它。

接下来,您需要将协议的工厂设置为https。为此,只需调用SchemeRegistry.register()方法。

然后,您需要使用SingleClientConnManager创建DefaultHttpClient。同样在下面的代码中,您可以看到默认情况下还将通过方法HttpsURLConnection.setDefaultHostnameVerifier()

使用我们的标志(ALLOW_ALL_HOSTNAME_VERIFIER)

以下代码适用于我:

    HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

    DefaultHttpClient client = new DefaultHttpClient();

    SchemeRegistry registry = new SchemeRegistry();
    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
    registry.register(new Scheme("https", socketFactory, 443));
    SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
    DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());

    // Set verifier     
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

    // Example send http request
    final String url = "https://encrypted.google.com/";
    HttpPost httpPost = new HttpPost(url);
    HttpResponse response = httpClient.execute(httpPost);

答案 2 :(得分:0)

尝试在项目中添加一个类 -

public class MySSLSocketFactory extends SSLSocketFactory {
    SSLContext sslContext = SSLContext.getInstance("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();
    }
}

然后调用此方法获取客户端 -

public HttpClient getNewHttpClient() {
        try {
            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));

            ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

            return new DefaultHttpClient(ccm, params);
        } catch (Exception e) {
            return new DefaultHttpClient();
        }
    }

希望这会对你有所帮助。