HttpPost request JSON as a simple processing method for incoming request body

Source: Internet
Author: User

The JSON parameters are sent through the HttpClient post method for interface testing. Learn how to provide the "cloud".

Clouds
Links: https://www.zhihu.com/question/30878548/answer/121149629
Source: Know the JSON to be sent as a string into the body can be
1  Public StaticString sendhttppost (string URL, string body)throwsException {2Closeablehttpclient httpClient =Httpclients.createdefault ();3HttpPost HttpPost =Newhttppost (URL);4Httppost.addheader ("Content-type", "Application/json");5Httppost.setentity (Newstringentity (body));6 7Closeablehttpresponse response =Httpclient.execute (httppost);8System.out.println (Response.getstatusline (). Getstatuscode () + "\ n");9httpentity entity =response.getentity ();TenString responsecontent = entityutils.tostring (Entity, "UTF-8");  One System.out.println (responsecontent); A  - response.close (); - httpclient.close (); the returnresponsecontent; -}

Example of my test code:

1      Public Static voidMain (string[] args) {2         //test the API interface of the company, passing JSON as a string into the HttpPost's request body3String result =NULL;4HttpClient client =Httpclients.createdefault ();5UriBuilder Builder =NewUriBuilder ();6Uri uri =NULL;7         Try {8URI = Builder.setscheme ("http")9. Sethost ("Xxx.xxx.xxx.xxx:xxxx")Ten. SetPath ("/api/authorize/login") One . Build (); A              -HttpPost post =NewHttpPost (URI); -             //set the request header thePost.setheader ("Content-type", "Application/json"); -String BODY = "{\" key\ ": \" \ ", \" secret\ ": \" \ "}"; -             //set the request body -Post.setentity (Newstringentity (body)); +             //Get return information -HttpResponse response =Client.execute (POST); +result =response.tostring (); A}Catch(Exception e) { atSYSTEM.OUT.PRINTLN ("Interface request Failed" +e.getstacktrace ()); -         } - System.out.println (result); -}

HttpPost request JSON as a simple processing method for incoming request body

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.