开发者

How to use WWW::Mechanize to submit a form which isn't there in HTML?

I'开发者_开发百科m trying to do a script in WWW::Mechanize that will build a job in Hudson. The script is this:

$mech->post("$hudson_url/view/All/job/$job_name/build?delay=0sec", \%job_arguments);

The problem is that the Hudson log complains that it expected a form submission. The following was extracted from the Hudson logs:

Caused by: java.lang.Error: This page expects a form submission
    at org.kohsuke.stapler.RequestImpl.getSubmittedForm(RequestImpl.java:769)
    at hudson.model.ParametersDefinitionProperty._doBuild(ParametersDefinitionProperty.java:116)
    at hudson.model.AbstractProject.doBuild(AbstractProject.java:1531)
    ... 69 more

The problem is, the build in Hudson is parameterized, and so Hudson aparently generates the form using JavaScript. Eventually, Hudson will call this URL. How can I make this request so that Hudson thinks I'm posting a form?


UPDATE: I have request data as shown in Chrome. This is what works:

Request URL:<url>/hudson/view/All/job/ReleaseThis/build?delay=0sec
Request Method:POST
Status Code:302 Moved Temporarily
Request Headers
Accept:application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:935
Content-Type:application/x-www-form-urlencoded
Cookie:screenResolution=1920x1080; JSESSIONID=B3F99F6C376318AB6331AE6F27917D52; screenResolution=1920x1080; __utmz=250097027.1291302888.1.1.utmcsr=...|utmccn=(referral)|utmcmd=referral|utmcct=/sig/Home.action; __utma=250097027.1666115034.1291302888.1291302888.1291302888.1
Host:<hudson_url>
Origin:<huson_url>
Referer:https://<hudson_url>/hudson/view/All/job/ReleaseThis/build?delay=0sec
User-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.84 Safari/534.13
Query String Parameters
delay:0sec
Form Data
name:URL
value:https://<url>
value:1.12-SNAPSHOT
name:RELEASE_VERSION
value:1.12-TEST16
name:UPDATE_MASTER_POM
name:PROPERTY_NAME
value:
name:DEV_VERSION
value:
name:STABLE_VERSION
value:
json:{"parameter": [{"name": "URL", "value": <url>}, {"name": "SNAPSHOT_VERSION", "value": "1.12-SNAPSHOT"}, {"name": "RELEASE_VERSION", "value": "1.12-TEST16"}, {"name": "UPDATE_MASTER_POM", "value": false}, {"name": "PROPERTY_NAME", "value": ""}, {"name": "DEV_VERSION", "value": ""}, {"name": "STABLE_VERSION", "value": ""}]}
Submit:Build
Response Headers
Connection:close
Content-Length:0
Content-Type:text/plain; charset=UTF-8
Date:Fri, 04 Feb 2011 13:33:27 GMT
Location:https://<hudson.url>/hudson/view/All/job/ReleaseThis/
Server:Apache-Coyote/1.1
Via:1.1 <hudson.url>:8890 (Apache/2.2.3)


There are two possibilities, one is that there is a javascript generated random string as one of the fields, in order to protect from CSRF attacks, and bot autosubmission. In this case, you will need to reverse the javascript algo to parse and post the string, or use WWW::Scripter, which however I haven't tested out.

Second, and I belive more likely option is that the script checks fore referer, in this case, just set the referer via editing the header, and you might be good to go.

One more thing to watch is that you have exactly the same headers and cookies in the browser and perl script requests; thus you might first get the page with the form, to get the cookies set, or set them yourself in the script.

There is not a single measure that can't be bypassed, for you to use script, you just have to find which one is employed by testing...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜