java - Jmeter API : how to add post data to httpsampler

标签 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.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);

它应该能帮到你。请参阅HTTPSamplerBase.addNonEncodedArgument()方法 JavaDoc.

我还建议添加 HTTP Header Manager发送值为 application/jsonContent-Type header

关于java - Jmeter API : how to add post data to httpsampler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37699034/

相关文章:

android - 无法在 eclipse for android 中导入为工具提示提供的库项目和示例

python - 使用python请求登录网站

jquery - 从 Jquery 调用方法后面的代码

wordpress - 尝试使用 Oauth 1.0 在 Wordpress API 中创建帖子时出现 401

java - 如何使用 HQL (Hibernate) 查询日期的平均天数差异?

java.lang.UnsatisfiedLinkError : Native Library sqljdbc_auth. dll 已经加载到另一个类加载器中

java - 将 JSON 数组发布到 Android 中的 Web 服务

android - 如何在 Eclipse 中正确安装 mysql 连接器?

java - 使用 maven 插件将静态资源上传到 Amazon s3 服务器

java - Openshift Java SQLite,无法打开/创建数据库文件