java vuser协议
Posted cnhkzyy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java vuser协议相关的知识,希望对你有一定的参考价值。
一. 项目流程
登录-->修改支付密码-->下订单-->支付订单-->查看订单列表
支付密码:md5进行加密算法。
1. 为什么要学习java vuser协议?
为了解决工作中需要调用开发的算法,进行脚本的开发
2. 工作中会遇到:需要对支付密码进行md5算法的调用,以及更复杂的情况(实际项目中可能使用sign + 每次请求的,再请求到服务器,这样做的好处:不容易被攻击和破解)
3. 不懂java代码,怎么解决?
可以先学会思路,按照步骤一步一步解决
二. 步骤
1. 搭建jdk环境变量
LR11:jdk 1.6版本,32位
LR12:jdk 1.7版本,32位
2. 用IDEA工作来调试md5加密算法
(1) 从开发那里获取算法
(2) 在IDEA里新建一个maven工程,生成一个pom.xml配置文件
(3) 把开发的算法(java文件),放在工程里进行调试,只要没报错,就可以了
package com.pertest; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /* * testmd5.MD5 算法 */ public class MD5 { // 全局数组 private final static String[] strDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; public MD5() { } // 返回形式为数字跟字符串 private static String byteToArrayString(byte bByte) { int iRet = bByte; // System.out.println("iRet="+iRet); if (iRet < 0) { iRet += 256; } int iD1 = iRet / 16; int iD2 = iRet % 16; return strDigits[iD1] + strDigits[iD2]; } // 返回形式只为数字 private static String byteToNum(byte bByte) { int iRet = bByte; System.out.println("iRet1=" + iRet); if (iRet < 0) { iRet += 256; } return String.valueOf(iRet); } // 转换字节数组为16进制字串 private static String byteToString(byte[] bByte) { StringBuffer sBuffer = new StringBuffer(); for (int i = 0; i < bByte.length; i++) { sBuffer.append(byteToArrayString(bByte[i])); } return sBuffer.toString(); } public static String GetMD5Code(String strObj) { String resultString = null; try { resultString = new String(strObj); MessageDigest md = MessageDigest.getInstance("MD5"); // md.digest() 该函数返回值为存放哈希值结果的byte数组 resultString = byteToString(md.digest(strObj.getBytes())); } catch (NoSuchAlgorithmException ex) { ex.printStackTrace(); } return resultString; } public static void main(String[] args) throws UnsupportedEncodingException { String iso = "123456"; String utf8 = new String(iso.getBytes("ISO-8859-1"),"UTF-8"); // System.out.println(iso); MD5 getMD5 = new MD5(); //String iso; System.out.println(getMD5.GetMD5Code(utf8)); System.out.println(utf8); } }
(4) 需要和脚本进行关联
登录(输入用户名和密码,请求服务器,从服务器返回结果)-->提取token-->修改支付密码,使用httpclient方法请求
package com.pertest; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.codehaus.jettison.json.JSONObject; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; /** * Created by gh on 2018-12-21. */ public class HttpClientRequest { private static HttpClient httpClient = new DefaultHttpClient(); private HttpPost httppost; private HttpGet httpget; private HttpResponse response; private HttpResponse response2; private HttpEntity entity; private static String postResult = null; public static String sendPost(String url, String param) { try { HttpPost httpPost = new HttpPost(url); JSONObject jsonParam = new JSONObject(param); StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8"); entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); HttpResponse result = httpClient.execute(httpPost); HttpEntity entity2 = result.getEntity(); postResult = EntityUtils.toString(entity2,"UTF-8"); // System.out.println("Response content: " + postResult); EntityUtils.consume(entity2); } catch (Exception e) { e.printStackTrace(); } return postResult; } /** * 向指定URL发送GET方法的请求 * * @param url * 发送请求的URL * @param param * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return URL 所代表远程资源的响应结果 */ public static String sendGet(String url, String param) { String result = ""; BufferedReader in = null; try { String urlNameString = url + param; URL realUrl = new URL(urlNameString); // 打开和URL之间的连接 URLConnection connection = realUrl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立实际的连接 connection.connect(); // 获取所有响应头字段 Map<String, List<String>> map = connection.getHeaderFields(); // 遍历所有的响应头字段 for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); } // 定义 BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader( connection.getInputStream(),"UTF-8")); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送GET请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } }
import com.pertest.HttpClientRequest; import com.pertest.MD5; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; public class TestPayPwdMD5 { public static void main(String[] args) throws JSONException { //定义登录login_utl地址 String login_utl = "http://192.168.0.105:8080/mobile/api/user/login"; String login_data = "{\\"mobile\\": \\"15899558333\\", \\"password\\": \\"123456\\"}"; //调用HttpClientRequest方法 String login_result = HttpClientRequest.sendPost(login_utl, login_data); System.out.println("登录返回的信息: " + login_result); JSONObject jsonObj = new JSONObject(login_result); System.out.println(jsonObj); //获取token值 String token = (String) jsonObj.getJSONObject("data").get("token"); System.out.println(token); //把获取到的token值放在重置支付密码的请求参数里 //重置支付密码需要:密码 + md5 String resetPwd_url = "http://192.168.0.105:8080/mobile/api/user/resetpaypwd"; //调用MD5中的GetMD5Code方法 String newPwd = MD5.GetMD5Code("123456789"); //System.out.println(newPwd); //重置密码请求参数 String resetPwd_data = String.format("{\\"token\\": \\"%s\\", \\"password\\": \\"%s\\"}", token, newPwd); //System.out.println(resetPwd_data); String resetPwd_result = HttpClientRequest.sendPost(resetPwd_url, resetPwd_data); System.out.println(resetPwd_result); } }
3. 把算法打成jar包,提供给LoadRunner调用
4. 在LoadRunner里面新建一个java vuser协议的脚本
5. 在action()里面,直接把我们在IDEA里的代码复制粘贴
/* * LoadRunner Java script. (Build: _build_number_) * * Script Description: * */ import lrapi.lr; import com.pertest.HttpClientRequest; import com.pertest.MD5; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; public class Actions { //在这里也有三个部分:init(),action(),end() public int init() throws Throwable { return 0; }//end of init public int action() throws Throwable { //定义登录login_utl地址 String login_utl = "http://192.168.0.105:8080/mobile/api/user/login"; String login_data = "{\\"mobile\\": \\"15899558333\\", \\"password\\": \\"123456\\"}"; lr.start_transaction("登录"); //调用HttpClientRequest方法 String login_result = HttpClientRequest.sendPost(login_utl, login_data); System.out.println("登录返回的信息: " + login_result); JSONObject jsonObj = new JSONObject(login_result); //获取token值 String token = (String) jsonObj.getJSONObject("data").get("token"); int status = (int) jsonObj.getInt("code"); if (status == 0) { lr.end_transaction("登录", lr.PASS); } else { lr.end_transaction("登录", lr.FAIL); } //调试函数 lr.output_message("登录返回的结果: " + login_result); lr.error_message("登录返回的结果: " + login_result); lr.start_transaction("重置支付密码"); //把获取到的token值放在重置支付密码的请求参数里 //重置支付密码需要:密码 + md5 String resetPwd_url = "http://192.168.0.105:8080/mobile/api/user/resetpaypwd"; //调用MD5中的GetMD5Code方法 String newPwd = MD5.GetMD5Code("123456789"); //System.out.println(newPwd); //重置密码请求参数 String resetPwd_data = String.format("{\\"token\\": \\"%s\\", \\"password\\": \\"%s\\"}", token, newPwd); //System.out.println(resetPwd_data); String resetPwd_result = HttpClientRequest.sendPost(resetPwd_url, resetPwd_data); System.out.println(resetPwd_result); JSONObject jsonObj2 = new JSONObject(resetPwd_result); int status2 = (int) jsonObj.getInt("code"); if(status2 == 0) { lr.end_transaction("重置支付密码", lr.PASS); } else { lr.end_transaction("重置支付密码", lr.FAIL); } return 0; }//end of action public int end() throws Throwable { return 0; }//end of end }
需要注意的是,结束事务的时候要做if判断:
//获取token值 int status = (int) jsonObj.getInt("code"); if (status == 0) { lr.end_transaction("登录", lr.PASS); } else { lr.end_transaction("登录", lr.FAIL); }
6. 导入jar包,引入用到的方法信息。直接点击Runtime Settings或者F4,在ClassPath中输入以下内容,前两个是固定的,最后一个是jar包的位置
7. 编译,运行
8. HTTP协议的时候提示使用lr_out_message或lr_error_message
9. 怎么判断登录是否真正成功?会在场景里作为事务进行统计,判断,也就是我们前面说的
//获取token值 String token = (String) jsonObj.getJSONObject("data").get("token"); int status = (int) jsonObj.getInt("code"); if (status == 0) { lr.end_transaction("登录", lr.PASS); } else { lr.end_transaction("登录", lr.FAIL); }
10. 获取订单列表,是GET请求,需要对token进行urlcode编码
String tokenUTF8 = ""; //进行URLCode try { tokenUTF8 = URLEncoder.encode(token, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }
/* * LoadRunner Java script. (Build: _build_number_) * * Script Description: * */ import lrapi.lr; import com.pertest.HttpClientRequest; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; public class Actions { public int init() throws Throwable { return 0; }//end of init public int action() throws Throwable { //定义登录login_utl地址 String login_utl = "http://192.168.0.105:8080/mobile/api/user/login"; String login_data = "{\\"mobile\\": \\"15899558333\\", \\"password\\": \\"123456\\"}"; //调用HttpClientRequest方法 String login_result = HttpClientRequest.sendPost(login_utl, login_data); System.out.println("登录返回的信息: " + login_result); JSONObject jsonObj = new JSONObject(login_result); System.out.println(jsonObj); //获取token值 String token = (String) jsonObj.getJSONObject("data").get("token"); System.out.println(token); String tokenUTF8 = ""; //进行URLCode try { tokenUTF8 = URLEncoder.encode(token, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } String getOrderList_url = "http://192.168.0.105:8080/mobile/api/order/getorders"; String getOrderList_param = "?offset=0&" + "token=" + token; //把token替换成tokenUTF8 getOrderList_param = getOrderList_param.replace(token, tokenUTF8); String getResult = HttpClientRequest.sendGet(getOrderList_url, getOrderList_param); System.out.println("getResult: " + getResult); JSONObject getResultObj = new JSONObject(getResult); int getCode = (int) getResultObj.getInt("code"); System.out.println("getCode: " + getCode); return 0; }//end of action public int end() throws Throwable { return 0; }//end of end }
以上是关于java vuser协议的主要内容,如果未能解决你的问题,请参考以下文章