Java JMeterAPI:如何向httpsampler添加post数据
我是jmeter的新手。我使用java和jmeter API编写了一个程序,只需对某个web服务发出get请求即可获得结果 但是,我无法向服务器发送POST请求。每当我在执行代码后打开.jtl文件时,都会将请求体设置为null 我的节目:Java JMeterAPI:如何向httpsampler添加post数据,java,eclipse,post,jmeter,Java,Eclipse,Post,Jmeter,我是jmeter的新手。我使用java和jmeter API编写了一个程序,只需对某个web服务发出get请求即可获得结果 但是,我无法向服务器发送POST请求。每当我在执行代码后打开.jtl文件时,都会将请求体设置为null 我的节目: package com.walmart.gls.PerformanceFramework; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutpu
package com.walmart.gls.PerformanceFramework;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.gui.LoopControlPanel;
import org.apache.jmeter.control.gui.TestPlanGui;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
public class AppTest {
public static void main(String args[]) throws FileNotFoundException,
IOException {
// Set jmeter home for the jmeter utils to load
File jmeterHome = new File("C:\\apache-jmeter-2.13\\");
String slash = System.getProperty("file.separator");
if (jmeterHome.exists()) {
File jmeterProperties = new File(jmeterHome.getPath() + slash
+ "bin" + slash + "jmeter.properties");
if (jmeterProperties.exists()) {
StandardJMeterEngine jmeter = new StandardJMeterEngine();
JMeterUtils.setJMeterHome(jmeterHome.getPath());
JMeterUtils.loadJMeterProperties(jmeterProperties.getPath());
// JMeterUtils.initLogging();// you can comment this line out to
// see extra log messages of i.e. DEBUG level
JMeterUtils.initLocale();
HashTree testPlanTree = new HashTree();
HTTPSamplerProxy httpsampler = new HTTPSamplerProxy();
httpsampler.setDomain("jsonplaceholder.typicode.com");
httpsampler.setPort(GlobalVariables.DEFAULT_HTTP_PORT);
httpsampler.setMethod("POST");
httpsampler.setPath("/posts");
HTTPArgument httpArgument = new HTTPArgument();
httpArgument
.setValue("{ data: {\"title\": \"venkatachalam\", \"body\": \"Venkata\", \"userId\": 3} }");
httpsampler.addTestElement(httpArgument);
httpsampler.setName("Posting POSTS");
httpsampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
httpsampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
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();
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Sample 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());
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());
testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan,
threadGroup);
threadGroupHashTree.add(httpsampler);
SaveService
.saveTree(
testPlanTree,
new FileOutputStream(
"C:\\Users\\rvj03\\Documents\\JMETER results\\jmeter_api_sample.jmx"));
Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault(
"summariser.name", "summary");
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}
String reportFile = "C:\\Users\\rvj03\\Documents\\JMETER results\\report.jtl";
String csvFile = "C:\\Users\\rvj03\\Documents\\JMETER results\\report.csv";
ResultCollector logger = new ResultCollector(summer);
logger.setFilename(reportFile);
ResultCollector csvlogger = new ResultCollector(summer);
csvlogger.setFilename(csvFile);
testPlanTree.add(testPlanTree.getArray()[0], logger);
testPlanTree.add(testPlanTree.getArray()[0], csvlogger);
jmeter.configure(testPlanTree);
jmeter.run();
System.out.println("Test completed. See " + jmeterHome + slash
+ "report.jtl file for results");
System.out.println("JMeter .jmx script is available at "
+ jmeterHome + slash + "jmeter_api_sample.jmx");
System.exit(0);
}
}
System.err
.println("jmeterHome property is not set or pointing to incorrect location");
System.exit(1);
}
}
更改这些行:
HTTPArgument httpArgument = new HTTPArgument();
httpArgument
.setValue("{ data: {\"title\": \"venkatachalam\", \"body\": \"Venkata\", \"userId\": 3} }");
httpsampler.addTestElement(httpArgument);
对下列事项:
httpsampler.addNonEncodedArgument("","{ data: {\"title\": \"venkatachalam\", \"body\": \"Venkata\", \"userId\": 3} }","");
httpsampler.setPostBodyRaw(true);
它应该对你有好处。参见方法JavaDoc
我还建议添加一个值为
application/json
的tosendContent-Type
头,仍然不走运。每当我打开由代码生成的.jtl时,它不会给出任何请求正文(请求正文为null)。但当我导入.jmx文件并在JMeterGUI中运行时,它会运行并给出结果。有点令人震惊。请帮助我如何添加标题到我的采样器。我试过了,但它不起作用。当我在Jmeter中打开.jtl文件时,我的请求主体为空,没有任何post参数。至少应该说是发布到某个uri,对吗?