51Testing软件测试论坛

标题: Loadrunner java vuser获取返回信息中的字节数组 [打印本页]

作者: xinyuzxx    时间: 2017-2-6 15:01
标题: Loadrunner java vuser获取返回信息中的字节数组
最近需要用Loadrunner 对java开发的一个web接口进行测试,接口返回的是字节数组。如果直接用web_reg_save_param进行获取,获取不到返回的字节数组信息。
使用的是java vuser进行脚本编写的,想问一下各位,有什么好方法来获取这个响应的字节数组没?多谢

PS:目前知道在jmeter中可以用这个语句进行获取:byte[] response_data =prev.getResponseData();
不知Loadrunner中有没有对应的方法?


作者: xinyuzxx    时间: 2017-2-6 15:36
补充说明:响应头信息如下:
HTTP/1.1 200 OK
Date: Mon, 06 Feb 2017 07:28:40 GMT
Transfer-Encoding: chunked
Connection: close
Content-Encoding: gzip
Content-Language: zh-CN
Set-Cookie: SERVERID=6e068877643f77ca078467e57bfed29e|1486366120|1486366119ath=/

响应头中有Content-Encoding: gzip,问过开发那边,好像这个有影响
作者: xinyuzxx    时间: 2017-2-17 17:24
问题我自己解决
没有找到直接使用loadrunner已有函数的方法,幸好的是java vuser中可以直接用java语句来实现想要的功能,所以我直接写了一个方法,如下,希望能给有同样问题的朋友们一点参考

import java.io.IOException;  
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;  
import org.apache.http.util.EntityUtils;

        private byte[] GetByteFromPost(String strurl,String strparam)
        {
            HttpPost httpPost = new HttpPost(strurl);
            
            RequestConfig requestConfig = RequestConfig.custom()   
                .setSocketTimeout(15000)   
                .setConnectTimeout(15000)   
                .setConnectionRequestTimeout(15000)   
                .build();           
            CloseableHttpClient httpClient = null;   
            CloseableHttpResponse response = null;   
            StringEntity strEntity=null;
            HttpEntity entity = null;   
            byte[] responseContent = null;  

            try
            {
                // 创建默认的httpClient实例.   
                httpClient = HttpClients.createDefault();
                strEntity = new StringEntity(strparam);
                strEntity.setContentEncoding("UTF-8");
                strEntity.setContentType("text/plain; charset=UTF-8");
                httpPost.setEntity(strEntity);
                httpPost.setConfig(requestConfig);   
                // 执行请求   
                response = httpClient.execute(httpPost);
                entity = response.getEntity();   
                responseContent = EntityUtils.toByteArray(entity);               
            } catch (Exception e) {   
                e.printStackTrace();   
            } finally {   
                try {   
                    // 关闭连接,释放资源   
                    if (response != null) {   
                        response.close();   
                    }   
                    if (httpClient != null) {   
                        httpClient.close();   
                    }   
                } catch (IOException e) {   
                    e.printStackTrace();   
                }   
            }   
            return responseContent;   
        }


另外说明下,要保证运行时设置里的Classpath没有多余的jar,否则可能会报错




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2