从密码保护http获取信息。 Java的

时间:2014-05-19 19:03:25

标签: java http windows-server-2003 windows-server

我正在尝试从一个简单的java程序中获取http中的文件。 我尝试使用域用户名和本地用户名。 我们正在使用Windows Server 2003。

我找到了2个解决方案。第一个是:

public class HttpTest {

    public static void main(String[] args) {
        try {

            URL url = new URL("http://corp.domain.com/name/info.html");
            String userPassword = "user:password";
            String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes());
            CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
            URLConnection connection = url.openConnection();
            connection.setRequestProperty("Authorization", "Basic " + encoding);
            InputStreamReader isr = new InputStreamReader(connection.getInputStream());
            BufferedReader in = new BufferedReader(isr);
            String str;
            while ((str = in.readLine()) != null) {
                System.out.println(str);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我发现的另一种方式是:

public class HttpTest {

    public static void main(String[] args) {
        try {
            Authenticator.setDefault(new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                String prompt = getRequestingPrompt();
                String host = getRequestingHost();
                InetAddress addressIP = getRequestingSite();
                int port = getRequestingPort();
                String username = "user";
                String password = "password";

                return new PasswordAuthentication(username, password.toCharArray());
            }
        });

            URL url = new URL("http://corp.domain.com/name/info.html");
            CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
            URLConnection connection = url.openConnection();
            InputStreamReader isr = new InputStreamReader(connection.getInputStream());
            BufferedReader in = new BufferedReader(isr);
            String str;
            while ((str = in.readLine()) != null) {
                System.out.println(str);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

第一个引发以下异常:

java.io.IOException: Server returned HTTP response code: 401 for URL: http://corp.domain.com/name/info.html
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1838)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at httptest.HttpTest.main(HttpTest.java:51)

第51行指向:

InputStreamReader isr = new InputStreamReader(connection.getInputStream());

第二个抛出:

java.net.ProtocolException: Server redirected too many  times (20)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1846)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at httptest.HttpTest.main(HttpTest.java:51)

第51行是相同的。

如果我尝试从浏览器访问服务器,则两个登录密码对(域和本地)都有效。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

根据上面的评论,您似乎正在使用NTLM身份验证。我建议使用一个本机支持NTLM身份验证的库。此问题将在此页面中讨论:

authenticate with ntlm (or kerberos) using java UrlConnection

这是Apache HttpClient网站上关于NTLM的页面:

http://hc.apache.org/httpcomponents-client-4.3.x/ntlm.html