问题
Use Bean Shell sampler to hit simple Google search, do I need a code for this?
回答1:
It is better to use HTTP Request sampler, however if for some reason you need to do it in Beanshell here is the example script:
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://google.com");
HttpResponse response = httpclient.execute(httpGet);
String responseString = EntityUtils.toString(response.getEntity());
String responseCode = String.valueOf(response.getStatusLine().getStatusCode());
String responseCode = String.valueOf(response.getStatusLine().getStatusCode());
return responseString;
See the following reference material:
- HttpClient Quick Start
- How to Use BeanShell: JMeter's Favorite Built-in Component
来源:https://stackoverflow.com/questions/36276916/how-to-read-a-web-page-using-bean-shell-in-jmeter