我正在尝试使用 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 ?
最佳答案
您的测试存在几个问题:
- 不推荐使用 API 来创建 JMeter 测试,因为 API 可能会发生变化,因此您的测试可能会变得脆弱,并且无法在升级到下一个 JMeter 版本后继续存在。您应该使用 JMeter GUI 创建测试,一旦完成,您就可以自由地使用任何方法运行它。
- 您不应在同一主机上运行 JMeter 和被测应用程序,以避免相互干扰,JMeter 测试可能非常资源密集型。
- 在 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/