Well, I attempted to make a program that logs into the Neopets site (www.neopets.com) using the username and password given. It compiles with no errors, but after it does everything the person still isn't logged in. It gives the message I told it to if it still says you need to log in. I tested the URLs manually via Firefox. I don't know if this visits the URLs, or just connects to the host. Please help! Fixing it and comparing would be helpful! Thanks.
My Code
My Code
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class NeoLogin implements ActionListener {
private JFrame window = new JFrame("Nets's Neopets Login");
private JLabel usernameLabel = new JLabel("Username: ");
private JTextField usernameField = new JTextField("");
private JLabel passwordLabel = new JLabel("Password: ");
private JTextField passwordField = new JTextField("");
private JButton login = new JButton("Login");
String username;
String password;
public NeoLogin() {
window.setSize(300, 300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(5,5));
window.add(usernameLabel);
window.add(usernameField);
window.add(passwordLabel);
window.add(passwordField);
window.add(login);
login.addActionListener(this);
window.setVisible(true);
}
public void actionPerformed(ActionEvent a) {
BufferedReader usernameStream;
BufferedReader passwordStream;
BufferedReader loginStream;
String usernameLoginResponse;
String passwordLoginResponse;
String loggedInResponse;
login.setEnabled(false);
username = usernameField.getText();
password = passwordField.getText();
if (username.equals("")) {
JOptionPane.showMessageDialog(null, "You forgot to fill out the username field!");
login.setEnabled(true);
}
if (password.equals("")) {
JOptionPane.showMessageDialog(null, "You forgot to fill out the password field!");
login.setEnabled(true);
}
System.out.println("Going to the main site...");
try {
URL neopetsMain = new URL("http://neopets.com/index.phtml");
URLConnection neopetsMainConnection = neopetsMain.openConnection();
System.out.println("At the main site, waiting 5 seconds...");
Thread.sleep(5000);
System.out.println("Going to the login page...");
URL neopetsLoginPage = new URL("http://neopets.com/loginpage.phtml");
URLConnection neopetsLoginConnection = neopetsLoginPage.openConnection();
System.out.println("At the login page, waiting 2 and a half seconds...");
Thread.sleep(2500);
System.out.println("Typing in username and going to the password page...");
URL neopetsUsernameLogin = new URL("http://neopets.com/login.phtml?username="+username);
usernameStream = new BufferedReader(new InputStreamReader(neopetsUsernameLogin.openStream()));
while ((usernameLoginResponse = usernameStream.readLine()) != null) {
if (usernameLoginResponse.contains("Incorrect username in cookie.")) {
JOptionPane.showMessageDialog(null, "Username is invalid!");
login.setEnabled(true);
usernameStream.close();
break;
}
}
System.out.println("About to login, waiting 2 and a half seconds...");
Thread.sleep(2500);
URL neopetsPasswordLogin = new URL("http://neopets.com/login.phtml?username="+username+"&password="+password);
passwordStream = new BufferedReader(new InputStreamReader(neopetsPasswordLogin.openStream()));
while ((passwordLoginResponse = passwordStream.readLine()) != null) {
if (passwordLoginResponse.contains("That username/password combination is invalid.")) {
JOptionPane.showMessageDialog(null, "Invalid username or password!");
login.setEnabled(true);
passwordStream.close();
break;
}
}
loginStream = new BufferedReader(new InputStreamReader(neopetsMain.openStream()));
while ((loggedInResponse = loginStream.readLine()) != null) {
if (loggedInResponse.contains("Welcome, "+username)) {
JOptionPane.showMessageDialog(null, "Successfully logged in!");
login.setEnabled(true);
break;
}
if (loggedInResponse.contains("Login to Neopets!")) {
System.out.println("Error with logging in, fix your cookies.");
JOptionPane.showMessageDialog(null, "Odd error with logging in! Try fixing your cookies!");
login.setEnabled(true);
loginStream.close();
break;
}
}
} catch (InterruptedException e) {
System.out.println("Waiting was interrupted.");
System.out.println("InterruptedException:"+e);
JOptionPane.showMessageDialog(null, "Interrupted when trying to act like a human by waiting a few seconds.");
login.setEnabled(true);
} catch (IOException e) {
System.out.println("Unable to connect.");
System.out.println("IOException: "+e);
JOptionPane.showMessageDialog(null, "Unable to connect to the designated page on Neopets.");
login.setEnabled(true);
}
}
public static void main(String args[]) {
System.out.println("Welcome to the Nets's Neopets Login debugger.");
new NeoLogin();
}
}
