我正在尝试通过连接到 url 来读取 xml 文件并读取输入流,但是我有一个错误“java.io.IOException:服务器返回 HTTP 响应代码:401 for URL:https://....”
我通过 Authenticator 类处理了身份验证的情况
这是代码:
private static InputStream getConnection(String url) {
InputStream in = null;
try {
final String login="cloudtest@arrow.com";
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;
}