【发布时间】:2013-05-02 15:45:35
【问题描述】:
我正在尝试通过连接到 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;
}
【问题讨论】:
-
如何使用
Authenticator将凭据传递给服务器?不确定您是否检查了服务器支持的身份验证类型(基本/表单等)。
标签: java servlets https connection inputstream