1

这一次我真的被困住了。我正在为 Froyo (2.2.0) 开发 Android 应用程序。我需要连接到通过https. 至于现在信任所有证书是我想做的事情(我将在接下来的几天内解决这个问题,但现在发布在我身上)。我已经从这篇非常有用的帖子中复制粘贴了代码。太完美了,我这样构建我的客户:

public class RestClient {
    private static final String LOG_TAG = "RestClient";

    private HttpClient httpClient;
    private Context context;

    public RestClient(Context context) {
        this.httpClient = CustomSSLSocketFactory.getCustomHttpClient();
        this.context = context;
    }

    public String post(String path, String requestBody,
        String optFieldOfInterest) {
        HttpPost httpPost = new HttpPost(getAddress() + path);
        httpPost.setEntity(prepareStringEntity(requestBody));
        Log.d(LOG_TAG, "Executing request:\n" + requestBody);
        return executeRequest(httpPost, optFieldOfInterest);
    }

    private StringEntity prepareStringEntity(String requestBody) {
        StringEntity entity = null;
        try {
            entity = new StringEntity(requestBody, "UTF-8");
            entity.setContentType("application/json");
        } catch (UnsupportedEncodingException e) {
            Log.e(LOG_TAG, "Error while preparing request", e);
        }
        return entity;
    }

    private String executeRequest(HttpRequestBase request)
          throws ClientProtocolException, IOException {
        HttpResponse httpResponse = null;
        String responseString = null;
        httpResponse = httpClient.execute(request); // Exception line
        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            String responseBody =
                IOHelper.parseStream(httpResponse.getEntity().getContent());
            Log.d(LOG_TAG, "Response body:\n" + responseBody);
            responseString = responseBody;
        } else {
            Log.w(LOG_TAG, "I am not passing through here my log shows it");
        }
        return responseString;
    }
}

但是我遇到以下错误:

org.apache.http.NoHttpResponseException: The target server failed to respond
    at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:85)
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
    at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
    at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
    at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:421)
    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.mycompany.myproject.ws.client.MySSLHttpClient.executeRequest(MySSLHttpClient.java:70)
    at com.mycompany.myproject.ws.client.MySSLHttpClient.post(MySSLHttpClient.java:120)
    at com.mycompany.myproject.ws.asynctasks.RegisterDeviceAsyncTask.doInBackground(RegisterDeviceAsyncTask.java:57)
    at com.mycompany.myproject.ws.asynctasks.RegisterDeviceAsyncTask.doInBackground(RegisterDeviceAsyncTask.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:185)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    at java.lang.Thread.run(Thread.java:1019)

我看到其他人也偶然发现了类似的问题。例如,这篇文章列出了两种解决方案,但都不适合我。该线程还提出了一种新的解决方案,也不适用于我。大多数列出的修复程序甚至都没有改变我得到的异常。

如您所见,我尝试了很多事情。希望其他人遇到这样的问题并找到另一种解决方案。

4

1 回答 1

0

现在问题已经解决了。

问题不在于 java / Android 代码,而在于服务器配置。案例如下:我试图访问开发环境服务器。然而,事实证明我们的基础架构工程师使用与生产服务器完全相同的证书对其进行了配置。这意味着证书中的主机名是错误的(与生产环境匹配)。

奇怪的是,这些网络服务可以通过 Firefox 和 Chrome 访问,即使使用了错误的证书,这导致了所有的混乱。

希望在这里描述我的案例将有助于将来其他人解决同样的问题。

于 2012-04-06T08:59:37.417 回答