0

可能这经常是问题,但我无法解决。我需要自动填写从服务器提供的网络表单上的一些字段。我使用 Apache HttpClient 让我的生活更轻松)现在,可以考虑我的步骤来实现目标:

    1. 我没有 http://trac.edgewall.org/ 的证书,所以我下载了这个软件并在本地安装,完成后我必须创建 NewTicket。
    2. 我在本地使用没有任何 SSL(SSL 隧道)的 Trac。(将我的程序更改为能够使用 HTTPS 并不难)。
    3. 到目前为止,我可以认证并执行GET请求,但我不能执行POST请求

    4. 例如:我对http://localhost:8000/tracenvir/newticket执行 GET 请求。这个(~/newticket)页面如下所示: http ://s04.radikal.ru/i177/0912/cb/d43971cebc02.png 作为回应,我有:(部分)
    "输入类型="文本" id="字段摘要" 名称="字段摘要" 大小="70" "

    "textarea id="field-description" name="field_description" class="wikitext" rows="10" cols="68"/textarea"'
    5. 所以,我写这个:

        int status = 0;
        int cookLength=0;
        Cookie[] cookies = null;

        HttpClient client = new HttpClient();
        client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        HttpState initialState = new HttpState();
        client.setState(initialState);

        //**********//
        //**Log in**//
        //**********//

        GetMethod login = new GetMethod("http://localhost:8000/tracenvir/login");
        client.getState().setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("rauch", "qwert"));
        login.setDoAuthentication(true);
        System.setProperty("javax.net.ssl.trustStore", "/home/rauch/NetBeansProjects/jssecacerts");

        try {
            status = client.executeMethod(login);

        System.out.println("response code = "+status);
        cookies = client.getState().getCookies();
        cookLength = cookies.length;

        for(int i=0;i (less than) cookLength;i++) {
            System.out.println(cookies[i].toString());
        }
        login.releaseConnection();
        } catch(IOException ex) {
            ex.printStackTrace();
        }

        //*********************//
        //**Create New Ticket**//
        //*********************//

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

        NameValuePair[] data = {
            new NameValuePair("field-summary","second error"),
            new NameValuePair("field-descryption","Some stupid descryption..."),
            new NameValuePair("field-type","defect"),
            new NameValuePair("field-priority","major"),
            new NameValuePair("field-version","1.0"),
            new NameValuePair("field-owner","moscow server"),
            new NameValuePair("submit","create ticket"),
        };


        //post.setRequestBody(data);
        post.addParameters(data);
        post.addRequestHeader("Referer","http://localhost:8000/tracenvir/login");
        for(int i=0;i (less than) cookLength;i++) {
            initialState.addCookie(cookies[i]);
        }
        client.setState(initialState);


        try {
            status = client.executeMethod(post);

        System.out.println("response code = "+status);

        byte[] buf = new byte[10];
        int r=0;
        BufferedInputStream is = new BufferedInputStream(post.getResponseBodyAsStream());

        while((r = is.read(buf)) > 0) {
            System.out.write(buf, 0, r);
        }
        post.releaseConnection();
        } catch(IOException ex) {
            ex.printStackTrace();
        }
    }

我有这个:

    400 错误:错误请求
    缺少或无效的表单令牌。你有启用cookies吗?

怎么了?

    作为对 GET 请求的响应,我得到以下信息:
      响应代码 = 200 trac_auth=38144ec2830678183afebf0b14c51721
      trac_form_token=e9648f17987551b8f97e1953

可能我需要改变这个:

    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
4

2 回答 2

1

http://www.google.com/support/toolbar/bin/answer.py?hl=en&answer=47972

我认为上面的链接将帮助你解决这个问题。它还提供视频..我希望您的问题能够得到解决

http://www.formautofill.com/

该站点将为您提供一个软件,该软件将提供自动填表功能。微软提供。

于 2009-12-11T19:06:39.083 回答
0

行。以下链接是关于 post 方法的。我想它可能对你有帮助。

http://www.willmaster.com/library/manage-forms/automatic-form-submission-to-a-cgi-program.php

给我答复。如果没有。

于 2009-12-11T20:38:13.610 回答