簡體   English   中英

Cookies…Http POST無法正常工作,為什么?

[英]Cookies…Http POST doesn't correctly work, why?

可能這通常是個問題,但我無法解決。 我需要自動填寫從服務器提供的Web表單上的某些字段。 我使用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 =”字段摘要“ name =” field_summary“ size =” 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錯誤:請求錯誤
    表單令牌丟失或無效。 您是否啟用了Cookie?

怎么了?

    作為對GET請求的響應,我得到以下信息:
      響應代碼= 200 trac_auth = 38144ec2830678183afebf0b14c51721
      trac_form_token = e9648f17987551b8f97e1953

可能我需要更改此:

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

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

我認為上面的鏈接將幫助您解決這個問題。 它也提供視頻..希望您的查詢能夠得到解決

http://www.formautofill.com/

該站點將為您提供一個可以自動填寫表格的軟件。 由Microsoft提供。

好。 下面的鏈接是關於post方法。 我認為這可能對您有幫助。

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

給我答復。 如果沒有。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM