多亏了这个线程How to download and save a file from Internet using Java?,我知道如何下载文件,现在我的问题是我需要在下载的服务器上进行身份验证。它是一个到subversion服务器的http接口。我需要查找哪个字段?
使用上一条评论中发布的代码,我得到了这个异常:
java.io.IOException: Server returned HTTP response code: 401 for URL: http://myserver/systemc-2.0.1.tgz
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1305)
at java.net.URL.openStream(URL.java:1009)
at mypackage.Installer.installSystemc201(Installer.java:29)
at mypackage.Installer.main(Installer.java:38)谢谢,
发布于 2009-06-05 12:54:41
您可以扩展Authenticator类并注册它。链接中的javadoc解释了如何实现。
我不知道这是否适用于获得问题公认答案的nio方法,但它肯定适用于老式的方法,即该方法下的答案。
在验证器类实现中,您可能会使用PasswordAuthentication并覆盖验证器实现的getPasswordAuthentication()方法以返回它。这将是传递给您所需的用户名和密码的类。
根据您的请求,以下是一些示例代码:
public static final String USERNAME_KEY = "username";
public static final String PASSWORD_KEY = "password";
private final PasswordAuthentication authentication;
public MyAuthenticator(Properties properties) {
String userName = properties.getProperty(USERNAME_KEY);
String password = properties.getProperty(PASSWORD_KEY);
if (userName == null || password == null) {
authentication = null;
} else {
authentication = new PasswordAuthentication(userName, password.toCharArray());
}
}
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}并在main方法中注册它(或者在调用URL之前在代码行的某个地方注册):
Authenticator.setDefault(new MyAuthenticator(properties));用法很简单,但我发现API令人费解,而且与您通常思考这些事情的方式有些倒退。非常典型的单例设计。
发布于 2014-08-06 22:43:55
为Authenticator编写重写类:
import java.net.Authenticator;
import java.net.PasswordAuthentication;
public class MyAuthenticator extends Authenticator {
private static String username = "";
private static String password = "";
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication (MyAuthenticator.username,
MyAuthenticator.password.toCharArray());
}
public static void setPasswordAuthentication(String username, String password) {
MyAuthenticator.username = username;
MyAuthenticator.password = password;
}
}编写你的主类:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.URL;
public class MyMain{
public static void main(String[] args) {
URL url;
InputStream is = null;
BufferedReader br;
String line;
// Install Authenticator
MyAuthenticator.setPasswordAuthentication("Username", "Password");
Authenticator.setDefault (new MyAuthenticator ());
try {
url = new URL("Your_URL_Here");
is = url.openStream(); // throws an IOException
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (is != null) is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
}
}发布于 2009-06-05 12:47:15
您是否尝试过在表单http://user:password@domain/path中构建您的URL
https://stackoverflow.com/questions/955624
复制相似问题