使用 java 从 Internet 下载文件:如何进行身份验证? [英] Download a file from the internet using java : How to authenticate?

查看:20
本文介绍了使用 java 从 Internet 下载文件:如何进行身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢这个帖子 如何下载并使用 Java 从 Internet 保存文件?我知道如何下载文件,现在我的问题是我需要在我下载的服务器上进行身份验证.它是 Subversion 服务器的 http 接口.我需要查找哪个字段?

Thanks to this thread How to download and save a file from Internet using Java? I know how to download a file, now my problem is that I need to authenticate on the sever from which I'm dowloading. It's an http interface to a subversion server. Which field do I need to look up into ?

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

Using the code posted in the last comment, I get this exception:

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)

谢谢,

推荐答案

您扩展了 Authenticator 类并注册它.链接中的 javadoc 解释了如何操作.

You extend the Authenticator class and register it. The javadocs at the link explain how.

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

I don't know if this works with the nio method that got the accepted answer to the question, but it for sure works for the old fashioned way that was the answer under that one.

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

Within the authenticator class implementation, you are probably going to use a PasswordAuthentication and override the getPasswordAuthentication() method of your Authenticator implementation to return it. That will be the class which is passed the user name and password you need.

根据您的要求,这里有一些示例代码:

Per your request, here is some sample code:

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 之前的某处):

And you register it in the main method (or somewhere along the line before you call the URL):

Authenticator.setDefault(new MyAuthenticator(properties));

用法很简单,但我发现 API 令人费解,并且与您通常对这些事情的看法有点倒退.非常典型的单例设计.

The usage is simple, but I find the API convoluted and kind of backwards for how you typically think about these things. Pretty typical of singleton design.

这篇关于使用 java 从 Internet 下载文件:如何进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆