简体   繁体   English

HTTPS登录? Java的

[英]HTTPS login? Java

I am trying to write a segment of code that will attempt to login to twitter to check if user name is correct and then return if it is true or false. 我正在尝试编写一段代码,尝试登录到twitter以检查用户名是否正确,然后返回true(如果为true或false)。 It is not working due to the fact that if I put in the correct user name and pass that I still get the sign in page. 它没有用,因为如果我输入正确的用户名并通过,我仍然会登录页面。

public class httpConnect {
//Variables

public Boolean correctCredentials(String site, String user, String pass) {
    String data = connect(site, user, pass);
    char[] charArray = data.toCharArray();
    try {
        File log = new File("C:\\Users\\________\\Desktop" + "\\" + "log.txt");
        if (log.exists()) {
            Scanner scan = new Scanner(log);
            while (scan.hasNextLine()) {
                String temp = scan.nextLine();
                temp = charArray.toString() + (char) 10 + (char) 10 + (char) 10 + temp;
                charArray = temp.toCharArray();
            }
        }
        BufferedWriter out = new BufferedWriter(new FileWriter("C:\\Users\\________\\Desktop" + "\\" + "log.txt", true));  //bufferedWriter for speed
        out.append((char) 10);
        for (int x = 0; x < charArray.length; x++) {
            out.append(charArray[x]);
        }
        out.flush();
        out.close();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e + "\nSorry, could not write File"); //gives error if path does not exist
    }
    return false;
}

public String connect(String site, String user, String pass) {
    StringBuilder response = new StringBuilder();
    ;
    try {
        String encodedUser = URLEncoder.encode(user, "UTF-8");
        String encodedPass = URLEncoder.encode(pass, "UTF-8");
        /*
         * commit - LOGIN_ACTION_NAME
         * session[username_or_email] - LOGIN_USER_NAME_PARAMETER_NAME
         * session[password] - LOGIN_PASSWORD_PARAMETER_NAME
         */
        String content = "login=" + "commit" + " amp;amp;"
                + "session[username_or_email]" + "=" + encodedUser + "amp;amp;"
                + "session[password]" + "=" + encodedPass;
        HttpsURLConnection urlConnect = (HttpsURLConnection) (new URL(site).openConnection());
        urlConnect.setDoInput(true);
        urlConnect.setDoOutput(true);
        urlConnect.setRequestProperty("Content-Type", "text/html; charset=utf-8");
        urlConnect.setRequestMethod("POST");
        DataOutputStream dataOutputStream = new DataOutputStream(urlConnect.getOutputStream());
        dataOutputStream.writeBytes(content);
        dataOutputStream.flush();
        dataOutputStream.close();
        BufferedReader buf = new BufferedReader(new InputStreamReader(urlConnect.getInputStream()));
        String responseLine = "";
        while ((responseLine = buf.readLine()) != null) {
            response.append(responseLine);
        }
        System.out.println(response);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return response.toString();
}
}

So basically what I am going to do is attempt login and then scan page to see if title says "Twitter \\ Home" and if it does then I know I logged in correctly. 所以基本上我要做的是尝试登录,然后扫描页面,看看标题是否说“Twitter \\ Home”,如果确实如此,我知道我正确登录。 I have not gotten that far, but when I browse it myself it does not work. 我没有那么远,但是当我自己浏览它时它不起作用。

Btw I don't want to use Twitter4j because it refuses to return a Boolean. 顺便说一句我不想使用Twitter4j,因为它拒绝返回布尔值。


It also may be that I do not have the right page to attempt to log into. 也可能是我没有正确的页面尝试登录。 How would I find it? 我怎么找到它? What would it be for twitter? 什么是推特?

Have you had a look at using the official Twitter API (http://dev.twitter.com/doc) to do this? 你有没有看过使用官方Twitter API(http://dev.twitter.com/doc)来做这件事?

Looking at your code suggests that you're mimicking the browser's authentication requests, but this probably misses out some information such as cookies, request tokens and whatever else they might use in their login page. 查看您的代码表明您正在模仿浏览器的身份验证请求,但这可能会遗漏一些信息,例如Cookie,请求令牌以及他们在登录页面中可能使用的任何其他信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM