2014-03-04 36 views
1

最近,我打算使用HttpClient來模擬網站上的操作。有什麼方法可以查看httpclient發送的原始請求嗎?

當我趕上通過chrome developer tools發佈請求,該content-typemultipart/form-data,所以我想構建一個MultiPartEntity去實現它。但我總是得到403禁止。在這種情況下,我確信我的cookie是登錄狀態,並且我已經嘗試成功執行其他操作。

所以我想知道如何偵測httpclient發送的原始請求,以便我可以將它與真實的請求進行比較。非常感謝!

P.S. 在所有的multipart的,有一個其如下所示:

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

我試圖使用FileBody來表示該部分。但正如你所看到的,文件名以及內容都是空的。如果我傳遞一個新的文件(「」)到FileBody,它會拋出一個異常,指出找不到文件。所以現在,我只是使用這個代碼:

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

希望它會顯示解決此問題的一些線索。

FYI下面是來自於Chrome瀏覽器開發工具,以及我的等價代碼段的請求信息:我的

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

等效代碼片段:

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

回答

0

我有一個悲慘的時間這在Android上。您可以使用WebScarab來攔截瀏覽器/網站通信(您也可以選擇查看原始字節)。基本上,你在瀏覽器中設置了一個本地代理。這是一個很好的小解決方案。

請注意,如果您使用的MultipartEntityBuilder從的Apache HTTP工具,你必須鏈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); 
+0

如何使用'WebScarab'來檢測'HttpClient'的輸出? – Judking

+0

我看到我誤解了這個帖子。抱歉。我要研究這個。如果我找不到任何東西,我會刪除我的答案。 – mttdbrd

+0

它看起來像你可以通過代理連接。看到這裏:http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/connmgmt.html#d5e473 – mttdbrd

0

我已經通過使用ByteArrayEntity解決了該問題,該工具發送由Chrome開發人員工具攔截的原始數據。到目前爲止它已經運行良好。

相關問題