2

试图从路由器网站中获取一些数据,但无法找出 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--

将响应解析为文档并打印,它显示我的凭据无效。会不会是我的 Content-Type 弄错了?

这是我的连接代码:

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();

任何见解将不胜感激!

提前致谢!

编辑:解析响应,服务器返回以下错误消息,我相信这进一步怀疑它与网站处理内容类型的方式有关。

<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>
4

1 回答 1

2

好的,刚刚解决了!

显然,服务器并不期待带有“用户名”、“ubnt”、“密码”、“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 不能很好地处理多部分/表单数据,但我认为它也可以与 jSoup 一起使用。

于 2013-11-06T13:58:48.160 回答