首页 > 其他分享 >Jmeter - BeanSell 后置处理器 结合 HttpClient 使用

Jmeter - BeanSell 后置处理器 结合 HttpClient 使用

时间:2024-05-14 14:09:44浏览次数:12  
标签:http BeanSell client Cookie import apache org Jmeter HttpClient

背景:在后置处理器中发送POST 请求,请求体为JSON数据

疑问:
1.如果获取Cookie?

2.HttpClient 怎么发送POST?

3.HttpClient 怎么添加Cookie?

解决:
1.如果获取Cookie?

import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.Cookie;

// 获取当前线程的Cookie管理器
CookieManager manager = ctx.getCurrentSampler().getCookieManager();

// 遍历所有Cookie
for (int i = 0; i < manager.getCookieCount(); i++) {
    Cookie cookie = manager.get(i);
    log.info("Cookie Name: " + cookie.getName() + " Cookie Value: " + cookie.getValue());
}

2.HttpClient 怎么发送POST?

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.HttpClientBuilder;
import org.apache.http.util.EntityUtils;


String json = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
String url = "http://httpbin.org/post";

CloseableHttpClient httpClient = HttpClientBuilder.create().build();
try {
    StringEntity entity = new StringEntity(json);
    entity.setContentType("application/json");

    // 创建POST请求
    HttpPost post = new HttpPost(url);
    post.setEntity(entity);

    // 执行请求
    CloseableHttpResponse response = httpClient.execute(post);

    // 获取响应内容
    String result = EntityUtils.toString(response.getEntity());
    System.out.println(result);

    // 关闭响应
    response.close();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        httpClient.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

https://jmeter.apache.org/api/org/apache/jmeter/protocol/http/control/Cookie.html

3.HttpClient 怎么添加Cookie?

import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.util.EntityUtils;
 
import java.io.IOException;
 
public class HttpClientCookieExample {
    public static void main(String[] args) {
        // 创建一个Cookie存储对象
        CookieStore cookieStore = new BasicCookieStore();
 
        // 创建一个包含cookie存储的HttpClientContext对象
        HttpClientContext context = HttpClientContext.create();
        context.setCookieStore(cookieStore);
 
        // 添加一个cookie
        BasicClientCookie cookie = new BasicClientCookie("name", "value");
        cookie.setDomain("example.com");
        cookie.setPath("/");
        cookieStore.addCookie(cookie);
 
        // 使用HttpClient
        try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build()) {
            // 构建HttpGet对象
            HttpGet httpGet = new HttpGet("http://example.com");
 
            // 执行请求
            CloseableHttpResponse response = httpClient.execute(httpGet, context);
 
            // 打印响应内容
            System.out.println(EntityUtils.toString(response.getEntity()));
 
            // 关闭响应对象
            response.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

标签:http,BeanSell,client,Cookie,import,apache,org,Jmeter,HttpClient
From: https://www.cnblogs.com/czzz/p/18191177

相关文章

  • jmeter函数记录
    1、Jmeter时间偏移函数:__timeShift2、#vars保存对象类型(包括List,Map,closure之类),使用如下方法:#vars.putObject("object_name",Object);3、Jmeter执行文件与上传文件在同一文件夹下,获取相对路径先用BeanShell函数获取当前路径:importorg.apache.jmeter.services.File......
  • Jmeter
    一、Jmeter-绝对并发之集合点设置https://blog.csdn.net/weixin_42162451/article/details/116806130二、Jmeter-常用插件将附件的两个jar包copy到目录:C:\apache-jmeter-5.4.3\apache-jmeter-5.4.3\lib\exthttps://blog.csdn.net/dreamchasering/article/details/77685637......
  • JMeter + ant + Jenkins 接口测试持续集成
    JMeter+ant+Jenkins接口测试持续集成操作系统:linux环境变量地址jdk11https://www.oracle.com/java/technologies/downloads/jmeter5.6https://jmeter.apache.org/ant1.10.14https://ant.apache.org/bindownload.cgiJenkins2.414.1https://mirrors......
  • jmeter无图形界面执行测试并生成报告
    一.命令行:jemeter-n-txxx.jmx-lMMM.jtl-e-oNNN二.命令行参数解释:2.1运行参数-n无图形界面执行-t指定要执行的脚本路径xxx.jmx,必须指定,格式为jmx文件(-txxx.jmx)-l指定运行过程中生成数据文件路径,必须指定,格式为jtl文件,文件名随......
  • jmeter以命令行模式运行:非GUI界面
    *`-n`:表示非GUI模式运行:命令行模式运行jmeter脚本*`-t`:要执行的jmeter脚本(JMX):a.默认执行当前路径下的脚本,b.或执行指定路径下的脚本*`-l`:生成结果文件(JTL):a.默认在当前路径下生成JTL文件,b.或在指定路径下生成JTL文件*`-e`:生成HTML报告*`-o`:HTML报告的文件夹路径......
  • jmeter插件管理器安装-Plugins Manager
    有些函数是jmeter自带函数,有些函数是自定义的需要通过插件安装的,例如jmeter没有自带base64加密函数,若要使用该函数,可以通过插件安装自定义函数1.下载jmeter插件管理器:https://jmeter-plugins.org/wiki/PluginsManager/ 2.重启在jmeter,在“选项”下显示插件管理器"Plugins......
  • Jmeter调用java代码
    加密:MD5、Base64、SHA、RSA、签名混合加密:jmeter的md5加密函数:BeanShell调用java代码: 调用jar包:1)在测试计划中引入jar包2)调用代码 ......
  • Jmeter内存溢出:java.lang.OutOfMemoryError: Java heap space解决思路
    一、问题原因用JMeter压测,有时候当模拟并发请求较大或者脚本运行时间较长时,JMeter会停止,报OOM(内存溢出)错误。原因是JMeter是一个纯Java开发的工具,内存由java虚拟机JVM管理,当内存回收不及时,堆内存不足时,就会报内存溢错误。概念补充:内存泄露:应用使用资源之后没有及时释放,导致应......
  • RestClient C# 举例 是用jsonbody ,并列出httpclient 等价的方式
    以下是使用RestSharp发送POST请求并附带JSON请求体的示例,以及相应的使用HttpClient的等价方式:首先,使用RestSharp:usingSystem;usingRestSharp;usingNewtonsoft.Json;classProgram{staticvoidMain(string[]args){//创建RestClient实......
  • HttpClient 进行soap请求
    当你在使用C#的HttpClient进行SOAP请求时,确保你的代码类似于以下示例:usingSystem;usingSystem.Net.Http;usingSystem.Text;usingSystem.Threading.Tasks;classProgram{staticasyncTaskMain(string[]args){try{//创建H......