--put method of interface pressure measurement 2

Source: Internet
Author: User


Since LR does not support put, I have to write a vuser script through Java, using the HttpClient jar package, which I use for the httpclient4.5 version.





/* * LoadRunner Java script. (Build: _build_number_) * * Script Description: * */import lrapi.lr;import Lrapi.web; Import Java.io.bufferedwriter;import Java.io.filewriter;import Java.io.ioexception;import Java.io.unsupportedencodingexception;import Java.util.arraylist;import Java.util.list;import java.util.Date; Import Java.text.simpledateformat;import Org.apache.http.httpentity;import Org.apache.http.httpresponse;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.httpclient;import Org.apache.http.client.entity.urlencodedformentity;import Org.apache.http.client.methods.httppost;import Org.apache.http.client.methods.httpput;import Org.apache.http.impl.client.httpclients;import Org.apache.http.message.basicnamevaluepair;import Org.apache.http.util.entityutils;public class Actions{public int Init () throws Throwable {return 0;} End of Initpublic int action () throws Throwable {HttpClient client = Httpclients.createdefault (); Httpput put = new Httpput ("http://xxxxcart.beta1.fn/xxxCart/rest/v1/cart/");//lr.save_string (lr.eval_string ("<owner>") , "user"); Lr.output_message ("the value of str =" + lr.eval_string ("<user>") + "."); String sourcestr = lr.eval_string ("<name5529206105542>"); String sourcearray[] = Sourcestr.split (","); list<basicnamevaluepair> parameters = new arraylist<basicnamevaluepair> ();p Arameters.add (new Basicnamevaluepair ("owner", Sourcearray[0]); Lr.save_string (sourcearray[0], "user");    Parameters.Add (New Basicnamevaluepair ("owner", "name5529206105542")); for (int i=2;i<sourcearray.length;i++) {    Lr.save_string (Sourcearray[i], "ID1");//Lr.output_message ("Id1 value is" +lr.eval_string ("<id1>"));    Parameters.Add (New Basicnamevaluepair ("Area_seq", "CS000016"));    Parameters.Add (New Basicnamevaluepair ("Is_login", "1")); Parameters.Add (New Basicnamevaluepair ("Mem_type", "0"));//Parameters.Add (New Basicnamevaluepair ("Cart_detail_list "," [{\ "cart_detail_id\": \ "715\", \ "qty\": 2,\ "selected\": True,\ "campaign_seq\": \ "\"}] ")); Parameters.Add (New Basicnamevaluepair ("Cart_detail_list", "[{\" cart_detail_id\ ": \" <id1>\ ", \" qty\ ": <qty >,\ "selected\": True,\ "campaign_seq\": \ "\"}] "));/*list<basicnamevaluepair> parameters = new arraylist< Basicnamevaluepair> ();p Arameters.add (New Basicnamevaluepair ("owner", "<user>"));p Arameters.add (new Basicnamevaluepair ("Area_seq", "CS000016"));p Arameters.add (New Basicnamevaluepair ("Is_login", "1")); Parameters.Add (New Basicnamevaluepair ("Mem_type", "0"));p Arameters.add (New Basicnamevaluepair ("Cart_detail_list", "[{\" cart_detail_id\ ": \" <id>\ ", \" qty\ ": 2,\" selected\ ": True,\" campaign_seq\ ": \" \ "}]")); *//System    . OUT.PRINTLN ("parameters is" + parameters);        try {put.setentity (new urlencodedformentity (Parameters, "UTF-8"));    Lr.start_transaction ("T1");              HttpResponse response = Client.execute (Put);        httpentity entity = response.getentity (); String result = entityutils.tostring (entity, "UTF-8");        SYSTEM.OUT.PRINTLN (result);        Boolean B = result.contains ("\" code\ ": 1");//System.out.println (b);    if (b = = true) {//system.out.println ("true"); Lr.end_transaction ("T1", LR.    PASS);    } else {//system.out.println ("false"); Lr.end_transaction ("T1", LR. FAIL);     FileWriter writer = Null;try {date now = new Date (); SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");//You can easily modify the date format String hehe = Dateform    At.format (now);        writer = new FileWriter ("E://codeerror.txt", true);    Writer.write (result);     BufferedWriter buffer = new BufferedWriter (writer);    Buffer.write (hehe);    Buffer.write (lr.eval_string ("<user>"));        Buffer.write (result);    Buffer.newline ();        Buffer.flush (); Buffer.close ();}   catch (IOException e) {e.printstacktrace ();       } finally {try {if (writer! = null) {writer.close (); }} catch (Ioexception e) {e.printstacktrace ();    }}}} catch (Unsupportedencodingexception e) {e.printstacktrace ();    } catch (Clientprotocolexception e) {//TODO auto-generated catch block E.printstacktrace ();    } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }}//end of Forreturn 0;} End of Actionpublic int end () throws Throwable {return 0;} End of End}























Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

--put method of interface pressure measurement 2

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.