java - 主机名在 Lollipop 设备中不匹配,但在 Postman 和 marshmallow 设备中工作正常

标签 java android ssl httpclient android-5.0-lollipop

最近 SSL 证书被添加到服务器,所以我将 android 中的 url 从 http://appname.com 更改为至 https://www.appname.com ,这在棉花糖设备和 Postman 上运行良好,但在 Lollipop 设备上抛出 javax.net.ssl.SSLException: hostname in certificate didn't match: www.appname.com != www.companyname.com OR www.companyname.com OR公司名称.com

我曾尝试在 setHostnameVerifier 中添加 companyname.com,但没有帮助。这是代码:

 HashMap<String, String> postDataParams=new HashMap<>();
        postDataParams.put("u_phone",CN);
        postDataParams.put("u_code",st);
        postDataParams.put("device_flag",mob_device);
        postDataParams.put("app_type","PRO");
        HostnameVerifier hostnameVerifier = new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                HostnameVerifier hv =
                        HttpsURLConnection.getDefaultHostnameVerifier();
                return hv.verify("companyname.com", session);
            }
        };


        try{

            URL url = new URL("https://www.appname.com/sync/validatecheck.php");
            HttpsURLConnection urlConnection =
                    (HttpsURLConnection)url.openConnection();
            //urlConnection.setHostnameVerifier(hostnameVerifier);
            urlConnection.setReadTimeout(10000);
            urlConnection.setConnectTimeout(15000);
            urlConnection.setRequestMethod("POST");
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);

            OutputStream os = urlConnection.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(getPostDataString(postDataParams));

            writer.flush();
            writer.close();
            os.close();
            int responseCode=urlConnection.getResponseCode();

            if (responseCode == HttpsURLConnection.HTTP_OK) {
                String line;
                BufferedReader br=new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                while ((line=br.readLine()) != null) {
                    result+=line;
                }
            }
            else {
                result="";

            }
        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection "+e.toString());
        }

这可能是什么问题?以及如何解决这个问题?

最佳答案

该错误表示颁发证书的主机名(主题中的 CN 字段)与服务器名称不匹配。

如果您使用的是 URL https://www.appname.com,则证书应颁发给 www.appname.com*。 appname.com。证书的主机名是 appname.com 然后错误是正确的,你可以使用 https://appname.com 但不是 https://www.appname .com.

https://appname.com 中部署您的服务器,为 www.appname.com 颁发新证书或设置 HostnameVerifier允许 www.appname.com

关于java - 主机名在 Lollipop 设备中不匹配,但在 Postman 和 marshmallow 设备中工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43090874/

相关文章:

java - 我怎样才能通过他的variantKey找到一个ProductVariant?

java.lang.String 无法转换为 JSONArray

java - 如何处理最终字符串?

安卓滑动面板

android - 在 Ubuntu 16.04 操作系统的 Android Studio 中选择 Dracula 主题时无法读取文本

ssl - GKE ca.cert 中没有 IP,这会导致连接到 API 服务器时出现 SSL 错误

java - 使用gradle构建Android应用程序时如何删除特定权限?

android - 内容提供者公地

python - debian 8.2 中的 "pip is configured with locations that require TLS/SSL"

java - 如何调试 ERR_SSL_PROTOCOL_ERROR/SSL_ERROR_RX_RECORD_TOO_LONG?