How to create a robot using requests in Java?

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 the way I'm doing is not the most appropriate, because it's a pretty handy 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 do not know which class or what resources and techniques to use.

Here is an example code of "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);
    }
}
    
asked by anonymous 14.03.2014 / 18:22

2 answers

2

To make requests HTTP in Java, you must use the HTTP connection class provided by jdk . An example is the HttpURLConnection . You can still use some apache packages intended for this purpose.

To finish, your question is a little broad, but depending on what you want to do, maybe java is not the ideal language for the type of task you want to do, maybe a Language script , like is best for your purposes, as you intend to create scripts to accomplish boring tasks.

    
14.03.2014 / 18:37
1

You can automate tasks within the computer using some tools.

I know these two very good tools to automate tasks, even though they use their own languages.

  • autohotkey
  • autoit (recommend)
  • If you need something a bit 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 the html better use the library HttpCommons .

        
    14.03.2014 / 19:47