【问题标题】:Beanshell code to perform HTTP post request instead of http sampler用于执行 HTTP 发布请求而不是 http 采样器的 Beanshell 代码
【发布时间】:2017-09-28 09:59:48
【问题描述】:

我正在编写 Beanshell 代码来执行 HTTP 发布请求,而不是使用 HTTP 采样器

我的代码:

     import org.apache.http.HttpResponse;
     import org.apache.http.NameValuePair;
     import org.apache.http.entity.StringEntity;
     import org.apache.http.client.HttpClient;
     import org.apache.http.client.entity.UrlEncodedFormEntity;
     import org.apache.http.client.methods.HttpGet;
     import org.apache.http.client.methods.HttpPost;
     import org.apache.http.impl.client.HttpClientBuilder;

     //Previous Response  time
     int responseTime = Integer.parseInt(String.valueOf(prev.getTime()));
     //Previous Response  Size
     int size = Integer.parseInt(String.valueOf(prev.getResponseData().length));
     //log.info("Reponse time " +responseTime);
     //Previous Response Status Code
     int responseCode = Integer.parseInt(String.valueOf(prev.getResponseCode()));
     String testId =String.valueOf(${__time(yyyyMMdd)});
     String executionTimestamp =String.valueOf(${__time(yyyyMMdd)});
     //double int executionTimestamp = Integer.parseInt(${__time()});
     String Transaction="Transaction_Login";
     String applicationName ="Login";
     String conversationId ="Sampledata";
     String Status="";
     String msg="mesages";
     //Set Status according to responseCode
        if(responseCode=="200"){
     Status="OK";
        }else {
     Status="Fail";
        }

     HttpClient httpClient = HttpClientBuilder.create().build();
     try{

     StringEntity params =new StringEntity("\"testId\":\""+testId+"\",\"TransactionName\":\""+Transaction+"\",\"applicationName\":\""+applicationName+"\",\"conversationId\":\""+conversationId+"\",\"size\":\""+size+"\",\"status\":\""+Status+"\",\"messages\":\""+msg+"\",\"executionTimestamp\":\""+executionTimestamp+"\",\"timeTaken\":\""+responseTime+"\"");



     HttpPost request = new HttpPost("http://servername/transactionrecorder/");
     request.addHeader("content-type", "application/json");
     request.setEntity(params);
     HttpResponse response = httpClient.execute(request);
     log.info("response :" +response);
     }catch(Exception e){
     log.info("ExceptionKPI :" +e);
     }

错误响应:

 jmeter.util.BeanShellTestElement: response :HttpResponseProxy{HTTP/1.1 415 
 [Accept: application/octet-stream, text/plain;charset=ISO-8859-1, application/xml, text/xml,
 application/x-www-form-urlencoded, application/*+xml, multipart/form-data, application/json;charset=UTF-8, application/*+json;charset=UTF-8, */*, Content-Type: text/html;charset=utf-8, Content-Language: en, Content-Length: 1089, Date: Thu, 28 Sep 2017 09:46:01 GMT] ResponseEntityProxy{[Content-Type: text/html;charset=utf-8,Content-Length: 1089,Chunked: false]}} 

【问题讨论】:

    标签: post jmeter http-post beanshell


    【解决方案1】:

    您需要发送一个带有大括号{}的有效JSON,例如:

     StringEntity params =new StringEntity("{\"testId\":\""+testId+"\",\"TransactionName\":\""+Transaction+"\",\"applicationName\":\""+applicationName+"\",\"conversationId\":\""+conversationId+"\",\"size\":\""+size+"\",\"status\":\""+Status+"\",\"messages\":\""+msg+"\",\"executionTimestamp\":\""+executionTimestamp+"\",\"timeTaken\":\""+responseTime+"\"}");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2021-04-03
      • 2016-09-09
      相关资源
      最近更新 更多