无法通过与android客户端的ssl连接从tomcat服务器获取数据

时间:2014-08-07 17:05:44

标签: java android ssl

我有一个名为MyHttpClient的类,我正在尝试使用带有.bks密钥库的“https”方案连接到服务器并下载我的字符串。

MyHttpClient:

public class MyHttpClient extends DefaultHttpClient {

static String response = null;
public final static int GET = 1;
public final static int POST = 2;

private static Scheme httpsScheme = null;
private static Scheme httpScheme = null;
private static String TAG = "MyHttpClient";

public MyHttpClient() {

    if (httpScheme == null || httpsScheme == null) {
        httpScheme = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80);
        httpsScheme = new Scheme("https", mySSLSocketFactory(), 443);
    }
    getConnectionManager().getSchemeRegistry().register(httpScheme);
    getConnectionManager().getSchemeRegistry().register(httpsScheme);
}

public String makeServiceCall(String url, int method) {
    return this.makeServiceCall(url, method, null);
}

private SSLSocketFactory mySSLSocketFactory() {
    SSLSocketFactory ret = null;
    try {
        final KeyStore ks = KeyStore.getInstance("BKS");

        final InputStream inputStream = MainActivity.getAppContext().getResources().openRawResource(R.raw.android);

        ks.load(inputStream, MainActivity.getAppContext().getString(R.string.store_pass).toCharArray());
        inputStream.close();

        ret = new SSLSocketFactory(ks);
    } catch (UnrecoverableKeyException ex) {
        Log.d(TAG, ex.getMessage());
    } catch (KeyStoreException ex) {
        Log.d(TAG, ex.getMessage());
    } catch (KeyManagementException ex) {
        Log.d(TAG, ex.getMessage());
    } catch (NoSuchAlgorithmException ex) {
        Log.d(TAG, ex.getMessage());
    } catch (IOException ex) {
        Log.d(TAG, ex.getMessage());
    } catch (Exception ex) {
        Log.d(TAG, ex.getMessage());
    } finally {
        return ret;
    }
}

public String makeServiceCall(String url, int method,
        List<NameValuePair> params) {
    try {
        // http client
        MyHttpClient myClient = new MyHttpClient();
        //DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        // Checking http request method type
        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            // adding post params
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }

            httpResponse = myClient.execute(httpPost);

        } else if (method == GET) {
            // appending params to url
            if (params != null) {
                String paramString = URLEncodedUtils
                        .format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);

            httpResponse = myClient.execute(httpGet);

        }
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return response;
}

}

我从另一个类和下面的asynctask调用此类,但我总是从asynctask“无法从URL获取任何数据”中收到相同的消息

private class GetProducts extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
    }
    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        MyHttpClient sh = new MyHttpClient();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {

        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
}

logcat:

javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
at      org.apache.harmony.xnet.provider.jsse.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.jav    a:146)
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:93)
at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:388)
at       org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConne    ctionOperator.java:165)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at    org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119    )
at    org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.example.ssl.MyHttpClient.makeServiceCall(MyHttpClient.java:110)
at com.example.ssl.MyHttpClient.makeServiceCall(MyHttpClient.java:51)
at com.example.ssl.MainActivity$GetProducts.doInBackground(MainActivity.java:64)
at com.example.ssl.MainActivity$GetProducts.doInBackground(MainActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
D/Response:(7670): > null
E/ServiceHandler(7670): Couldn't get any data from the url
E/ViewRootImpl(7670): sendUserActionEvent() mView == null

请帮助,我不知道问题出在哪里,在客户端或服务器中?

0 个答案:

没有答案