目前我正在开展一个项目,我需要在Android上使用基本身份验证的网络服务器GET/PUT
数据。
我按照http://loopj.com/android-async-http/的说明操作,但遇到了
“引起:java.security.cert.CertificateException:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚。”`
上述网站上的方法是使用现已弃用的HttpClient
。我知道有HttpURLConnection
,但我无法找到适合我目的的教程。
答案 0 :(得分:-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();
}
}
}