Cookies...Http POST doesn't correctly work, why?

Probably this is often problem, but I can't solve it. I need to automaticallly fill out some fields on the web form, which is given from server. I use Apache HttpClient to make more easy my life) By now, one can consider my steps to acheive the aim:
    1. I have not certificate to http://trac.edgewall.org/ so I download this software and install locally and at finish I'll have to create NewTicket.
    2. I locally use Trac without any SSL(SSL tunnel). (It's not difficult to change my program to be able to use HTTPS).
    3. By now, I can authenticate and perform GET request, but I can't perform POST request
    4. For instance: I perform GET request to the http://localhost:8000/tracenvir/newticket . This (~/newticket) page looks as following: http://s04.radikal.ru/i177/0912/cb/d43971cebc02.png And as response I have : (part of it)
    "input type="text" id="field-summary" name="field_summary" size="70" "
    "textarea id="field-description" name="field_description" class="wikitext" rows="10" cols="68"/textarea"'
    5. So, I write this:

        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();
        }
    }
And I have this:
    400 Error: Bad Request
    Missing or invalid form token. Do you have cookies enabled?
What's wrong?
    As response on GET request I get this:
      response code = 200 trac_auth=38144ec2830678183afebf0b14c51721
      trac_form_token=e9648f17987551b8f97e1953
Probably I nedd change this:
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

以上就是Cookies...Http POST doesn't correctly work, why?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏