LoadRunner send gzip compression JSON format

Source: Internet
Author: User

Ext.: http://blog.csdn.net/gzh0222/article/details/7711281

Using the Java VUser implementation, send the GZIP compressed JSON format.

/** LoadRunner Java script. (Build: _build_number_) * * Script Description: **/import lrapi.lr;import java.io.*; import Java.io.bufferedreader;import java.io.bytearrayinputstream;import java.io.ByteArrayOutputStream; Import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.inputstream;import Java.util.zip.gzipinputstream;import Java.util.zip.gzipoutputstream;import Org.apache.http.header;import Org.apache.http.httpentity;import Org.apache.http.httpresponse;import Org.apache.http.client.httpclient;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.methods.httppost;import Org.apache.http.entity.inputstreamentity;import org.apache.http.impl.client.DefaultHttpClient; Public classactions{ Public intinit () throws Throwable {return 0;}//End of Init Public intaction () throws Throwable {String foo="{\ "job_id\": 93044,\ "client_id\": 1009,\ "version\": \ "10001\", \ "dev_type\": 1,\ "app_guid\": \ "1\"}";           HttpResponse response; HttpClient HttpClient=Newdefaulthttpclient (); HttpPost HttpPost=NewHttpPost ("Http://10.10.10.10:61013/proxy/c/job/detail.json"); Httppost.setheader ("Content-type","Application/json; Charset=utf-8"); Httppost.setheader ("accept-encoding","gzip"); byte[] Bgzip =gzip (foo); System. out. println (Bgzip.length); Inputstreamentity httpentity=NewInputstreamentity (NewBytearrayinputstream (bgzip), bgzip.length); Httpentity.setchunked (true);    Httppost.setentity (httpentity); Lr.start_transaction ("Send"); Response=Httpclient.execute (HttpPost);           Httppost.setentity (httpentity); intHttpcode =response.getstatusline (). Getstatuscode (); System. out. println (Httpcode); Httpentity Entity=response.getentity (); Header Header= Response.getfirstheader ("Content-type"); System. out. println (header); InputStream InputStream=entity.getcontent (); InputStream=NewGzipinputstream (InputStream); Lr.end_transaction ("Send", LR.           AUTO); InputStreamReader ISR=NewInputStreamReader (InputStream,"Utf-8");//set the encoding format of the read stream, custom encodingBufferedReader br =NewBufferedReader (ISR); StringBuffer SB=NewStringBuffer ();           String TEMPBF;  while((TEMPBF = Br.readline ())! =NULL) {sb.append (TEMPBF); Sb.append ("\ r \ n"); } String HTML=sb.tostring (); System. out. println (HTML);            Isr.close ();        Inputstream.close (); return 0;}//End of Action Public Static byte[] gzip (String foo) {Bytearrayoutputstream BAOs=NewBytearrayoutputstream (); Gzipoutputstream Gzos=NULL; Try{Gzos=NewGzipoutputstream (BAOs); Gzos.write (Foo.getbytes ("UTF-8")); } Catch(IOException e) {//TODO auto-generated Catch blocke.printstacktrace ();} finally {     if(Gzos! =NULL)Try{Gzos.close ();}Catch(IOException ignore) {};} returnBaos.tobytearray (); } Public intEnd () throws Throwable {return 0;}//End of End}

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.