性能测试工具-Ngrinder使用之post请求
post请求--groovy,使用groovy自带的httpclitent
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | 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"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步