java程序在浏览器中打开一个网页并将一些数据发布到打开的页面中

时间:2014-03-10 04:32:08

标签: java post browser

我遇到了问题。问题是我想编写一个java代码,它在默认浏览器中打开一个网页,并将我的数据发布到打开的网页中。

请任何人指导我这样做。我对此没有任何线索。

非常感谢您的帮助。提前致谢

3 个答案:

答案 0 :(得分:3)

这将打开默认浏览器

java.awt.Desktop.getDesktop().browse(theURI);

查看this,了解如何在网址中传递参数

答案 1 :(得分:1)

您可以编写一个带有表单的HTML文件自动提交给服务器并使用这些东西打开它:

File htmlFile = new File(url);
Desktop.getDesktop().browse(htmlFile.toURI());

答案 2 :(得分:0)

您需要一个网络驱动程序。看Selenium

我正在粘贴Getting started with Selenium

中的一些代码
package org.openqa.selenium.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class Example  {
    public static void main(String[] args) {
    // Create a new instance of the html unit driver
    // Notice that the remainder of the code relies on the interface, 
    // not the implementation.
    WebDriver driver = new HtmlUnitDriver();

    // And now use this to visit Google
    driver.get("http://www.google.com");

    // Find the text input element by its name
    WebElement element = driver.findElement(By.name("q"));

    // Enter something to search for
    element.sendKeys("Cheese!");

    // Now submit the form. WebDriver will find the form for us from the element
    element.submit();

    // Check the title of the page
    System.out.println("Page title is: " + driver.getTitle());

    driver.quit();
}
}

此示例显示如何打开google.com然后键入一些搜索文本并单击搜索按钮。