当前位置: 首页 > 知识库问答 >
问题:

如何通过java代码登录我的帐户

牛昱
2023-03-14

我试图写一个代码,其中我传递我现有的用户ID和密码,并希望当我执行代码时,它应该询问我的用户ID和密码,然后授权我访问主页。我试图这样做,但到目前为止,它要求用户ID和密码,并在输入凭据后,它向我显示登录(html)页面。

包装试验;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;



public class Api {

    private static final int MYTHREADS = 50;
    private static Scanner input;

    public static void main(String args[]) throws Exception {

        input = new Scanner (System.in);
        String user, pass;

        System.out.print("Enter your username:");
        user = input.nextLine();

        System.out.print("Enter your Password:");
        pass = input.nextLine();

        if (user.equals("tester") && (pass.equals("12345")))
        {
            System.out.println("welcome");
        }
        else {
            System.out.println("please try again!");
        }


        ExecutorService executor = Executors.newFixedThreadPool(MYTHREADS);
        String[] apiList = {"https://example.com", };

        for (int i = 0; i < apiList.length; i++) {

            String url = apiList[i];
            Runnable worker = new MyRunnable(url);
            executor.execute(worker);
        }
        executor.shutdown();
        // Wait until all threads are finish

        while (!executor.isTerminated()) {

        }
        System.out.println("\nFinished all threads");
    }

    public static class MyRunnable implements Runnable {
        private final String url;

        MyRunnable(String url) {
            this.url = url;
        }

/*错误作为实现java.lang.runnable.run*/

@覆盖公共无效运行(){

                String result = "";
                int code = 200;
                try {
                    URL siteURL = new URL(url);
                    HttpURLConnection connection = (HttpURLConnection) siteURL.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(3000);
                    connection.connect();
                    code = connection.getResponseCode();
                    connection.setRequestProperty("Content-Type", "application/json");
                    connection.setDoOutput(true);
                    String base64 = null;
                    connection.setRequestProperty("Authorization", "Basic" +base64);
                    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    String inputLine;
                    while ((inputLine = in.readLine()) != null)
                        System.out.println(inputLine);                  
                    if (code == 200) {
                        result = "-> Successful <-\t" + "Code: " + code;
                        ;
                    } else {
                        result = "-> Unsuccessful <-\t" + "Code: " + code;
                    }
                } catch (Exception e) {
                    result = "-> Sorry <-\t" + "Wrong domain - Exception: " + e.getMessage();

                }

                System.out.println(url + "\t\tStatus:" + result);

}
      } 
    }

共有1个答案

华永新
2023-03-14

我建议使用javax.ws.rs.client.客户端;

Client client = ClientBuilder.newClient();
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basicBuilder().credentials(login, password).build();
client.register(feature);

关于你的例子,我找不到你在哪里设置你的凭证请求

在您的情况下,也许一些验证器可以帮助:

Authenticator.setDefault (new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication ("username", "password".toCharArray());
}
});
 类似资料:
  • 嗨,我正在尝试对我的登录页面进行Microsoft单点登录身份验证。为此,我已经使用它创建了Azure多租户目录,并根据给定的链接https://www.c-sharpconer.com/article/azure-ad-authentication-for-mvc-web-application/创建了带有重定向URI的应用程序。成功地集成了代码,当运行时,我在输入用户名和密码后,我在错误下面得

  • 我想用java编写登录网站的代码。 下面是代码: 我曾在Facebook和Gmail上尝试过这段代码,但问题是它不起作用。 它一直告诉我cookies没有启用。(我使用了chrome浏览器,它们已启用)。 有没有其他方法可以实现这一点?

  • 问题内容: 我能够编写脚本以将我的电子邮件地址添加到email元素中。但是一旦通过脚本单击“下一步”,Google就会使用ajax将该电子邮件元素动态替换为密码元素。这是我遇到的问题,无法在该元素中提供密码并且无法登录。 网址:https://accounts.google.com/signin/v2/identifier?flowName = GlifWebSignIn&flowEntry =

  • 我想获取我的贝宝账户交易历史记录。与我登录时可以导出的数据相同。 那里的所有事务都不是通过RESTAPI本身生成的。 我读了一些帖子,上面说在这种情况下不能使用,例如“事务搜索”应用编程接口(https://developer.paypal.com/docs/api/transaction-search/v1/)。 我试过了,但没能成功。 在我获得访问令牌后: 我尝试调用/v1/reporting

  • 我面临的问题是: 用户首先使用“使用电子邮件注册”进行注册。之后,用户注册了一个谷歌帐户(有相同的电子邮件)。 现在之后,当用户尝试通过电子邮件和密码登录。我得到错误的密码是不正确的,我知道这是因为注册与电子邮件详细信息已经覆盖了谷歌帐户详细信息。 我要显示错误“电子邮件与谷歌帐户链接,请通过谷歌帐户登录”。

  • 本文向大家介绍ThinkPHP6通过Ucenter实现注册登录的示例代码,包括了ThinkPHP6通过Ucenter实现注册登录的示例代码的使用技巧和注意事项,需要的朋友参考一下 在和ucenter通信成功后,实现注册登录就非常简单了。简要记录如下。 1 新建一个controller用来继承Ucenter\Controller\UcController,并新建构造方法,用来实例化UcControl