首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用java从互联网下载文件:如何进行身份验证?

使用java从互联网下载文件:如何进行身份验证?
EN

Stack Overflow用户
提问于 2009-06-05 12:40:47
回答 5查看 36.2K关注 0票数 17

多亏了这个线程How to download and save a file from Internet using Java?,我知道如何下载文件,现在我的问题是我需要在下载的服务器上进行身份验证。它是一个到subversion服务器的http接口。我需要查找哪个字段?

使用上一条评论中发布的代码,我得到了这个异常:

代码语言:javascript
复制
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)

谢谢,

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2009-06-05 12:54:41

您可以扩展Authenticator类并注册它。链接中的javadoc解释了如何实现。

我不知道这是否适用于获得问题公认答案的nio方法,但它肯定适用于老式的方法,即该方法下的答案。

在验证器类实现中,您可能会使用PasswordAuthentication并覆盖验证器实现的getPasswordAuthentication()方法以返回它。这将是传递给您所需的用户名和密码的类。

根据您的请求,以下是一些示例代码:

代码语言:javascript
复制
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之前在代码行的某个地方注册):

代码语言:javascript
复制
Authenticator.setDefault(new MyAuthenticator(properties));

用法很简单,但我发现API令人费解,而且与您通常思考这些事情的方式有些倒退。非常典型的单例设计。

票数 16
EN

Stack Overflow用户

发布于 2014-08-06 22:43:55

为Authenticator编写重写类:

代码语言:javascript
复制
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;
    }
}

编写你的主类:

代码语言:javascript
复制
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
            }
        }

    }

}
票数 8
EN

Stack Overflow用户

发布于 2009-06-05 12:47:15

您是否尝试过在表单http://user:password@domain/path中构建您的URL

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/955624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档