【问题标题】:Error when using loopj: "Trust anchor for certification path not found."`使用 loopj 时出错:“未找到证书路径的信任锚。”`
【发布时间】:2017-10-17 16:57:12
【问题描述】:

目前我正在处理一个项目,我需要在该项目上 GET/PUT 来自具有 Android 基本身份验证的 Web 服务器的数据。

我按照http://loopj.com/android-async-http/的指示,却遇到了

“原因:java.security.cert.CertificateException:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚。”`

上面网站上的方法是使用HttpClient,现在已弃用。我知道有HttpURLConnection,但我找不到适合我的教程。

【问题讨论】:

  • Android 的一个问题是库很快就会过时。您使用的特定库 (loopj) 已经超过 2 年没有更新,因此您可能会遇到更多麻烦。由于您刚刚开始,因此最好尝试使用最新的库之一,例如 Volley 或 Retrofit,而不是将您的时间花在 loopj 上。然后,如果自签名 SSL 证书等出现问题,将更容易获得支持。
  • @DavidRawson 谢谢,我会试试 Volley

标签: java android ssl


【解决方案1】:

在这里回答:Http Basic Authentication in Java using HttpClient?

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class HttpBasicAuth {

    public static void main(String[] args) {

        try {
            URL url = new URL ("http://ip:port/login");
            String encoding = Base64Encoder.encode ("test1:test1");

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setRequestProperty  ("Authorization", "Basic " + encoding);
            InputStream content = (InputStream)connection.getInputStream();
            BufferedReader in   = 
                new BufferedReader (new InputStreamReader (content));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }

    }

}

【讨论】:

  • 感谢您的回复。如果我用自己的 url 替换 url,这个解决方案也有问题中提到的问题。 “找不到证书路径的信任锚”。如何解决这个问题。而对于"String encoding = Base64Encoder.encode("test1:test1");",我可以使用"String encoding = Base64.encodeToString("test1:test1".getBytes(),0);"吗代替?
猜你喜欢
  • 2018-10-07
  • 1970-01-01
  • 2019-01-02
  • 2020-11-08
  • 2018-02-20
  • 2019-06-18
  • 2014-01-29
  • 2017-10-03
  • 2019-05-19
相关资源
最近更新 更多