0

下午好,正如标题中所说,我无法在我的 Android 应用程序中连接到我的球童驱动网站上的子域。我正在将应用程序的后端从 Heroku 迁移到我自己的 digitalocean droplet,并且该应用程序在使用 Heroku 时可以正常工作。

我得到的错误信息如下:

W/System.err: javax.net.ssl.SSLException: hostname in certificate didn't match: <sub.domain.com> != <domain.com> OR <domain.com>
W/System.err:     at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:185)
W/System.err:     at org.apache.http.conn.ssl.BrowserCompatHostnameVerifier.verify(BrowserCompatHostnameVerifier.java:54)
W/System.err:     at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:114)
W/System.err:     at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:95)
W/System.err:     at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:388)
W/System.err:     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:165)
W/System.err:     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
W/System.err:     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
W/System.err:     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
W/System.err:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
W/System.err:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)

相关的Android代码是:

try {
    HttpClient client = new DefaultHttpClient();
    final HttpPostHC4 post = new HttpPostHC4(url);
    String json = "";

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<>();
    for (Map.Entry<String, String> entry : valuePair.entrySet()) {
    nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    }

    UrlEncodedFormEntityHC4 entity = new UrlEncodedFormEntityHC4(nameValuePairs, "UTF-8");
    content = entity.getContent();
    System.out.println(entity);
    post.setEntity(entity);

    final HttpResponse response = client.execute(post);
    code = response.getStatusLine().getStatusCode();
    responseText = EntityUtils.toString(response.getEntity());
} catch (Exception exc) {
    exc.printStackTrace();
    if (responseText != null) {
    System.out.println(responseText);
    }
}

这是我的 Caddyfile 的相关部分

sub.domain.com {

    root /var/www/sub.domain.com/public
    log /var/www/sub.domain.com/storage/logs/caddy-access.log
    errors /var/www/sub.domain.com/storage/logs/caddy-error.log

    fastcgi / /run/php/php7.0-fpm.sock php {
    index index.php
    }

    rewrite {
    r .*
    ext /
    to /index.php?{query}
    }

}

我可以在我的网络浏览器中连接到子域,所以我真的不确定为什么会这样。我不太热衷于禁用主机名验证,所以我想知道是否有办法在 Caddy 端修复它。

4

1 回答 1

0

所以,我可以通过切换到 okhttp 库来解决这个问题。

摇篮依赖:

dependencies {
    compile group: 'com.squareup.okhttp3', name: 'logging-interceptor', version: '3.8.0'
    compile 'com.squareup.okhttp3:okhttp:3.8.0'
}

新版本的课程:

/**
 * Created by luke on 8/29/2016.
 */

import android.net.Uri;

import org.apache.http.NameValuePair;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Request.Builder;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

public class HttpPostRequest {
    public static final MediaType JSON
        = MediaType.parse("application/json; charset=utf-8");
    InputStream content;
    String responseText;
    int code;

    HttpPostRequest(String url, Map<String, String> valuePair) {
    try {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(logging).build();
        Uri uri = Uri.parse(url);


        HttpUrl.Builder httpUrlBuilder = new HttpUrl.Builder();
        httpUrlBuilder.host(uri.getHost());
        httpUrlBuilder.scheme(uri.getScheme());

        ArrayList<String> paths = new ArrayList<>(uri.getPathSegments());

        for (String path : paths) {
            httpUrlBuilder.addPathSegment(path);
        }

        Builder builder = new Request.Builder();

        FormBody.Builder formBuilder = new FormBody.Builder();

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<>();
        for (Map.Entry<String, String> entry : valuePair.entrySet()) {
            formBuilder.add(entry.getKey(), entry.getValue());
        }
        builder.url(httpUrlBuilder.build());
        builder.post(formBuilder.build());
        Response response = client.newCall(builder.build()).execute();
        code = response.code();
        responseText = response.body().string();
        responseText = responseText != null ? responseText : "";

    } catch (Exception exc) {
        exc.printStackTrace();
        if (responseText != null) {
            System.out.println(responseText);
        }
    }

    }
}
于 2017-05-25T07:26:05.613 回答