如何通过java填写HTTP表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1383086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:25:13  来源:igfitidea点击:

How to fill HTTP forms through java?

javajavascripthttpresponseforms

提问by Yatendra Goel

I want to fill a text field of a HTTP form through java and then want to click on the submit button through java so as to get the page source of the document returned after submitting the form. I can do this by sending HTTP request directly but I don't to this in this way.

我想通过java填写一个HTTP表单的文本字段,然后想通过java点击提交按钮,以获取提交表单后返回的文档的页面源。我可以通过直接发送 HTTP 请求来做到这一点,但我不这样做。

回答by Kibbee

You would probably need to write a Java Applet, as the only other way than sending a direct request would be to have it interface with the browser.

您可能需要编写一个 Java Applet,因为除了发送直接请求之外,唯一的其他方法是让它与浏览器连接。

Of course, for this to work, you would have to embed the applet in the page. If you don't control the page, this can't be done. If you do control the page, you might as well be using Javascript, instead of trying to get a Java Applet to do it, which would be much more cumbersome and difficult.

当然,要使其正常工作,您必须在页面中嵌入小程序。如果您不控制页面,则无法完成此操作。如果您确实控制了页面,那么您还不如使用 Javascript,而不是尝试让 Java Applet 来执行此操作,这会更加麻烦和困难。

Just to clarify, what is the problem you are having creating an HTTP Request and why do you want to use a different method?

澄清一下,您在创建 HTTP 请求时遇到什么问题,为什么要使用不同的方法?

回答by Geo

I usually do it using HtmlUnit. They have an example on their page :

我通常使用HtmlUnit 来完成。他们的页面上有一个示例:

@Test
public void submittingForm() throws Exception {
    final WebClient webClient = new WebClient();

    // Get the first page
    final HtmlPage page1 = webClient.getPage("http://some_url");

    // Get the form that we are dealing with and within that form, 
    // find the submit button and the field that we want to change.
    final HtmlForm form = page1.getFormByName("myform");

    final HtmlSubmitInput button = form.getInputByName("submitbutton");
    final HtmlTextInput textField = form.getInputByName("userid");

    // Change the value of the text field
    textField.setValueAttribute("root");

    // Now submit the form by clicking the button and get back the second page.
    final HtmlPage page2 = button.click();
}

And you can read more here.

你可以在这里阅读更多

回答by Brian Agnew

If you don't want to talk HTTP directly (why?), then take a look at Watij.

如果你不想直接谈论 HTTP(为什么?),那么看看Watij

It allows you to invoke a browser (IE) as a COM control within your Java process, navigate through page elements by using their document ids etc., fill in forms and press buttons. Because it's running a browser, Javascript will run as normal (like if you were doing this manually).

它允许您在 Java 进程中调用浏览器 (IE) 作为 COM 控件,使用页面元素的文档 ID 等导航页面元素,填写表单并按下按钮。因为它运行的是浏览器,Javascript 将正常运行(就像您手动执行此操作一样)。