性能测试工具-Ngrinder使用之post请求
post请求--groovy,使用groovy自带的httpclitent
package org.ngrinder;
import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.*
import static org.hamcrest.Matchers.*
import net.grinder.plugin.http.HTTPRequest
import net.grinder.plugin.http.HTTPPluginControl;
import net.grinder.script.GTest
import net.grinder.script.Grinder
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
/**
* A simple example using the HTTP plugin that shows the retrieval of a
* single page via HTTP.
*
* This script is automatically generated by ngrinder.
*
* @author admin
*/
@RunWith(GrinderRunner)
class TestRunner {
public static GTest test
public static HTTPRequest request
public static NVPair[] paras =[];
@BeforeProcess
public static void beforeProcess() {
HTTPPluginControl.getConnectionDefaults().timeout = 6000
test = new GTest(1, "wiki.qianbao-inc.com")
request = new HTTPRequest()
// 设置请求参数
List<NVPair> paramList = new ArrayList<NVPair>()
paramList.add(new NVPair("os_username", "zhangwei6@qianbao.com"))
paramList.add(new NVPair("os_password", "zhangwei6@qianbao.com"))
paras = paramList.toArray()
test.record(this,"test");
// 记录日志
grinder.logger.info("before process.");
}
@BeforeThread
public void beforeThread() {
// 配置延迟报告统计结果
grinder.statistics.delayReports=true;
// 记录日志
grinder.logger.info("before thread.");
}
@Before
public void before() {
grinder.logger.info("before thread. init headers and cookies")
}
@Test
public void test(){
HTTPResponse result = request.POST("http://wiki.qianbao-inc.com/dologin.action",paras)
if (result.statusCode == 301 || result.statusCode == 302) {
grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode);
} else {
assertThat(result.statusCode, is(200));
println result.getText().toString()
}
}
}
post请求,使用apache的httpclient,直接调用方法即可
content-type="application/x-www-form-urlencoded"
public static String postHttp(List<BasicNameValuePair> paras, String url){
CloseableHttpClient httpClient = null;
HttpPost httppost = null;
HttpEntity reqEntity = null;
String responseStr = null;
httpClient = HttpClients.createDefault();
try {
reqEntity = new UrlEncodedFormEntity(paras);
httppost = new HttpPost(url);
httppost.setHeader("Content-type", "application/json");
httppost.setEntity(reqEntity);
CloseableHttpResponse response = httpClient.execute(httppost);
HttpEntity responseEntity = response.getEntity();
if (null != responseEntity && 200 == response.getStatusLine().getStatusCode()){
responseStr = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
System.out.println("【Test case】The responseString is :" + responseStr);
}
EntityUtils.consume(reqEntity);
EntityUtils.consume(responseEntity);
response.close();
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
return responseStr;
}
content-type="application/json"
public static String postHttp(String reqData,String url){
CloseableHttpClient httpClient = null;
HttpPost httppost = null;
StringEntity reqEntity = null;
String responseStr = null;
String times = String.valueOf(System.currentTimeMillis());
String key = "qb_api_center";
StringBuilder stringBuilder = new StringBuilder().append(times).append(key);
String authorization = DigestUtils.md5Hex(stringBuilder.toString());
httpClient = HttpClients.createDefault();
try {
reqEntity = new StringEntity(reqData);
httppost = new HttpPost(url);
httppost.setHeader("Content-type", "application/json");
httppost.setHeader("authorization", authorization);
httppost.setHeader("time", times);
httppost.setEntity(reqEntity);
CloseableHttpResponse response = httpClient.execute(httppost);
HttpEntity responseEntity = response.getEntity();
if (null != responseEntity && 200 == response.getStatusLine().getStatusCode()){
responseStr = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
System.out.println("【Test case】The responseString is :" + responseStr);
}
EntityUtils.consume(reqEntity);
EntityUtils.consume(responseEntity);
response.close();
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
return responseStr;
}
content-type="multipart/form-data"
public static String postHttp(HttpEntity requestEntity, String url, String platform){
CloseableHttpClient httpClient;
String responseString = null;
CloseableHttpResponse httpResponse = null;
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("source", "pc");
httpPost.setHeader("version", "version");
httpPost.setHeader("platform", platform);
httpPost.setEntity(requestEntity);
if (StringUtils.startsWith(url, "https")) {
httpClient = wrapClient();
} else {
httpClient = HttpClients.createDefault();
}
try {
httpResponse = httpClient.execute(httpPost);
System.out.println("[Test Case] httpResponse.getStatusLine() = " + httpResponse.getStatusLine());
HttpEntity responseEntity = httpResponse.getEntity();
if (null != responseEntity && 200 == httpResponse.getStatusLine().getStatusCode()) {
responseString = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
}
EntityUtils.consume(requestEntity);
EntityUtils.consume(responseEntity);
httpResponse.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
return responseString;
}
浙公网安备 33010602011771号