Httputil is responsible for providing the post method of HTTP, and its core method is as follows:
/*** do POST request *@paramURL *@paramParametermap *@return * @throwsException*/ PublicString doPost (string url, map<?,? > Parametermap)throwsException {/*Translate parameter map to parameter date string*/StringBuffer Parameterbuffer=NewStringBuffer (); if(Parametermap! =NULL) {Iterator<?> iterator =Parametermap.keyset (). iterator (); String Key=NULL; String value=NULL; while(Iterator.hasnext ()) {Key=(String) iterator.next (); if(Parametermap.get (key)! =NULL) {Value=(String) parametermap.get (key); } Else{Value= ""; } parameterbuffer.append (Key). Append ("="). append (value); if(Iterator.hasnext ()) {Parameterbuffer.append ("&"); } } } //System.out.println ("POST parameter:" + parameterbuffer.tostring ());URL Localurl=Newurl (URL); URLConnection Connection=OpenConnection (Localurl); HttpURLConnection HttpURLConnection=(httpurlconnection) connection; Httpurlconnection.setdooutput (true); Httpurlconnection.setrequestmethod ("POST"); Httpurlconnection.setrequestproperty ("Accept-charset", CharSet); Httpurlconnection.setrequestproperty ("Content-type", "application/x-www-form-urlencoded"); Httpurlconnection.setrequestproperty ("Content-length", String.valueof (Parameterbuffer.length ())); OutputStream OutputStream=NULL; OutputStreamWriter OutputStreamWriter=NULL; InputStream InputStream=NULL; InputStreamReader InputStreamReader=NULL; BufferedReader Reader=NULL; StringBuffer Resultbuffer=NewStringBuffer (); String Templine=NULL; Try{OutputStream=Httpurlconnection.getoutputstream (); OutputStreamWriter=NewOutputStreamWriter (OutputStream); Outputstreamwriter.write (Parameterbuffer.tostring ()); Outputstreamwriter.flush (); if(Httpurlconnection.getresponsecode () >= 300) {resultbuffer.append (Httpurlconnection.getresponsecode ()); Throw NewException ("HTTP Request isn't success, Response code is" +Httpurlconnection.getresponsecode ()); } InputStream=Httpurlconnection.getinputstream (); InputStreamReader=NewInputStreamReader (InputStream); Reader=NewBufferedReader (InputStreamReader); while((Templine = Reader.readline ())! =NULL) {resultbuffer.append (templine); } } finally { if(OutputStreamWriter! =NULL) {outputstreamwriter.close (); } if(OutputStream! =NULL) {outputstream.close (); } if(Reader! =NULL) {reader.close (); } if(InputStreamReader! =NULL) {inputstreamreader.close (); } if(InputStream! =NULL) {inputstream.close (); } } returnresultbuffer.tostring (); }
In fact, this is not my own code, so there is nothing special to say. I only know that the URL and interface parameters are passed and the Post method is used to get the results of the server.
Interface testing-automated-java implementation-httputil