简体   繁体   中英

How to capture response of a Bean shell sampler

在此处输入图片说明

Above is my code i have written to hit a web service whose endpoint is expecting a Byte Stream object. I am able to do that but i am not getting any response. I have to test the response. Though i am getting 200 ok but a string is sent in response that i am not getting. 在此处输入图片说明

在此处输入图片说明

And the response is blank 在此处输入图片说明

How can I get the response ?

You can add your output by using the SampleResult object:

String output = "...";

SampleResult.setResponseData( output );
SampleResult.setDataType( org.apache.jmeter.samplers.SampleResult.TEXT );
  1. In order to read server's response you need to use URLConnection.getInputStream() method , not OutputStream
  2. In order to convert stream to string you can use IOUtils.toString() method
  3. In order to return data you can use return keyword
  4. Minimal working code is below, adjust as per your needs:

     import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import org.apache.commons.io.IOUtils; URL url = new URL("http://example.com"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); String response = IOUtils.toString(con.getInputStream(), StandardCharsets.UTF_8); return response; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM