English 中文(简体)
利用java从互联网下载文件: 如何认证?
原标题:
  • 时间:2009-06-05 12:40:47
  •  标签:

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 ?

采用上次评论中公布的守则,我获得这一例外:

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)

感谢

最佳回答

页: 1 链接上的“java” 说明如何解释。

我不知道这一方法是否与大家接受的回答问题的方法有关,但我相信,这种方法能够以旧的老式方式解决问题。

在认证班级实施过程中,你可能使用。 密码Authentication,并且推翻了你执行判决的方法。 这将是你所需要的用户名称和密码的传译。

您的请求,这里是一些抽样守则:

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;
}

并且,在你称之为URL之前,你以主要方法(或者说大致一行)登记:

Authenticator.setDefault(new MyAuthenticator(properties));

这种使用很简单,但我发现,针对你通常如何思考这些问题,Adrend和那种倒退。 单一州设计的典型。

问题回答

这是我写到的一些法典,它提供了一个网站,并展示了该系统的内容。 它使用基本认证:

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();
}

上述法典的问题:

  1. 该代码为生产线(但从点到点)。

  2. 该法典产生了这一汇编者警告:

foo.java:11: warning: sun.misc.BASE64Encoder is Sun proprietary API and may be removed in a future release
      sun.misc.BASE64Encoder().encode(passwdstring.getBytes());
              ^ 1 warning

确实应该使用Austhenticator这一班子,但对于我来说,我无法指出,我也找不到任何例子,这只是表明,当你用自己的语言做冷却时, Java人实际上不喜欢。 : P-3

因此,以上是od解决办法,但它确实可行,可以轻易修改。

撰写主审论文:

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
            }
        }

    }

}

http://user:password@domain/path” rel=“nofollow noreferer” http://user:password@domain/path?

这一开放源图书馆http://spnego.sourceforge.net也有一些实例说明如何使用S SpnegoHttpURLConnection。

该类别的建筑商之一允许你通过用户名和密码。

举例看一下 s子。





相关问题
热门标签