我想通过连接到URL读取一个XML文件并读取输入流,但我有一个错误 “java.io.IOException:服务器返回的HTTP响应代码:401网址:https:// ......”错误401,而试图连接到URL读取xml
我通过验证器类
这里处理身份验证的情况下是代码:
private static InputStream getConnection(String url) {
InputStream in = null;
try {
final String login="[email protected]";
final String password="password";
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(login, password.toCharArray());
}
});
URL myUrl = new URL(url);
URLConnection urlConn = myUrl.openConnection();
urlConn.connect();
in = urlConn.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
return in;
}
如何使用'Authenticator'将证书传递给服务器?不知道你是否检查过服务器支持的认证类型(基本/形式等)。 – Santosh 2013-05-08 12:08:42