Interface testing-automated-java implementation-httputil

Source: Internet
Author: User
Tags response code

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.