Android Http Post 正在下载错误的 SSL 证书

标签 android ssl

因此,在我的网站上,我使用了几个不同的 SSL 证书。一个用于根域“illution.dk”,一个用于我的子域“ci.illution.dk”。问题是,当我使用 HttpPost 发出发布请求时,我请求一个像“https://ci.illution.dk/login/device”这样的 URL,它只是抛出一条错误消息说:

10-04 18:35:13.100: W/System.err(1680): javax.net.ssl.SSLException: hostname in certificate didn't match: <ci.illution.dk> != <www.illution.dk> OR <www.illution.dk> OR <illution.dk>

我认为这意味着它正在下载illution.dk的证书,然后看到它不支持ci.illution.dk。但是,当我加载浏览器并浏览到“https://ci.illution.dk”时,一切都很好。我的安卓代码如下:

HttpClient httpclient = new DefaultHttpClient();
        //appContext.getString(R.string.base_url)
        HttpPost httppost = new HttpPost("https://ci.illution.dk/login/device");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username", params[0]));
            nameValuePairs.add(new BasicNameValuePair("password", params[1]));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            return response;
        } catch (ClientProtocolException e) {
            Log.d("ComputerInfo", "Error while loggin in: ClientProtocolException");
            return null;
        } catch (IOException e) {
            Log.d("ComputerInfo", "Error while loggin in: IOException");
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            Log.d("ComputerInfo", "Error while loggin in");
            e.printStackTrace();
            return null;
        }

最佳答案

看看下面的代码你就会得到答案。我在我的代码中使用了它。您必须在代码中使用。

BufferedReader reader = new BufferedReader(new InputStreamReader( 是,“iso-8859-1”),8);

StringBuilder sb = new StringBuilder();

字符串行=空;

while ((line = reader.readLine()) != null) {

sb.append(line + "\n");

是.close();

看看我在其中使用的以下示例

    ArrayList<DailyExpDto> list = new ArrayList<DailyExpDto>();
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair("date", "" + date));
    qparams.add(new BasicNameValuePair("uid", ""
            + Myapplication.getuserID()));

    try {
        HttpClient httpclient = new DefaultHttpClient();
                    httpclient.getCredentialsProvider().setCredentials(
                new AuthScope(null, -1),
                new UsernamePasswordCredentials("YOURUSRNAME", "YOURPASSWORD"));
        HttpPost httppost = new HttpPost(url + "daily_expenditure.php?");
        httppost.setEntity(new UrlEncodedFormEntity(qparams));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }
    // convert response to string
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();

        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }

    Log.v("log", result);
    JSONObject jobj = null;
    try {
        jobj = new JSONObject(result);

    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }
    try {

        JSONArray JArray_cat = jobj.getJSONArray("category");
        JSONArray JArray_desc = jobj.getJSONArray("description");
        JSONArray JArray_exp = jobj.getJSONArray("expenditure");
        for (int i = 0; i < JArray_cat.length(); i++) {
            DailyExpDto dto = new DailyExpDto();
            dto.category = JArray_cat.getString(i);
            dto.desc = JArray_desc.getString(i);
            dto.exp = JArray_exp.getInt(i);
            list.add(dto);
        }

    } catch (Exception e) {
        // TODO: handle exception
    }
    return list;
}

关于Android Http Post 正在下载错误的 SSL 证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12731970/

相关文章:

android - Genymotion 无法启动

postgresql - 是否可以将 AWS RDS Postgres SSL 连接与 DNS 别名而不是 AWS 端点一起使用?

codeigniter - 为什么在 .htaccess 中启用 SSL 后我的网站加载缓慢?

Android 从 Activity 堆栈弹出

android - 如何在 Android AppWidget 中设置列​​ TableView 的初始滚动位置?

android - Android 中电子邮件的自定义标题?

android - 无法实现扩展的 TextView

amazon-web-services - 如何使用 ACM 为 EC2 设置 SSL?

linux - 由于 TLS 错误,无法进行 svn checkout

python - 由于 SSL 错误,无法让 virtualenv 工作