javax.net.ssl.SSLException:证书中的主机名不匹配:

时间:2017-01-13 06:44:59

标签: java android ssl

我在Android 4.4.4中遇到这些例外,但在Android 6和网络中它运行良好。

问题是什么?

@Override
protected String doInBackground(Object... params) {
    HttpResponse httpResponse = null;
    String response = null;
    Object httpRequest = null;
    if (null != params && params.length > 0) {
        httpRequest = params[0];
    }
    try {
        HttpParams httpParameters = new BasicHttpParams();
        int timeoutConnection = Constants.TIMEOUT_CONNECTION;
        HttpConnectionParams.setConnectionTimeout(httpParameters,
                timeoutConnection);
        int timeoutSocket = Constants.TIMEOUT_CONNECTION;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

        HttpClient httpClient = new DefaultHttpClient(httpParameters);

        if (httpRequest instanceof HttpPost) {
            httpResponse = httpClient.execute((HttpPost) httpRequest);
        } else if (httpRequest instanceof HttpGet) {
            httpResponse = httpClient.execute((HttpGet) httpRequest);
        } else if (httpRequest instanceof HttpDelete) {
            httpResponse = httpClient.execute((HttpDelete) httpRequest);
        } else if (httpRequest instanceof HttpPut) {
            httpResponse = httpClient.execute((HttpPut) httpRequest);
        }
        StringBuffer stringBuffer = new StringBuffer("");
        if (httpResponse != null)// Retrieve response
        {
            if (httpResponse.getEntity() == null) {
                LogManager.D(TAG, "Entity is null");
            }
            String output;
            InputStream is = httpResponse.getEntity().getContent();
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(is));

            while ((output = br.readLine()) != null) {
                stringBuffer.append(output);
            }
            stringBuffer.trimToSize();
            is.close();
            br.close();
            br = null;
            is = null;

            httpResponse.getEntity().consumeContent();
            response = stringBuffer.toString();
            statusCode = httpResponse.getStatusLine().getStatusCode();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return response;
}

以及exception

中的Android 4.4.4
01-13 11:14:45.001: W/System.err(10927): javax.net.ssl.SSLException: hostname in certificate didn't match:
 <abc.dbc.net> != <*.azurewebsites.net> OR <*.azurewebsites.net> OR <*.scm.azurewebsites.net> OR <*.azure-mobile.net> OR <*.scm.azure-mobile.net>
01-13 11:14:45.006: W/System.err(10927):    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:185)
01-13 11:14:45.006: W/System.err(10927):    at org.apache.http.conn.ssl.BrowserCompatHostnameVerifier.verify(BrowserCompatHostnameVerifier.java:54)
01-13 11:14:45.006: W/System.err(10927):    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:114)
01-13 11:14:45.006: W/System.err(10927):    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:95)
01-13 11:14:45.006: W/System.err(10927):    at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:381)
01-13 11:14:45.006: W/System.err(10927):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:165)
01-13 11:14:45.006: W/System.err(10927):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-13 11:14:45.011: W/System.err(10927):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-13 11:14:45.011: W/System.err(10927):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-13 11:14:45.011: W/System.err(10927):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-13 11:14:45.011: W/System.err(10927):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-13 11:14:45.011: W/System.err(10927):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-13 11:14:45.011: W/System.err(10927):    at com.ericsson.dat.Presenter.threading.NetworkTask.doInBackground(NetworkTask.java:79)
01-13 11:14:45.011: W/System.err(10927):    at com.ericsson.dat.Presenter.threading.NetworkTask.doInBackground(NetworkTask.java:1)
01-13 11:14:45.011: W/System.err(10927):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-13 11:14:45.016: W/System.err(10927):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-13 11:14:45.016: W/System.err(10927):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-13 11:14:45.016: W/System.err(10927):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-13 11:14:45.016: W/System.err(10927):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-13 11:14:45.016: W/System.err(10927):    at java.lang.Thread.run(Thread.java:856)

0 个答案:

没有答案