Jsoup, get value before executing POST form

Here is the code I use to submit the form:

Connection.Response res = Jsoup.connect("http://example.com")
    .data("id", "myID")
    .data("username", "myUsername")
    .data("code", "MyAuthcode") // get the value of Auth code from page element
    .method(Method.POST).execute();

To submit this form successfully, the field with [name = "code"] must have a value.

The value can be found on the page in another element . How can I get the value of an element using the same connection before submitting the form as shown above?

I need to use the value from the element to successfully fill out the form.

+4
source share
2 answers

Jsoup actually opens a new HTTP connection for each request, so your job is not entirely possible, but you can get closer:

// Define where to connect (doesn't actually connect)
Connection connection = Jsoup.connect("http://example.com");

// Connect to the server and get the page
Document doc = connection.get();

// Extract the value from the page
String authCode = doc.select("input[name='code']").val();

// Add the required data to the request
connection.data("id", "myID")
    .data("username", "myUsername")
    .data("code", authCode);

// Connect to the server and do a post
Connection.Response response = connection.method(Method.POST).execute();

HTTP ( GET POST), .

, (, Apache - HTTPClient), jsoup , , Jsoup.parse().

+1

GET- POST, . Captcha jsoup

0

All Articles