How to create a robot using Java requests?

Asked

Viewed 2,344 times

2

I have been interested in making robots to perform small repetitive and boring tasks. I use a class of Java called Robot. However, I think that the way I am doing it is not the most appropriate, because it is a very handmade robot that works with mouse and keyboard events. I would like to develop one based on server requests (GET and POST for example), but I don’t know which class or which features and techniques to use.

Here’s an example code for "handmade robots":

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class Robo {

    public static void main(String[] args) throws AWTException, IOException,
            URISyntaxException {
        Robot robot = new Robot();

        int login[] = {
            KeyEvent.VK_L,
            KeyEvent.VK_O,
            KeyEvent.VK_G,
            KeyEvent.VK_I,
            KeyEvent.VK_N,
        };

        int senha[] = {
            KeyEvent.VK_1,
            KeyEvent.VK_2,
            KeyEvent.VK_3,
            KeyEvent.VK_4,
            KeyEvent.VK_5,
            KeyEvent.VK_6,
            KeyEvent.VK_7,
            KeyEvent.VK_8,
            KeyEvent.VK_9,
            KeyEvent.VK_0};

        java.awt.Desktop.getDesktop().browse(
                new URI("https://registro.br/cgi-bin/nicbr/login"));
        robot.delay(3000);

        //Login
        for (int i = 0; i < login.length; i++) {
            robot.keyPress(login[i]);
            robot.delay(100);
            robot.keyRelease(login[i]);
            robot.delay(300);
        }
        robot.delay(200);
        robot.keyPress(KeyEvent.VK_TAB);
        robot.delay(100);
        robot.keyRelease(KeyEvent.VK_TAB);
        robot.delay(1000);

        //Senha
        for (int j = 0; j < senha.length; j++) {
            robot.keyPress(senha[j]);
            robot.delay(100);
            robot.keyRelease(senha[j]);
            robot.delay(300);
        }
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.delay(100);
        robot.keyRelease(KeyEvent.VK_ENTER);
        robot.delay(1000);

        try {
            for (int i = 0, j = 1000; i < 1000; i++, j--) {
                robot.mouseMove(i, j);
                robot.delay(5);
            }
            robot.delay(500);
            //robot.mousePress(InputEvent.BUTTON1_MASK);
            //robot.mouseRelease(InputEvent.BUTTON1_MASK);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        robot.delay(200);
    }
}

2 answers

2


To make requests HTTP in Java, you need to use the HTTP connection class provided by jdk. An example is the Httpurlconnection. It is still possible to use some apache packages intended for this purpose.

Finally, your question is a little broad, but depending on what you want to do, java may not be the ideal language for the type of task you want to do, perhaps a scripting language, as , be better for your purposes, since your intention is to create scripts to perform boring tasks.

  • Perfect. I will try to study Httpurlconnection. This is exactly what I wanted.

1

You can automate tasks within the computer using some tools.

I know these two very good tools for automating tasks, although they use their own languages.

  1. autohotkey
  2. Autoit ( recommend )

If you need something a little more specific you can use the library Selenium

If you need an even more specific control that works with requests (GET, POST, PUT etc) and some interpretation of html better use the library Httpcommons.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.