【问题标题】:Is there any way to see the raw request sent by httpclient?有什么方法可以查看 httpclient 发送的原始请求吗?
【发布时间】:2014-04-05 11:14:28
【问题描述】:

最近,我打算在一个网站上使用HttpClient模拟一个操作。

当我通过chrome developer tools 收到post 请求时,content-type 属于multipart/form-data,所以我试图构造一个MultiPartEntity 来实现它。但我一直有 403 Forbidden。在这种情况下,我确定我的 cookie 处于登录状态,并且我已尝试成功执行其他操作。

所以我想知道如何窥探httpclient 发送的原始请求,以便将其与真实请求进行比较。非常感谢!

附言 在所有的多部分中,有一个如下所示:

------WebKitFormBoundarySYkKonqDITW7A9Bv
Content-Disposition: form-data; name="media_empty"; filename=""
Content-Type: application/octet-stream
 
 

我尝试使用FileBody 来表示这部分。但正如您所见,文件名和内容都是空的。如果我将一个新的 File("") 传递给FileBody,它会抛出一个异常,指示找不到该文件。所以现在,我只是在使用这段代码:

multiPartEntity.addPart("media_empty", new FileBody(new File("C:\\Users\\zhudi.zd\\Desktop\\BeCJToRCMAA5SdV.jpg-large"), "application/octet-stream"));

希望它能为解决这个问题提供一些线索。

仅供参考以下是来自 Chrome 开发工具的请求信息以及我的等效代码 sn-p:

--Request Headers--
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en,zh-CN;q=0.8,zh;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:50305
Content-Type:multipart/form-data; boundary=----WebKitFormBoundarySYkKonqDITW7A9Bv
Cookie:XXXXXX
Host:upload.twitter.com
Origin:https://target.com
Referer:https://target.com/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36

--Request Payload--
------WebKitFormBoundarySYkKonqDITW7A9Bv
Content-Disposition: form-data; name="post_authenticity_token"
 
199465e69ee
------WebKitFormBoundarySYkKonqDITW7A9Bv
Content-Disposition: form-data; name="iframe_callback"
 
window.top.swift_tweetbox_1393907901862
------WebKitFormBoundarySYkKonqDITW7A9Bv
Content-Disposition: form-data; name="in_reply_to_status_id"
 
 
------WebKitFormBoundarySYkKonqDITW7A9Bv
Content-Disposition: form-data; name="impression_id"
 
 
------WebKitFormBoundarySYkKonqDITW7A9Bv
Content-Disposition: form-data; name="earned"
 
 
------WebKitFormBoundarySYkKonqDITW7A9Bv
Content-Disposition: form-data; name="page_context"
 
 
------WebKitFormBoundarySYkKonqDITW7A9Bv
Content-Disposition: form-data; name="status"
 
 
------WebKitFormBoundarySYkKonqDITW7A9Bv
Content-Disposition: form-data; name="media_data[]"
 
ABCDEF
------WebKitFormBoundarySYkKonqDITW7A9Bv
Content-Disposition: form-data; name="media_empty"; filename=""
Content-Type: application/octet-stream
 
 
------WebKitFormBoundarySYkKonqDITW7A9Bv
Content-Disposition: form-data; name="place_id"
 
 
------WebKitFormBoundarySYkKonqDITW7A9Bv--

我的等效代码sn-p:

MultipartEntity entity = new MultipartEntity();
entity.addPart("post_authenticity_token", new StringBody(loginRequest.getAuthenticity_token()));
entity.addPart("iframe-callback", new StringBody("window.top.swift_tweetbox_"+String.valueOf(System.currentTimeMillis())));
entity.addPart("in-reply-to-status-id", new StringBody(""));
entity.addPart("impression-id", new StringBody(""));
entity.addPart("earned", new StringBody(""));
entity.addPart("page-context", new StringBody(""));
entity.addPart("status", new StringBody("some text here"));
entity.addPart("media_data[]", new StringBody("ABCDEF"));
entity.addPart("media_empty", new FileBody(new File("C:\\1.jpg"), "application/octet-stream"));  // trick part, I don't know how to set it as empty file
entity.addPart("place_id", new StringBody(""));

HttpPost post = new HttpPost("https://target.com/post");
post.setHeaders(...);//set all the headers posted above except the Content-Type, for multipartEntity will handle it automatically.
post.setEntity(entity);

response = httpClient.execute(post);

【问题讨论】:

    标签: java httpclient multipart


    【解决方案1】:

    我在 Android 上度过了一段痛苦的时光。您可以使用WebScarab 拦截浏览器/网站通信(您也可以选择查看原始字节)。基本上,您在浏览器中设置了本地代理。这是一个不错的小解决方案。

    请注意,如果您使用 Apache HTTP 工具中的 MultipartEntityBuilder,则必须将 addPart() 方法链接在一起,因为它们每个都返回一个新的 MulitpartEntityBuilder,如以下代码所示:

        mpeb = MultipartEntityBuilder.create()
                    .addTextBody("action", "upload")
                    .addPart("repo_upload_file", fbp.getBody())
                    .addTextBody("sesskey", jso.getString("sesskey"))
                    .addTextBody("repo_id", "3")
                    .addTextBody("itemid", jso.getString("itemid"))
                    .addTextBody("author",  jso.getString("author"))
                    .addTextBody("savepath", "/")
                    .addTextBody("title", current_filename);
    

    【讨论】:

    • 如何使用WebScarab 检测来自HttpClient 的输出?
    • 我知道我误解了这个帖子。对不起。我要调查一下。如果我没有找到任何东西,我会删除我的答案。
    • 您似乎可以通过代理进行连接。见这里:hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/…
    【解决方案2】:

    我已经通过使用ByteArrayEntity 解决了这个问题,它会发送被 Chrome 开发工具截获的原始数据。现在它运行良好。

    【讨论】:

      猜你喜欢
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-14
      相关资源
      最近更新 更多