【问题标题】:Getting the info from the password protected http. Java从受密码保护的 http 获取信息。爪哇
【发布时间】:2014-07-07 20:37:38
【问题描述】:

我正在尝试在一个简单的 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 行也是如此。

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

任何帮助将不胜感激。

【问题讨论】:

  • 您确定您的服务器使用的是基本身份验证吗? IIS 有几种模式:msdn.microsoft.com/en-us/library/ee825205(v=cs.10).aspx
  • 我不是。因为这是我第一次尝试做这样的事情。
  • Hmm.. 你能在浏览器中使用网络检查器或其他东西来查看浏览器发送的实际标头吗?应该很容易从 firefox 或 chrome 中提取这些
  • 我咨询了我们的 IT 人员。已检查 Windows 域服务器的集成 Windows 身份验证和摘要式身份验证。不是基本身份验证。
  • 这就是你要问的吗?请求:GET corp.domain.com/delorie.htm HTTP/1.0 HTTP/1.0 401 Unauthorized Content-Length:1656 Content-Type:text/html 服务器:Microsoft-IIS/6.0 WWW-Authenticate:协商 WWW-Authenticate:NTLM WWW-Authenticate:Digest qop= “AUTH”,算法MD5 =-sess,则随机数= “+升级+ v18bc06f81dbdfbe12faa0a4569e73cf01dfe89f5ae489730f04155ad6208eda8867f71e05a127aa4c37a568dca740f81b”,字符集= UTF-8,境界= “domain.local” X供电-通过:ASP.NET 2.0 MicrosoftOfficeWebServer:5.0_Pub X-Powered-作者:ASP.NET 代理支持:基于会话的身份验证连接:代理支持

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


【解决方案1】:

根据上面的 cmets,您似乎正在使用 NTLM 身份验证。我建议使用原生支持 NTLM 身份验证的库。这个问题在这个页面上讨论:

authenticate with ntlm (or kerberos) using java UrlConnection

这是 Apache HttpClient 站点上有关 NTLM 的页面:

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

【讨论】:

    猜你喜欢
    • 2011-07-22
    • 1970-01-01
    • 2022-07-03
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    相关资源
    最近更新 更多