【问题标题】:open a web page through java that need Authentication通过java打开需要Authentication的网页
【发布时间】:2014-11-28 15:50:30
【问题描述】:

我正在尝试通过 JAVA 打开一个需要用户名和密码的网页: page I am trying to acces

这是我复制的引用from this site的代码:

    public static void main(String[] args) {

    try {
        String webPage = "https://reports.tradenetworks.com/ReportServer_DISTRIBUTORRS?%2fDealing&rs:Command=ListChildren";
        String name = "Tradenetworks\\USER";
        String password = "PASS";
        String authString = name + ":" + password;
        System.out.println("auth string: " + authString);
        byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
        String authStringEnc = new String(authEncBytes);
        System.out.println("Base64 encoded auth string:" + authStringEnc);

        URL url = new URL(webPage);
        URLConnection urlConnection = url.openConnection();
        urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
        InputStream is = urlConnection.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);

        int numCharsRead;
        char[] charArray = new char[1024];
        StringBuffer sb = new StringBuffer();
        while ((numCharsRead = isr.read(charArray)) > 0) {
            sb.append(charArray, 0, numCharsRead);
        }
        String result = sb.toString();

        System.out.println("*** BEGIN ***");
        System.out.println(result);
        System.out.println("*** END ***");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

但是我从 eclipse 中得到这个错误:

java.io.IOException: Authentication failure
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at ConnectToUrlUsingBasicAuthentication.main(ConnectToUrlUsingBasicAuthentication.java:27)

【问题讨论】:

  • 不,您从站点的网络服务器而不是 Eclipse 收到该错误。停止射击信使。所以现在你必须调试你的问题,我会首先输出那个base64编码字符串来看看里面到底有什么——可能它不正确。当您只是从某个随机站点复制/粘贴代码时,往往会发生这种情况。您确实尝试在浏览器中打开网址,对吗?如果用户名/密码实际上是错误的,那就太可惜了。
  • 嘿用户名和密码正确我重新检查了如果base64正确我现在怎么办?

标签: java html authentication web


【解决方案1】:

我认为问题出在带有斜杠的 username 字符串 (\\) 中。这些字符在 URL 编码时看起来像这样:%5C%5C。所以,你应该试试String name = "Tradenetworks%5C%5CUSER";

希望有帮助!

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题!我设法通过使用自定义身份验证器使代码正常工作:

    try {
        // Sets the authenticator that will be used by the networking code
        // when a proxy or an HTTP server asks for authentication.
        Authenticator.setDefault(new CustomAuthenticator());
    
        URL url = new URL(webPage);
    
        // read text returned by server
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        in.close();
    } catch (MalformedURLException e) {
        System.out.println("Malformed URL: " + e.getMessage());
    } catch (IOException e) {
        System.out.println("I/O Error: " + e.getMessage());
    }
    

    这里是customAuthendicator的实现。

    public static class CustomAuthenticator extends Authenticator {
    
        // Called when password authorization is needed
        protected PasswordAuthentication getPasswordAuthentication() {
            String username = "ImtLis";
            String password = "ImtLis2017";
            // Return the information (a data holder that is used by
            // Authenticator)
            return new PasswordAuthentication(username, password.toCharArray());
        }
    
    }
    

    希望这会有所帮助。信息来源:here

    【讨论】:

      猜你喜欢
      • 2020-07-30
      • 1970-01-01
      • 2021-09-25
      • 2015-05-29
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      • 2015-11-02
      • 1970-01-01
      相关资源
      最近更新 更多