2013-11-04 42 views
2

嘗試從路由器網站獲取一些數據,但找不到POST請求頭。無法使用jSoup POST POST

這裏的請求頭:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:en-US,en;q=0.8 
Connection:keep-alive 
Content-Length:342 
Content-Type:multipart/form-data; boundary=----WebKitFormBoundary6C8rEu8jENa10v7s 
Cookie:AIROS_SESSIONID=48f1f0f41859ba467e3a2bf1de1f6dd0; ui_language=en_US 
Host:10.0.0.2 
Origin:http://10.0.0.2 
Referer:http://10.0.0.2/login.cgi?uri=/index.cgi 
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML,  like Gecko) Chrome/30.0.1599.101 Safari/537.36 

而這裏的請求負載:

------WebKitFormBoundary6C8rEu8jENa10v7s 
Content-Disposition: form-data; name="uri" 

/index.cgi 
------WebKitFormBoundary6C8rEu8jENa10v7s 
Content-Disposition: form-data; name="username" 

ubnt 
------WebKitFormBoundary6C8rEu8jENa10v7s 
Content-Disposition: form-data; name="password" 

ubnt 
------WebKitFormBoundary6C8rEu8jENa10v7s-- 

解析響應到文檔進行打印,它顯示我的憑據爲無效。難道我的內容類型錯了嗎?

這裏是我的連接代碼:

res1 = Jsoup.connect("http://10.0.0.2/login.cgi") 
.userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:25.0) Gecko/20100101  Firefox/25.0") 
.header("Content-Type", "multipart/form-data") 
.followRedirects(false) 
.referrer("http://10.0.0.2/login.cgi?uri=/index.cgi") 
.data("uri", "/index.cgi") 
.data("username", "ubnt").data("password", "ubnt") 
.method(Method.POST).execute(); 

任何有識之士將不勝感激!

在此先感謝!

編輯:解析響應,服務器返回以下錯誤消息,我相信這進一步加深了我對網站處理Content-Type的處理方式的懷疑。

<body> 
    <b>File Upload Error: No MIME boundary found</b> 
    <br /> 
    <b>There should have been a &quot;boundary=something&quot; in the Content-Type string</b> 
    <br /> 
    <b>The Content-Type string was: &quot;multipart/form-data&quot;</b> 
    <br /> 
    <b><i>l10n.inc:</i> Oops, SetCookie called after header has been sent on line 98</b> 
    <br /> 
    <tt> setcookie(&quot;ui_language&quot;, $active_language, 2147483647<b> 
    <blink> 
    ); /* last valid 32 bit time_t */ 
    </blink></b></tt> 

回答

2

好吧,剛剛解決了!

很顯然,服務器並不期待帶有「用戶名」,「ubnt」,「password」,「ubnt」的數據字段,但它期望的是原始發佈數據。更改內容類型:

post.setHeader("Content-Type","multipart/form-data; boundary=---------------------------21240622191493050652355892969"); 

和不斷變化的實體到

StringEntity postParamsString = new StringEntity("-----------------------------21240622191493050652355892969\r\nContent-Disposition: form-data; name=\"uri\"\r\n\r\n/stalist.cgi\r\n-----------------------------21240622191493050652355892969\r\nContent-Disposition: form-data; name=\"username\"\r\n\r\nubnt\r\n-----------------------------21240622191493050652355892969\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\nubnt\r\n-----------------------------21240622191493050652355892969--\r\n"); 

我得到了一個成功登錄。也改爲Apache Commons,因爲正如Peter指出的那樣,jSoup不能很好地處理multipart/form-data,但我認爲它也可以用於jSoup。