Package Com.mytesting;import Java.io.dataoutputstream;import Java.io.inputstream;import java.net.URL;import Java.net.urlconnection;import Org.apache.commons.io.ioutils;public class Post {public static void main (string[] args) { TODO auto-generated Method Stubdohttppost ("Http://172.18.3.27:8031/Activity/GetActivityList", "{\" tpid\ ": 123}") ;//The first is the request URL the second is the body parameter}/** * Send POST request * * @param postbody * @return */public static string Dohttppost (String Url,strin G postbody) {//System.out.println ("Originating Data:" + postbody); byte[] XmlData = Postbody.getbytes (); InputStream instr = Null;try {URL url = new url (URL); URLConnection Urlcon = Url.openconnection (); Urlcon.setdooutput (true); Urlcon.setdoinput (true); Urlcon.setusecaches ( FALSE); Urlcon.setrequestproperty ("Content-type", "Application/json"); Urlcon.setrequestproperty ("User-Agent", " App30007/3f845f3287a3c3f20a04b08c5074fc95/2b80541f-1c28-4fe7-8eef-4a20d688a984/0/"); UrlCon.setRequestProperty ( "Content-length", String.valueof (xmldata.length));D AtaoutputStream printout = new DataOutputStream (Urlcon.getoutputstream ());p rintout.write (xmlData);p Rintout.flush (); Printout.close (); instr = Urlcon.getinputstream (); byte[] bis = Ioutils.tobytearray (instr); String responsestring = new string (bis, "UTF-8"); if ((responsestring = = null) | | ("". Equals (Responsestring.trim ()))) {System.out.println ("return null");} SYSTEM.OUT.PRINTLN ("Return data is:" + responsestring); return responsestring;} catch (Exception e) {e.printstacktrace (); return "0";} finally {try {//Out.close (); Instr.close ();} catch (Exception ex) {R Eturn "0";}}}
To introduce the required packages, there are some additional packages that need to be downloaded
Java writing a POST request