0

I recently started learning programming with java and as I was playing league of legends for a few years now, I was wondering if you can make some kind of program, which can suggest champions to pick during championselect based on ally champions (teamcomps) and enemy champions (matchups, counter teamcomps...). Trying that I had trouble getting the data of championselect from riot. Here's what I tried:

public static void HTTP_Request(String host, int port, String path,
    String method, String data) throws UnknownHostException, IOException {

    System.out.println("open Socket: host=" + host + " Port=" + port);
    Socket socket = new Socket(host, port);

    OutputStream out = socket.getOutputStream();
    InputStream in = socket.getInputStream();

    if (method.equals("GET")) {
        String request;
        if (data != null && data.length() > 0) {
            request = "GET " + path + "?" + data + " HTTP/1.0\r\n"
            + "Accept: */*\r\n" + "Host: "+host+"\r\n"
            + "Connection: Close\r\n\r\n";
        } else { 
            request = "GET " + path +  " HTTP/1.0\r\n"
            + "Accept: */*\r\n" + "Host: "+host+"\r\n"
            + "Connection: Close\r\n\r\n";                }

            System.out.println("Request:\n" + request.toString());

            out.write(request.getBytes());
            out.flush();
        } else if (method.equals("POST")) { 

            String request = "POST " + path + " HTTP/1.0\r\n" + "Accept: */*\r\n"
            + "Host: " + host + "\r\n"
            + "Content-Type: application/x-www-form-urlencoded\r\n"
            + "Content-Length: " + data.length() + "\r\n\r\n" + data;


            System.out.println("Client fragt:\n" + request.toString());

            out.write(request.getBytes());
            out.flush();
        } else {
            System.out.println("Invalid HTTP method");
            socket.close();
            return;
        }

        StringBuffer response=new StringBuffer();
        byte[] buffer = new byte[4096];
        int bytes_read;

        while ((bytes_read = in.read(buffer, 0, 4096)) != -1) { 
            for(int i = 0; i < bytes_read; i++)
                response.append((char)buffer[i]);
        }

        System.out.println("Response:\n" + response.toString());

        if (response.substring(response.indexOf(" ") + 1,
            response.indexOf(" ") + 4).equals("200")) {
            File file = new File("index.html");
        PrintWriter printWriter = new PrintWriter(file);
        printWriter.println(response.substring(response.indexOf("\r\n\r\n") + 4));
        printWriter.close();
    } else
        System.out.println("HTTP request failed");

    socket.close();
}

public static void main(String[] args) throws Exception {

    String host = "127.0.0.1";
    int port = 2999;
    String path = "/liveclientdata/allgamedata";
    String method = "GET";
    String data = "";

    System.out.println(method + " https:\\" + host + ":" + port + path);
    try {
        HTTP_Request(host, port, path, method, data);
    } catch (UnknownHostException e1) {
        System.out.println("UnknownHostException");
        e1.printStackTrace();
    } catch (IOException e1) {
        System.out.println("IOException");
        System.out.println(e1.getMessage());
        e1.printStackTrace();
    }
}

And this is what I get executing it:

IOException
Connection refused: connect
java.net.ConnectException: Connection refused: connect
EricSchaefer
  • 22,338
  • 20
  • 63
  • 99
Insight
  • 21
  • 1

0 Answers0