This problem has been encountered in production, and the interface invocation problem of Java is recorded.
One is the JSON way;
Public StaticString sendpost (string url, Jsonobject obj)throwsioexception{OutputStreamWriter out=NULL; BufferedReader Reader=NULL; String Response=""; /*String paramstr = ""; if (null! = param) {//traversal parameter map added to the collection set<string> KeySet = Param.keyset (); for (String key:keyset) {paramstr = paramstr + key + ":" + param.get (Key) + "&"; }} int lastandindex = Paramstr.lastindexof ("&"); if (lastandindex! =-1) {paramstr = paramstr.substring (0, Lastandindex); } System.out.println ("address = = =" + URL); System.out.println ("Enter the parameter = = =" + paramstr);*/ Try{URL Httpurl=NULL;//the HTTP URL class uses this class to create a connection//Create URLHttpurl =Newurl (URL); //Establish a connectionHttpURLConnection conn =(HttpURLConnection) httpurl.openconnection (); Conn.setrequestproperty ("Accept", "*/*");//to set common request propertiesConn.setrequestproperty ("Charset", "UTF-8"); Conn.setrequestmethod ("POST"); Conn.setrequestproperty ("Content-type", "Application/json"); Conn.setrequestproperty ("Connection", "keep-alive"); Conn.setusecaches (false);//set do not cacheConn.setinstancefollowredirects (true); Conn.setdooutput (true); Conn.setdoinput (true); Conn.connect (); //POST Requestout =NewOutputStreamWriter (Conn.getoutputstream ()); Out.write (Obj.tojsonstring ()); Out.flush (); //Read ResponseReader =NewBufferedReader (NewInputStreamReader (Conn.getinputstream ())); String lines; while((lines = Reader.readline ())! =NULL) {lines=NewString (Lines.getbytes (), "Utf-8"); Response+=lines; } reader.close (); //Disconnect ConnectionConn.disconnect (); } Catch(Exception e) {System.out.println ("Send POST Request exception!" "+e); E.printstacktrace (); } //Use the finally block to close the output stream, input stream finally{ Try{ if(out!=NULL) {out.close (); } if(reader!=NULL) {reader.close (); } } Catch(IOException ex) {ex.printstacktrace (); } } returnresponse; }
Another normal str call interface;
/*** Request to send the Post method to the specified URL *@paramURL to send the request to *@paramparam The request parameter, the request parameter should be in the form of name1=value1&name2=value2. * @returnThe response result of the remote resource represented*/ Public StaticString sendpostwebreq (string url, map<string, object> param,intIthrowsException {String paramstr= ""; if(NULL! = param) {//traversal parameter map added to the collectionSet<string> KeySet =Param.keyset (); for(String key:keyset) {paramstr= paramstr + key + "=" + param.get (Key) + "&"; } } intLastandindex = Paramstr.lastindexof ("&"); if(Lastandindex! =-1) {Paramstr= paramstr.substring (0, Lastandindex); } System.out.println ("Address = = =" +URL); System.out.println ("Enter the parameter = = =" +paramstr); PrintWriter out=NULL; BufferedReader in=NULL; String result= ""; Try{URL Realurl=Newurl (URL); HttpURLConnection Conn= (httpurlconnection) realurl.openconnection ();//opening and linking between URLsConn.setrequestproperty ("Accept", "*/*");//to set common request propertiesConn.setrequestproperty ("Content-type", "Application/json"); Conn.setrequestproperty ("Connection", "keep-alive"); Conn.setrequestproperty ("Charset", "UTF-8"); Conn.setdooutput (true);//to send a POST request, you must set the following two linesConn.setdoinput (true);//gets the output stream corresponding to the URLConnection objectout =NewPrintWriter (NewOutputStreamWriter (Conn.getoutputstream (), "UTF-8"));//Send Request ParametersOut.print (PARAMSTR);//buffer for flush output streamOut.flush (); Inch=NewBufferedReader (NewInputStreamReader (Conn.getinputstream (), "UTF-8"));//defines the response of the BufferedReader input stream to read the URLString Line; while(line = In.readline ())! =NULL) {result+=Line ; } } Catch(Exception e) {System.out.println ("Send POST Request exception!" " +e); Throwe; } finally{//Use the finally block to close the output stream, input stream Try { if(In! =NULL) In.close (); } Catch(IOException ex) {}if(Out! =NULL) Out.close (); } returnresult; }
Java Interface invocation