Jmeter - BeanSell 后置处理器 结合 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();
}
}
}
HttpClient 依赖:
本文来自博客园,作者:chuangzhou,转载请注明原文链接:https://www.cnblogs.com/czzz/p/18191177