如何以编程方式使用JAVA填写Web表单?

时间:2009-12-14 11:49:25

标签: java cookies webforms automation

我只需要帮助您使用以编程方式填写网络表单。我使用Apache HttpClient 4.0.1。表格如下:
Web Form Example

它的HTML代码如下所示:

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" <ol>Some tags</ol> <ol> 
Do not show the ticket (pre)view when the user first comes to the "New Ticket" page.

Wait until they hit preview. Ticket Box (ticket fields along with description)</ol> <ol>form action="/tracenvir/newticket" method="post" id="propertyform"--div--input type="hidden" name="__FORM_TOKEN" value="dff95a43ddec5a653627d2c0"</ol>

<ol>input type="text" id="field-summary" name="field_summary" size="70"</ol> <ol>textarea id="field-description" name="field_description" class="wikitext" rows="10" cols="68"</ol> <ol>input type="hidden" name="field_status" value="new" </ol> <ol>

      input type="submit" name="preview" value="Preview" </ol> <ol>

      input type="submit" name="submit" value="Create ticket"</ol>

还有很多其他标签。这是我的java代码:

DefaultHttpClient client = new DefaultHttpClient();

    client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH);

    client.setCookieStore(new BasicCookieStore());


    //**LOG IN**//


    //System.setProperty("javax.net.ssl.trustStore", "/home/rauch/NetBeansProjects/jssecacerts");

    HttpGet login = new HttpGet("http://localhost:8000/tracenvir/login");

    client.getCredentialsProvider().setCredentials(AuthScope.ANY,

            new UsernamePasswordCredentials("rauch", "qwerty"));

然后正确登录......我得到200 OK,一切都很顺利。

    //**POST NewTicket**

    HttpPost post = new HttpPost("http://localhost:8000/tracenvir/newticket");

    List<NameValuePair> formparams = new ArrayList<NameValuePair>();

        formparams.add(new BasicNameValuePair("__FORM_TOKEN", cookies.get(1).getValue()));

        formparams.add(new BasicNameValuePair("field_summary", "Someerror"));

        formparams.add(new BasicNameValuePair("field_descryption","AnyDescryption"));

        formparams.add(new BasicNameValuePair("field_type", "defect"));

        formparams.add(new BasicNameValuePair("field_priority", "major"));

        formparams.add(new BasicNameValuePair("field_milestone", "milestone3"));

        formparams.add(new BasicNameValuePair("field_component", "comp2"));

        formparams.add(new BasicNameValuePair("field_version", "1.0"));

        formparams.add(new BasicNameValuePair("field_keywords", ""));

        formparams.add(new BasicNameValuePair("field_cc", ""));

        formparams.add(new BasicNameValuePair("field_owner", "java server"));

        formparams.add(new BasicNameValuePair("field_status", "new"));

        formparams.add(new BasicNameValuePair("submit", "Create ticket"));

    try {

        UrlEncodedFormEntity entity;

        entity = new UrlEncodedFormEntity(formparams, "UTF-8");

        post.setEntity(entity);

        post.addHeader("Referer","http://localhost:8000/tracenvir/newticket");


        HttpResponse response = client.execute(post);


        System.out.println("Create ticket: "+response.getStatusLine());


        client.getConnectionManager().shutdown();


    } catch(UnsupportedEncodingException ex) {

        ex.printStackTrace();

    } catch(IOException ex) {

        ex.printStackTrace();

    }

服务器响应 HTTP / 1.0 200 OK 。但是这个“New Ticket”没有出现在ViewTickets网页上。如果我使用普通的Web浏览器执行相同操作,请填写字段并按下“创建票证”按钮一切正常,我可以在ViewTickets网页上看到此NewTicket。这是浏览器生成请求的内容:

__FORM_TOKEN=0856803edd721d8b9592231d&field_summary=fuckingStatusField&field_description=mmm+status&field_type=defect&field_priority=major&field_milestone=milestone1&field_component=component1&field_version=2.0&field_keywords=&field_cc=&field_owner=ubuntu-server&field_status=new&submit=Create+ticket)</ol>

为什么不起作用?默认情况下,我不应该使用这个:

formparams.add(new BasicNameValuePair("__FORM_TOKEN", cookies.get(1).getValue()));

DefaultHttpClient必须这样做,但事实并非如此。如果我对此声明发表评论,则服务器响应 HTTP / 1.0 400错误请求

如何正确填写此表单?

我试图模仿浏览器:首先是GET / newticket页面,然后用浏览器生成的Headers生成POST请求.....但编程方式我从服务器上有200 OK,但这个NewTicket没有出现在Tickets列表中

2 个答案:

答案 0 :(得分:4)

Apache Http Components(或旧的HttpClient),Selenium,HtmlUnit - 取决于您的具体情况

答案 1 :(得分:4)

  1. 使用Wireshark等数据包捕获实用程序来监控http请求。

  2. 比较浏览器发送的内容与您的代码发送的内容。

  3. 相应地修改您的代码。