loadrunner 發送gzip壓縮json格式

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   java   使用   os   

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

使用java vuser實現,發送gzip壓縮json格式。

/* * 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 class Actions{public int init() throws Throwable {return 0;}//end of initpublic int action() throws Throwable {           String foo = "{\"job_id\":93044,\"client_id\":1009,\"version\":\"10001\",\"dev_type\":1,\"app_guid\":\"1\"}";           HttpResponse response;           HttpClient  httpclient = new DefaultHttpClient();  HttpPost httppost= new HttpPost ("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 = new InputStreamEntity(new ByteArrayInputStream(bgzip), bgzip.length);           httpentity.setChunked(true);  httppost.setEntity(httpentity);    lr.start_transaction("發送");           response=httpclient.execute(httppost);           httppost.setEntity(httpentity);           int httpCode = 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=new GZIPInputStream(inputStream);             lr.end_transaction("發送", lr.AUTO);           InputStreamReader isr = new InputStreamReader(inputStream, "utf-8"); // 設定讀取流的編碼格式,自訂編碼           BufferedReader br = new BufferedReader(isr);           StringBuffer sb = new StringBuffer();           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 actionpublic static byte[] gzip(String foo){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzos = null; try {     gzos = new GZIPOutputStream(baos);     gzos.write(foo.getBytes("UTF-8")); } catch (IOException e) {     // TODO Auto-generated catch block     e.printStackTrace(); } finally {     if (gzos != null) try { gzos.close(); } catch (IOException ignore) {}; } return baos.toByteArray();      }public int end() throws Throwable {return 0;}//end of end}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.