博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
loadrunner 发送gzip压缩json格式(转)
阅读量:6365 次
发布时间:2019-06-23

本文共 3159 字,大约阅读时间需要 10 分钟。

转: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}

 

你可能感兴趣的文章
线程池的设计(二):领导者追随者线程池的设计
查看>>
获取设备列表
查看>>
Django使用网上模板做个能展示的博客
查看>>
基于同IP不同端口,同端口不同Ip的虚拟主机 基于FQDN的虚拟主机
查看>>
项目软件集成三方模块,编译中int32和uint32定义冲突解决方法
查看>>
StretchDIBits速度测试(HALFTONE)
查看>>
在.NET Workflo“.NET研究”w 3.5中使用多线程提高工作流性能
查看>>
验证Oracle处理速度
查看>>
自己写一个jquery
查看>>
BGP聚合attribute-map
查看>>
艾伟:C#中抽象类和接口的区别
查看>>
Flink - NetworkEnvironment
查看>>
BZOJ4374 : Little Elephant and Boxes
查看>>
【.Net Framework 体积大?】不安装.net framework 也能运行!?开篇叙述-1
查看>>
LLDP协议、STP协议 笔记
查看>>
tomcat中的Manager App帐号password管理
查看>>
如何使用 GroupBy 计数-Count()
查看>>
有了这个课件制作工具,还怕备课有难题?
查看>>
jquery之clone()方法详解
查看>>
Delphi 用文件流读取文本文件字符串的方法
查看>>