小编Wee*_*ees的帖子

访问受密码保护的URL

我正在尝试访问需要通过用户名和密码进行身份验证的网址.

建设时没有错误..我错过了什么吗?

这是第一个类,它设置将由网络代码使用的验证器

 public class AccessPasswordProtectedURLWithAuthenticator {

  public static void main(String[] args) {

    try {

        // when a proxy or an HTTP server asks for authentication.

        Authenticator.setDefault(new Authenticator(){});

        URL url = new URL("http://website.html");

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

    }

}

} …
Run Code Online (Sandbox Code Playgroud)

authentication android

9
推荐指数
1
解决办法
1108
查看次数

标签 统计

android ×1

authentication ×1