java - Android Froyo : Connection to https using trusting all socket factory results in target server failed to respond

标签 java android ssl

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

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)

我看到其他人也偶然发现了类似的问题。例如this post列出了两个解决方案,但都不适合我。 This thread还提出了一个新的解决方案,也不适合我。大多数列出的修复程序甚至都没有改变我遇到的异常。

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

最佳答案

现在问题已经解决了。

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

奇怪的是,即使使用错误的证书,也可以通过 Firefox 和 Chrome 访问这些网络服务,这引起了所有的困惑。

希望在这里描述我的案例能帮助其他人在未来解决同样的问题。

关于java - Android Froyo : Connection to https using trusting all socket factory results in target server failed to respond,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10033345/

相关文章:

c# - 使用 https 协议(protocol)使用 webservice

java - 如何在 LambdaJ 中创建值为 Collection 的 Map?

java - 无法在 Java 中播放声音

java - 如何在 Android Studio IDE 中找到我项目中所有未使用的方法?

android - 从 FragmentStatePagerAdapter 获取资源

使用不同 AWS ELB (EC2) 的子域的 SSL

java - 如何在 Spring Boot 中从属性文件读取正则表达式模式

java - 如何通过 SWIG 处理从 C++ 到 Java 的异常

java - 在php中创建JSONArray并将其发送到android

php - 测试客户端浏览器中是否安装了客户端证书