jmeter中的java请求 用httpclient写的http请求 及参数化
来源:互联网 发布:linux交叉编译工具链 编辑:程序博客网 时间:2024/05/29 18:16
首先,jmeter中的sample的原理:
jmeter 中的java 请求,sample 原理,java testjmeter自带的包,把包放在类路径下面,通过反射机制,通过反射机制扫出来。
先导入五个jar包
package com.young.testing91;import java.io.IOException;import org.apache.http.client.ClientProtocolException;import org.apache.jmeter.config.Arguments;import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;import org.apache.jmeter.samplers.SampleResult;public class addComputerInfoClient extends AbstractJavaSamplerClient {private static String uriPOST = "http://localhost:9000/computers";@Overridepublic Arguments getDefaultParameters() {Arguments arguments = new Arguments();arguments.addArgument("val", "");return arguments;}public SampleResult runTest(JavaSamplerContext arg0) {// 添加事务 new SampleResultSampleResult result = new SampleResult();result.sampleStart();String value = arg0.getParameter("val");try {int responseCode = SendHttpRequest.sendPostRequest(value);if (responseCode == 200) {result.setSuccessful(true);} else {result.setSuccessful(false);}} catch (ClientProtocolException e) {result.setSuccessful(false);e.printStackTrace();} catch (IOException e) {result.setSuccessful(false);e.printStackTrace();} catch (Exception e) {e.printStackTrace();}result.sampleEnd();return result;}public static void main(String[] args) {new addComputerInfoClient().runTest(new JavaSamplerContext(new Arguments()));}}
package com.young.testing91;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;public class SendHttpRequest {// send url is http://localhost:9000/computers?f=ACEprivate static String uriPOST = "http://localhost:9000/computers";public static int sendPostRequest(String value) throws ClientProtocolException, IOException {CloseableHttpClient httpclient = HttpClients.createDefault();HttpPost httpPost = new HttpPost(uriPOST);List<NameValuePair> params = new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("name", value));params.add(new BasicNameValuePair("introduced", "2017-2-19"));params.add(new BasicNameValuePair("discontinued", "2017-2-19"));params.add(new BasicNameValuePair("company", "22"));httpPost.setEntity(new UrlEncodedFormEntity(params));long startTime = System.currentTimeMillis();CloseableHttpResponse httpResponse = httpclient.execute(httpPost);int responseCode = httpResponse.getStatusLine().getStatusCode();System.out.println(responseCode + " ," + (System.currentTimeMillis() - startTime) + "ms");return responseCode;
在jmeter里,多个参数用这个方法增加
arguments.addArgument("val", "");
导包的时候用runnable JAR file, 此方法可以把依赖的类都导出
在ecplise中的httpclient jar包版本要于jmeter中的版本一直或是比jmeter中的高,否则会报错java.lang.NoSuchFieldError: INSTANCE。
把导出的jar包放到jmeter中 lib 中的ext目录下,然后在jmeter中新建一个java请求,就可以选择刚才导入的jar包,选择参数
0 0
- jmeter中的java请求 用httpclient写的http请求 及参数化
- jmeter测试http请求之参数化
- jmeter中http请求参数化
- Jmeter(三)-简单的HTTP请求(参数化)
- 用Java的HttpClient写一个简单的http请求和返回
- 用Java的HttpClient写一个简单的http请求和返回
- 用Java的HttpClient写一个简单的http请求和返回
- Jmeter写Java请求的过程
- jmeter java请求参数配置
- JMeter简单的GET请求,参数化
- Jmeter 参数化请求实例
- 使用Apache中的HttpClient的实例CloseableHttpClient进行http请求
- Jmeter对于请求参数封装及使用
- Jmeter:正则表达式提取器上一个http请求报文内容作为下一个请求的参数
- 【JAVA】通过HttpClient发送HTTP请求的方法
- java使用HttpClient模拟发送http的get、post请求
- Jmeter测试HTTP请求
- jmeter-http请求
- hrbust1003 Tiles of Tetris, NOT!
- Java 动态生成复杂 Word
- JAVA单例模式
- CocoaPods的安装以及遇到的坑
- 如何自定义view布局,然后添加到LinearLayout中
- jmeter中的java请求 用httpclient写的http请求 及参数化
- JSP生命周期
- cpu 拓补结构
- Android代码优化的10条小建议
- 某云RDS数据库本地恢复--服务器重启数据库打开丢失问题--亲验
- scrollerView内的数据每次刷新都会滚动到底部处理
- 最全的10G光模块知识
- Python之OS模块详解
- MongoDB学习笔记