Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过java代码登录到我的帐户_Java - Fatal编程技术网

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

如何通过java代码登录到我的帐户,java,Java,我正在尝试编写一个代码,其中我正在传递我现有的用户id和密码,并希望当我执行代码时,它应该询问我的用户id和密码,然后授权我访问主页。我试过这样做,但到目前为止,它要求用户id和密码,输入凭据后,它会显示登录(html)页面 包装试验 import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java

我正在尝试编写一个代码,其中我正在传递我现有的用户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.io.BufferedReader;
导入java.io.InputStreamReader;
导入java.net.HttpURLConnection;
导入java.net.URL;
导入java.util.Scanner;
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
公共类Api{
私有静态最终整数=50;
专用静态扫描仪输入;
公共静态void main(字符串args[])引发异常{
输入=新扫描仪(System.in);
字符串用户,通过;
System.out.print(“输入您的用户名:”);
user=input.nextLine();
System.out.print(“输入密码:”);
pass=input.nextLine();
if(user.equals(“tester”)&(pass.equals(“12345”))
{
System.out.println(“欢迎”);
}
否则{
System.out.println(“请再试一次!”);
}
ExecutorService executor=Executors.newFixedThreadPool(MYTHREADS);
字符串[]apiList={”https://example.com", };
for(int i=0;i
/*实现java.lang.runnable.run时出错*/

@凌驾 公开募捐{

字符串结果=”;
int代码=200;
试一试{
URL siteURL=新URL(URL);
HttpURLConnection连接=(HttpURLConnection)siteURL.openConnection();
connection.setRequestMethod(“GET”);
connection.setConnectTimeout(3000);
connection.connect();
代码=连接。getResponseCode();
setRequestProperty(“内容类型”、“应用程序/json”);
connection.setDoOutput(真);
字符串base64=null;
connection.setRequestProperty(“授权”、“基本”+base64);
BufferedReader in=新的BufferedReader(新的InputStreamReader(connection.getInputStream());
字符串输入线;
而((inputLine=in.readLine())!=null)
系统输出打印LN(输入线);
如果(代码==200){

result=“->Successful unccessful Sorry我建议使用javax.ws.rs.client.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());
}
});

嗯……您设置了用户/密码,但您没有使用;-)没有更好的方法访问该API吗?请阅读“如何创建”。然后使用链接改进您的问题(不要通过评论添加更多信息)。否则,我们无法回答您的问题并帮助您。@Roland可能是一个更好的方法,我尝试了2-3,但没有得到理想的结果,所以我尝试坚持使用此方法。我没有使用我的用户/密码!请您解释一下。我看到您将
用户
密码
变量设置为
系统中给定的任何变量>,但您以后不会引用它。您的
base64
-变量是
null
,因此:您还没有使用用户/密码…使用char[]而不是字符串作为密码更安全。很抱歉,我尝试了您的建议,但没有成功。正如您提到的,您找不到“您将凭据设置为请求的位置”。我想要(尝试)它应该询问我的凭据,并直接授权我访问主页。如果我在其中犯了任何错误,请告诉我(我确信我犯了错误)。实际上,在执行请求之前,您必须设置您的登录名和密码。请尝试使用Authenticator
Authenticator.setDefault (new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication ("username", "password".toCharArray());
}
});