zoukankan      html  css  js  c++  java
  • loadrunner get、Post 请求的一个java vuser 脚本

    /*
     * 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);
    
    
    
    
        }
    
    }

     发送 http post请求  java示例

    package com.ytinf.service.lrTest;
    
    import com.alibaba.fastjson.JSONObject;
    import org.apache.http.HttpEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    
    
    public class PostRequest {
        public JSONObject post(String address,JSONObject jsonObject) throws Exception{
            CloseableHttpClient httpClient= HttpClients.createDefault();
            HttpPost httpPost=new HttpPost(address);
    
            String data=jsonObject.toJSONString();
            StringEntity entity=new StringEntity(data,"UTF-8");
            httpPost.setEntity(entity);
            CloseableHttpResponse response=null;
            JSONObject jsonResult=null;
            try{
                response=httpClient.execute(httpPost);
                jsonResult=JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
            }finally {
                if (response!=null){
                    response.close();
                }
                httpClient.close();
            }
    
            return jsonResult;
        }
    }
  • 相关阅读:
    影像数据的裁切和保存(源代码)
    C#读取EXIF信息类多
    C#里到底怎么样才能嵌入汇编?
    using和回收资源
    两个由栈组成的队列和两个由队列组成的栈
    C解两道题
    Inside WUAIntroduction
    轮子和做轮子
    致曾经整过的"框架"
    C#动态分配一维数组和二维数组函数
  • 原文地址:https://www.cnblogs.com/xiaoxiao075/p/13633163.html
Copyright © 2011-2022 走看看