HttpClient + TestNG Interface Automatic Test chapter II

Source: Internet
Author: User

The request address is generated by parameter and parameter signatures, for example:

Http://ip/server?method=getPlans&planDate=2014-08-25&cinemaId=101&uid=remote&enc= d0fe8420c641dd87d4165c09fe1d0c70&time_stamp=1408960607250

1. Build URLs

1) First build parameters to

 Public Static Hashmap<string, string> Cinemas () {        HashMapnew hashmap<string, string>();        Params.put ("method", "Getcinemas");        Params.put ("UID", starttest.username);         return params;    }

2) generate parameter pairs that contain parameter signatures

 Public StaticList<namevaluepair> getformparams (hashmap<string, string>params) {List<NameValuePair> Formparams =NewArraylist<namevaluepair>();  for(Entry<string, string>E:params.entryset ()) {Formparams.add (NewBasicnamevaluepair (E.getkey (), E.getvalue ())); } formparams.add (NewBasicnamevaluepair ("Time_stamp", string.valueof (NewDate (). GetTime ())); Formparams.add (NewBasicnamevaluepair ("Enc", Getenc.getenc (Formparams, Starttest.password)); returnFormparams; }

3) Generate Request URL

     Public Static URI getUrl (list<namevaluepair> formparams, String HOST, String PATH) {        null;         Try {            new UriBuilder (). SetScheme ("http"). Sethost (HOST).                    SetPath (PATH). Setparameters (Formparams). build ();         Catch (URISyntaxException e) {            e.printstacktrace ();        }         return uri;    }

2. Send a request after building the URL

 Public StaticHttpentity getentity (hashmap<string, string>params, string HOST, String PATH) {httpentity entity=NULL; Closeablehttpclient httpclient=Httpclients.createdefault (); Try {            //Create a HttpGet request. HttpGet HttpGet =NewHttpGet (GetUrl (Getformparams (params), HOST, PATH)); //perform a GET requestCloseablehttpresponse response =Httpclient.execute (HttpGet); Try {                //put the response into the entity carrierentity =response.getentity (); if(Entity! =NULL) {Entity=Newbufferedhttpentity (entity); if(StartTest.log) {Resultslog.writefile (entityutils.tostring (Entity,"UTF-8"), Starttest.path); }                    }            } finally{response.close (); }        } Catch(clientprotocolexception e) {e.printstacktrace (); } Catch(ParseException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally {            //Disconnect Connection            Try{httpclient.close (); } Catch(IOException e) {e.printstacktrace (); }        }        returnentity; }

3. Parse the target JSON data and return the target array

 Public Staticstring[] Targetarray (String Sourcejson, String targetfield) {string targetarray[]=NULL; Jsonobject JS=Jsonobject.fromobject (Sourcejson); if(Sourcejson.contains ("Data") {Jsonarray Datajsonarray= Js.getjsonarray ("Data"); Targetarray=Newstring[datajsonarray.size ()];  for(inti=0; I<datajsonarray.size (); i++) {Targetarray[i]=(Jsonobject.fromobject (datajsonarray.getstring (i))). GetString (Targetfield); }    }  Else{targetarray[0] = js.getstring ("Errcode") + "----" + js.getstring ("ErrMsg"); }    returnTargetarray;}

4. Execute the request URL and parse back the JSON data to get the target value to form the array

     Public StaticString[] Targetvaluearray (hashmap<string, string>params, String Targetfield) {String targetarray[]=NULL; Httpentity Entity=getpost.getentity (params, starttest.round_host, Starttest.round_path); Try{Targetarray=Jsonparsing.targetarray (entityutils.tostring (Entity,"UTF-8"), Targetfield); } Catch(IllegalStateException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }        returnTargetarray; }

Now send GET request from build url-->--parse JSON data get target value completed

HttpClient + TestNG Interface Automatic Test chapter II

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.