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