1.
Eclipse中调试Java脚本,脚本调试通过后,打成jar包;
2. 新建lr脚本,选择Java vuser协议;
3. Run-time Settings中Classpath设置jar包,没用的jar包一律删除,或不选择,否则会出现错误;然后添加Java脚本所需要的jar包;
4. jar可以放在Java vuser根目录,直接使用;
5. 写Java脚本,调试。
6. controller中并发时,使用Run Vuser as a process模式并发。
Java程序: public static void main(String[] args) { 。。。。}
Lr脚本:
import com.p2p.test.*;
import lrapi.lr;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class Actions
{
public static HttpClient httpClient = new HttpClient();
public int init() throws Throwable {
return 0;
}//end of init
public int action() throws Throwable {
// HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(
//http://172.30.1.44:8899/entruSettleNotify
//http://10.7.3.94:8899/frontAPI/entruSettleNotify
"http://172.30.1.44:8899/entruSettleNotify");//http://172.30.1.44:8899/entruSettleNotify http://10.7.3.94:8899/frontAPI/entruSettleNotify
// 设置用户浏览器为MicrosoftIE6.0
postMethod.setRequestHeader("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
// 设置编码
postMethod.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
// 设置请求参数
//String BatchNo = "20140707109257829643";
//String childBatchNo = "140387";
String reid = "2233344450"; // 还款用 要改
try{
String xmldsES0003="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
+ "<message xmlns=\"http://www.w3school.com.cn\"> "
+ "<head> "
+ "<version>01</version> "
+ "<type>0002</type> "
+ "<channelNo>HM</channelNo> "
+ "<tradDate>20140515</tradDate> "
+ "<tradTime>141558</tradTime> "
+ "<tradFlowNo>ES000320140515100591</tradFlowNo> "
+ "<tradNo>ES0003</tradNo> "
+ "</head> "
+ "<body> "
+ "<merchantNo>1092</merchantNo> "
//20140712109264712593
//20140712109289216534
+ "<batchNo>20140715109258670149</batchNo> " // 要改
+ "<submitDate>790538</submitDate> "
+ "<batchStatus>06</batchStatus> "
+ "<batchUse>01</batchUse> "
+ "<batchRemark></batchRemark> "
+ "<batchSumCount>1</batchSumCount> "
+ "<batchSumAmt>100</batchSumAmt> " // 要改
+ "<batchSumSuccCount>0</batchSumSuccCount> "
+ "<batchSumSuccAmt>0.00</batchSumSuccAmt> "
+ "<list name=\"batchList\"> "
+ "<row> "
//961754
//160759
+ "<childBatchNo>578936</childBatchNo> " // 要改
//102100009545
//305100001057
+ "<cnaps>102100009545</cnaps> "
+ "<bankAcc>1</bankAcc> "
+ "<rcvName>1</rcvName> "
+ "<acctAttribute>02</acctAttribute> "
//中国工商银行股份有限公司北京玉林支行
//中国民生银行股份有限公司北京中关村支行
+ "<branchName>中国工商银行股份有限公司北京玉林支行</branchName> "
+ "<branchProvince></branchProvince> "
+ "<branchCity></branchCity> "
+ "<branchSite></branchSite> "
+ "<amount>100</amount> " // 要改
+ "<currencyType>CNY</currencyType> "
+ "<state>02</state> "
+ "<procTime>20140515131232</procTime> "
+ "<remark>转账成功</remark> "
+ "</row> "
+ "</list> "
+ "</body>"
+ "</message>";
String xmldsES0006="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
+ "<message xmlns=\"http://www.w3school.com.cn\"> "
+ "<head> "
+ "<version>01</version> "
+ "<type>0002</type> "
+ "<channelNo>HM</channelNo> "
+ "<tradDate>20140613</tradDate> "
+ "<tradTime>105500</tradTime> "
+ "<tradFlowNo>ES000620140410100821</tradFlowNo> "
+ "<tradNo>ES0006</tradNo> "
+ "</head> "
+ "<body> "
+ "<rechargeId>"+reid+"</rechargeId> "
+ "<merchantNo>1054</merchantNo> "
+ "<balanceId>161</balanceId> "
+ "<rechargeAmt>1003315.07</rechargeAmt> "
+ "<tradeDate>20140621</tradeDate> "
+ "<state>1</state> "
+ "<businessType>2</businessType> "
+ "<info>161</info> "
+ "<remark>161</remark> "
+ "</body>"
+ "</message>";
String xmlds = xmldsES0003;//xmldsES0003; //要改
//
String aa = xmlds+"111111";
String mac=DigestUtils.md5Hex(aa);
// System.out.println("上送报文:"+xmlds);
// System.out.println("上送摘要:"+mac);
postMethod.addParameter("tradNo","ES0003"); // 要改 03放款 06还款
postMethod.addParameter("type","0002");//0002
postMethod.addParameter("data",xmlds);
postMethod.addParameter("mac",mac);
}catch (Exception e) {
e.printStackTrace();
}
String bankResultValue = "";
// 获取执行结果
int status = 0;
try {
status = httpClient.executeMethod(postMethod);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(status != 200){
System.out.println("连接失败");
}else{
//连接成功,获取response中的xml数据
try {
bankResultValue = new String(postMethod.getResponseBody(), "UTF-8");
System.out.println("返回结果:"+bankResultValue);
// System.out.println("第"+args[0]+"次返回结果:"+bankResultValue);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// return bankResultValue;
//
//
}
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}