samedi 13 août 2016

Logging into online servers through Java (Specifically Factorio)

I've been trying to make a launcher for Factorio the past few hours, but I've run into an issue. I'm trying to connect to the website and login to make sure that the user has actually purchased the game, but I can't figure out how.

Here's my connection class file:

I've tried printing out the website data to see if I can get anything different than the default HTML

package ultima.launcher;

import java.io.*;
import java.net.*;

import javax.swing.JFrame;

public class URLHandler {

// Variables to hold the URL object and its connection to that URL.
private static URL URLObj;
private static URLConnection connect;

public static void login(String username, String password, JFrame frame) {
    try {
        // Establish a URL and open a connection to it. Set it to output mode.
        URLObj = new URL("http://ift.tt/2bt8wa3");
        connect = URLObj.openConnection();
        connect.setDoOutput(true);  
    }
    catch (MalformedURLException ex) {
        System.out.println("The URL specified was unable to be parsed or uses an invalid protocol. Please try again.");
    }
    catch (Exception ex) {
        System.out.println("An exception occurred. " + ex.getMessage());           System.exit(1);
    }


    try {
        // Create a buffered writer to the URLConnection's output stream and write our forms parameters.
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connect.getOutputStream()));
        writer.write("username_or_email="+username+"&password="+password);
        writer.close();

        // Now establish a buffered reader to read the URLConnection's input stream.
        BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));

        String lineRead = "";

        // Read all available lines of data from the URL and print them to screen.
        while ((lineRead = reader.readLine()) != null) {
            System.out.println(lineRead);
        }

        reader.close();
    }
    catch (Exception ex) {
        System.out.println("There was an error reading or writing to the URL: " + ex.getMessage());
    }
}

}




Aucun commentaire:

Enregistrer un commentaire