简体   繁体   中英

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. 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. 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.


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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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