使用java从internet下载文件:如何进行身份验证?
多亏了这条线 我知道如何下载一个文件,现在我的问题是我需要在我下载的服务器上进行身份验证。它是subversion服务器的http接口。我需要查哪个领域 使用上一条评论中发布的代码,我得到了以下异常:使用java从internet下载文件:如何进行身份验证?,java,authentication,httpwebrequest,download,basic-authentication,Java,Authentication,Httpwebrequest,Download,Basic Authentication,多亏了这条线 我知道如何下载一个文件,现在我的问题是我需要在我下载的服务器上进行身份验证。它是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.HttpURLConne
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)
谢谢,您尝试过在表单中构建您的URL吗?我建议您从apache签出HttpClient,这使得下载/验证非常容易您可以扩展类并注册它。链接中的javadocs解释了如何使用 我不知道这是否适用于nio方法,nio方法得到了这个问题的公认答案,但它确实适用于老式的方法,这是该方法下的答案 在authenticator类实现中,您可能要使用一个并重写authenticator实现的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之前在该行的某个位置注册):
用法很简单,但我发现API很复杂,而且有点像你通常认为的那样。非常典型的单例设计。这是我编写的一些代码,用于获取网站并将内容显示到System.out。它使用基本身份验证:
import java.net.*;
import java.io.*;
public class foo {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://www.MY_URL.com");
String passwdstring = "USERNAME:PASSWORD";
String encoding = new
sun.misc.BASE64Encoder().encode(passwdstring.getBytes());
URLConnection uc = yahoo.openConnection();
uc.setRequestProperty("Authorization", "Basic " + encoding);
InputStream content = (InputStream)uc.getInputStream();
BufferedReader in =
new BufferedReader (new InputStreamReader (content));
String line;
while ((line = in.readLine()) != null) {
System.out.println (line);
}
in.close();
}
上述代码的问题:
请查看该类的java文档以获取示例。为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
}
}
}
}
如果你从url中省略了用户id和密码,你会得到什么样的例外?如果我省略了登录/通行证,或者如果我把它放进去(代码401),你能发布一些示例代码吗?javadoc没有给出任何示例,而且不可能知道如何实际使用这个类!嗯,您只需调用setDefault,它就是`Authenticator.setDefault(新Authenticator(){受保护的密码身份验证getPasswordAuthentication(){返回新密码身份验证(“login”,“pass.tocharray());}});`我很抱歉,事情没有解决。看起来@Yishai提到的验证器封装了该技术。如果您想要base64编码器,我推荐iharder.net的实现。公共领域,再自由不过了。Groovy已经有了base64支持!只需执行passwd.getBytes().encodeBase64().toString()。如果我已经有了base64编码的基本身份验证令牌,那么这是一个好的解决方案吗?
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
}
}
}
}