2017-07-26 34 views
-3

body選項用於控制實體封閉請求的主體(例如,PUT,POST,PATCH)。在PHP中使用它這樣像在php中的身體選項在java中使用什麼?

$client->request('POST', '/post', ['body' => $stream]); 

但我想在Java中使用像http://crawler.ankiti.com有JSON

{ 
    "data": [ 
    { 
     "url": "http:\/\/crawler.ankiti.com\/?module=server&do=test", 
     "method": "post", 
     "options": { 
     "body": "post body test 1" 
     }, 
     "target": "wo" 
    }, 
    { 
     "url": "http:\/\/crawler.ankiti.com\/?module=server&do=test", 
     "method": "post", 
     "target": "wo", 
     "options": { 
     "body": "post body test 1", 
     "form_params": { 
      "name": "abhay", 
      "surname": "aggarwal" 
     }, 
     "headers": { 
      "login": "abhay", 
      "password": "patparganj405" 
     } 
     } 
    }, 
    { 
     "url": "http:\/\/crawler.ankiti.com\/?module=server&do=test", 
     "method": "post", 
     "target": "wo", 
     "options": { 
     "multipart": [ 
      { 
      "name": "file text", 
      "path": "\/test\/file.txt" 
      }, 
      { 
      "name": "file image", 
      "path": "\/test\/file.jpg" 
      } 
     ] 
     } 
    } 
    ], 
    "errors": "", 
    "meta": "" 
} 

我要發佈於一體的請求一個字符串,它是存在於該URL的JSON 。

+0

我的眼球彈出我的頭,當我試圖閱讀。 – ajb

+0

歡迎使用stackoverflow,每個人都應該從某個地方開始。有關引用此鏈接可能會幫助你http://www.wikihow.com/Ask-a-Question-on-Stack-Overflow –

+0

手動編寫servlet的次數很少級別和容易出錯。使用類似Spring MVC或Dropwizard的東西。 – chrylis

回答

0

您可以使用httpServlet並按照這樣的過程。

response.setContentType("application/json"); 
// Get the printwriter object from response to write the required json object to the output stream  
PrintWriter out = response.getWriter(); 
// Assuming your json object is **jsonObject**, perform the following, it will return your json object 
out.print(jsonObject); 
out.flush(); 

希望這有助於

+0

謝謝Rahul的答覆,但正如我所知的PHP選項不能與form_params,multipart或JSON一起使用,所以在Java中是可能的 –

+0

是的,因爲他問的問題我只是在談論Java –