java - 使用 Jmeter Java API 以编程方式添加 header

标签 java json jmeter http-post

我正在尝试使用 Jmeter API 发送带有 json 的 http post 请求。我正在添加带有内容类型 header 的 HeaderManager,但它似乎没有按我的预期工作,因为我收到的请求是 Content-Type: application/x-www-form-urlencoded

    HTTPSamplerProxy httpSamplerProxy = new HTTPSamplerProxy();
    httpSamplerProxy.setDomain("localhost");
    httpSamplerProxy.setPort(8090);
    httpSamplerProxy.setPath("/");
    httpSamplerProxy.setMethod("POST");
    httpSamplerProxy.addEncodedArgument("Body Data", "{\"1\":\"2\"}", "");
    HeaderManager headerManager = new HeaderManager();
    headerManager.add(new Header("Content-type", "application/json"));
    httpSamplerProxy.setHeaderManager(headerManager);

我做错了什么?如何向 httpSampler 添加 header ?

最佳答案

您的测试存在几个问题:

  1. 不推荐使用 API 来创建 JMeter 测试,因为 API 可能会发生变化,因此您的测试可能会变得脆弱,并且无法在升级到下一个 JMeter 版本后继续存在。您应该使用 JMeter GUI 创建测试,一旦完成,您就可以自由地使用任何方法运行它。
  2. 您不应在同一主机上运行 JMeter 和被测应用程序,以避免相互干扰,JMeter 测试可能非常资源密集型。
  3. Content-Type header 中“键入”单词名称应该大写,这很重要。

如果您仍在寻找以编程方式添加 HTTP header 管理器的方法,请按照 comments 之一到Five Ways To Launch a JMeter Test without Using the JMeter GUI你应该采取一些不同的做法,即:

// JMeter Test Plan, basically JOrphan HashTree
HashTree testPlanTree = new HashTree();

// Create Header Manager
HeaderManager manager = new HeaderManager();
manager.add(new Header("Content-Type", "application/json"));
manager.setName(JMeterUtils.getResString("header_manager_title")); // $NON-NLS-1$
manager.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName());
manager.setProperty(TestElement.GUI_CLASS, HeaderPanel.class.getName());

// HTTP Sampler 
HTTPSamplerProxy httpSamplerProxy = new HTTPSamplerProxy();
httpSamplerProxy.setDomain("localhost");
httpSamplerProxy.setPort(8090);
httpSamplerProxy.setPath("/");
httpSamplerProxy.setMethod("POST");
httpSamplerProxy.setName("HTTP Request");
httpSamplerProxy.addEncodedArgument("Body Data", "{\"1\":\"2\"}", "");
httpSamplerProxy.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
httpSamplerProxy.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());


// Loop Controller
LoopController loopController = new LoopController();
loopController.setLoops(1);
loopController.setFirst(true);
loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
loopController.initialize();

// Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Example Thread Group");
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopController);
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());

// Test Plan
TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

// HTTP Request Sampler and Header Manager
HashTree requestHashTree = new HashTree();
requestHashTree.add(httpSamplerProxy, manager);

// Construct Test Plan from previously initialized elements
testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(requestHashTree);

关于java - 使用 Jmeter Java API 以编程方式添加 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40940569/

相关文章:

java - TCP/IP 等的套接字应该放在一个单独的类中吗? ( java )

java - org.springframework.web.reactive.function.UnsupportedMediaTypeException : Content type 'text/xml;charset=UTF-8' not supported for bodyType

java - 在 Java 应用程序中获取构建 ID

json - 在 Swift 中读取 JSON 输出

java - 将平面 JSON 结构转换为对象并添加另一个属性

java - Java 中的 XML 解析器应返回已使用的已定义类的 Arraylist

jquery - 通过 jquery 重复背景颜色模式?

jmeter - 如何处理Jmeter中的post方法进行负载测试?

jmeter - 如何使用开源工具捕获进程cpu、内存利用率

json - jmeter 无效的 UTF-8 中间字节