To send HTTP requests in JSON and XML format in Java, you must determine the server address (IP address, port, and specific directory) of the request)
Then set the connection Property Information
Request data format (JSON or XML)
Whether to send a message body to the server as needed (only HTTP headers can be sent without specific information). The data format must be consistent with the format set for the HTTP header information, if you want to view the specific information of the sending and server responses, you can use HTTP packet capture tools, such as httpanalizer, to check whether the request data format is correct.
The Code is as follows (the request data format is JSON ):
Import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. io. unsupportedencodingexception; import Java. io. outputstream; import java.net. httpurlconnection; import java.net. malformedurlexception; import java.net. URL; import Org. JSON. jsonobject; import Org. JSON. jsonarray; public class appaddtest {public static final string add_url = "Http: // 192.168.4.68: 35357/V2.0/users"; // public static final string add_url = "http: // 192.168.4.97: 35357/V2.0/tokens "; public static void appadd () throws ioexception {httpurlconnection connection = NULL; try {// create a connection URL url = new URL (add_url); connection = (httpurlconnection) URL. openconnection (); // sets the HTTP connection attribute connection. setdooutput (true); connection. setdoinput (true); connection. setrequestmethod ("Post"); // you can submit the functions provided by HTTP, such as get, post, delete, and input, as needed. setusecaches (false); connection. setinstancefollowredirects (true); // sets the HTTP header message connection. setrequestproperty ("Content-Type", "application/JSON"); // you can set the request format in JSON or XML format. setrequestproperty ("Content-Type", "text/XML"); // set the request format XML, connection. setrequestproperty ("accept", "application/JSON"); // you can set the response information in JSON format or XML // Connection. setrequestproperty ("X-auth-token", "XX"); // the information required by the specific HTTP server. Add the connection as required by the server. connect (); // Add the request content jsonobject user = new jsonobject (); User. put ("name", "alice4"); User. put ("Roles", "admin"); User. put ("enabled", "true"); User. put ("tenantid", "998abe42845e4f258da7e96b0d8335ec"); User. put ("password", "mypassword123"); // construct nested JSON data jsonobject OBJ = new jsonobject (); obj. put ("User ", User); outputstream out = connection. getoutputstream (); out. write (obj. tostring (). getbytes (); out. flush (); out. close (); // read the response bufferedreader reader = new bufferedreader (New inputstreamreader (connection. getinputstream (); string lines; stringbuffer sb = new stringbuffer (""); While (lines = reader. readline ())! = NULL) {lines = new string (lines. getbytes (), "UTF-8"); sb. append (lines);} system. out. println (SB); reader. close (); // disconnect the connection. disconnect ();} catch (malformedurlexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (unsupportedencodingexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace () ;}} public static void main (string [] ARGs) throws ioexception {appadd ();}}