gpt4 book ai didi

java - Jsoup,在执行表单POST之前获取值

转载 作者:搜寻专家 更新时间:2023-11-01 03:37:45 24 4
gpt4 key购买 nike

这是我用来提交表单的代码:

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();

要成功提交给定的表单,[name="code"] 的字段需要设置一个值。

可以在页面上的另一个元素中找到该值。在实际提交如上所示的表单之前,我究竟如何使用相同的连接获取元素的值?

我需要使用元素中的值,才能成功填写表单..

最佳答案

Jsoup 实际上为每个请求打开一个新的 HTTP 连接,所以你的要求不太可能,但你可以接近:

// 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() 返回的内容。

关于java - Jsoup,在执行表单POST之前获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25391685/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com