首页 > 编程语言 > 详细

loadrunner get请求的一个java vuser 脚本

时间:2020-09-08 16:50:07      阅读:105      评论:0      收藏:0      [点我收藏+]
/*
 * LoadRunner Java script. (Build: _build_number_)
 * 
 * Script Description: 
 *                     
 */

import lrapi.lr;

import com.ytinf.service.lrTest.GetRequest;
import com.alibaba.fastjson.JSONObject;

public class Actions
{

    public int init() throws Throwable {
        return 0;
    }//end of init


    public int action() throws Throwable {
        
        String url="http://XX.XX.XX.XX:8089/databridge_dg/hbase/lottery";
        GetRequest getRequest=new GetRequest();
        getRequest.addParameter("lotterysn","{lotterySn_lottery}");
        lr.think_time(1);
        lr.start_transaction("票明细查询");
        JSONObject jsonObject=null;
        try{
            jsonObject=getRequest.get(url);
            
        }catch(Exception e){
            lr.end_transaction("票明细查询",lr.FAIL);//如果超时报错,事务失败
            lr.output_message("获取响应报错了");
        }
        if (jsonObject!=null){
            if (jsonObject.get("status").equals(0)){
                lr.end_transaction("票明细查询",lr.PASS);//解析响应结果,事务成功
                
            }else{
                
                lr.end_transaction("票明细查询",lr.FAIL);
                lr.output_message("响应信息的status字段不为 0 。");//解析响应结果失败,事务失败
            }
        }else{
            lr.end_transaction("票明细查询",lr.FAIL);//响应结果 为 null,响应失败
            lr.output_message("获取的响应信息为 null");
        }
        
        
        return 0;
    }//end of action


    public int end() throws Throwable {
        return 0;
    }//end of end
}

附上 java 代码 通过 httpUtil 发送http 请求:

package com.ytinf.service.lrTest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GetRequest {
    private List<NameValuePair> params;

    public GetRequest() {}

    public GetRequest(List<NameValuePair> params) {
        this.params = params;
    }

    public void setParams(List<NameValuePair> params) {
        this.params = params;
    }

    // 直接传入要发送的参数列表
    public JSONObject get(String url, List<NameValuePair> params) throws Exception {
        CloseableHttpClient httpClient=HttpClients.createDefault();

        URIBuilder uriBuilder=new URIBuilder(url);
        uriBuilder.setParameters(params);

        HttpGet httpGet=new HttpGet(uriBuilder.build());
        CloseableHttpResponse response=null;
        String result=null;
        try{
            response=httpClient.execute(httpGet);
            // System.out.println(response.getStatusLine().getStatusCode());
            HttpEntity httpEntity=response.getEntity();
            // System.out.println(EntityUtils.toString(httpEntity));
            result=EntityUtils.toString(httpEntity);
        }finally {
            if (response!=null){
                response.close();
            }
            httpClient.close();
        }
        return JSONObject.parseObject(result);
    }
    // 可以直接 get URL,也可以通过单个add()方法,添加参数后 再执行
    public JSONObject get(String url) throws Exception {
        CloseableHttpClient httpClient=HttpClients.createDefault();

        URIBuilder uriBuilder=new URIBuilder(url);
        if (this.params!=null){
            uriBuilder.setParameters(this.params);
        }
        HttpGet httpGet=new HttpGet(uriBuilder.build());
        CloseableHttpResponse response=null;
        String result=null;
        try{
            response=httpClient.execute(httpGet);
            // System.out.println(response.getStatusLine().getStatusCode());
            HttpEntity httpEntity=response.getEntity();
            // System.out.println(EntityUtils.toString(httpEntity));
            result=EntityUtils.toString(httpEntity);
        }finally {
            if (response!=null){
                response.close();
            }
            httpClient.close();
        }
        return JSONObject.parseObject(result);
    }

    // 给实例变量添加 单个参数
    public void removeAllParameters(){
        if (this.params!=null && params.size()>0){
            this.params.clear();
        }

    }

    // 给实例变量添加 单个参数
    public void addParameter(String name,String value){
        if (this.params==null){
            this.params=new ArrayList<>();
        }
        this.params.add(new BasicNameValuePair(name,value));
    }



    public static void main(String[] args) throws Exception{
        String url="http://XX.XX.XX.XX:8089/databridge_dg/hbase/lottery";
        GetRequest getRequest=new GetRequest();
        List<NameValuePair> nameValuePairList=new ArrayList<>();
        nameValuePairList.add(new BasicNameValuePair("lotterysn","910440142860045587600108"));
        System.out.println(getRequest.get(url,nameValuePairList));

        getRequest.addParameter("lotterysn","910540078760041513600403");
        JSONObject jsonObject=getRequest.get(url);
        if (jsonObject.get("status").equals(0)){
            System.out.println("成功!");
        }else{
            System.out.println("失败!");
        }

        url="http://XX.XX.XX.XX:8089/databridge_dg/hbase/win_lottery";
        getRequest.removeAllParameters();
        getRequest.addParameter("drawidlotterysn","175660110610175660092646500708");
        JSONObject jsonObject1=getRequest.get(url);
        System.out.println("中奖票查询的响应结果:"+jsonObject1);




    }

}

 

loadrunner get请求的一个java vuser 脚本

原文:https://www.cnblogs.com/xiaoxiao075/p/13633163.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!