接口自動化測試,今天遇到POST接口帶參數(shù),參數(shù)在url上,發(fā)現(xiàn)原來的工具類中沒有該方法,重新調(diào)試加上。
?doPost方法如下:
/**
* 發(fā)送POST請求,帶請求參數(shù)
* @param url 請求地址
* @param headers 請求頭
* @param params 請求params
* @return getHttpClientResult getHttpClientResult方法
*/
//public static String doPost(String url, JSONObject json,String token) {
public HttpClientResult doPost(String url, Map<String, String> headers,Map<String,String> params){
CloseableHttpClient httpClient = null;
CloseableHttpResponse httpResponse=null;
HttpPost httpPost=null;
HttpClientResult httpClientResult=null;
//String result="";
try {
//1.創(chuàng)建httpClient對象
//httpClient = HttpClients.createDefault();
httpClient =createHttpsClient(filePath,passWord);
// 創(chuàng)建訪問的地址
URIBuilder uriBuilder = new URIBuilder(url);
if (params != null) {
Set<Map.Entry<String, String>> entrySet = params.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
uriBuilder.setParameter(entry.getKey(), entry.getValue());
}
}
//2.創(chuàng)建httpPost對象
httpPost = new HttpPost(uriBuilder.build());
//httpPost = new HttpPost(url);
//2.1對象設(shè)置請求頭
//httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
//httpPost.setHeader("Authorization",token);
packageHeader(headers, httpPost);
//2.2封裝請求參數(shù)
//packageParam(params, httpPost);
//2.3對象設(shè)置請求和傳輸超時時間
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(SOCKET_TIME_OUT).setConnectTimeout(CONNECT_TIME_OUT).build();
httpPost.setConfig(requestConfig);
//3.使用httpClient發(fā)起請求并響應獲取response
/*
httpResponse = httpClient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
//4.解析響應,獲取數(shù)據(jù)
//判斷狀態(tài)碼是否是200
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(entity,ENCODING);
//System.out.println(result);
}
//System.out.println(httpResponse.getStatusLine());
//System.out.println(httpResponse.getStatusLine().getStatusCode());
//System.out.println(httpResponse.getProtocolVersion());
//System.out.println(HttpStatus.SC_OK);
*/
httpClientResult=getHttpClientResult(httpResponse,httpClient,httpPost);
}
catch (Exception e){
log.error(e.toString());
}
finally {
//5.釋放資源
release(httpResponse, httpClient);
}
return httpClientResult;
}
調(diào)用:
String appId="123456789";
Map<String, String> headers=new HashMap<>();
headers.put("Content-Type","application/json;charset=UTF-8");
headers.put("Authorization","Bearer "+token);
@Test
public void c06_pluginDeployment() {
for (ApiDataBean data : listExcel) {
if (data.getApiName().trim().equals("pluginDeployment")) {
HttpsClientUtils httpUtils = new HttpsClientUtils();
String host=BaseRequest.getHost();//這里例如:https://IP
String reqUrl = host + data.getUrl(); //https://IP+接口url
System.out.println(reqUrl);
HttpClientResult httpResult= null;
try {
Map<String, String> params=new HashMap<>();
params.put("appId",appId);
System.out.println(appId);
httpResult = httpUtils.doPost(reqUrl, headers,params);
} catch (Exception e) {
e.printStackTrace();
log.error(String.valueOf(e));
}
String resultContent = httpResult.getContent();
//int code = JsonPath.read(resultContent, "$.code");
log.info(resultContent);
}
}
}
參考:文章來源:http://www.zghlxwxcb.cn/news/detail-741959.html
[Java 接口自動化框架]httpclient4.5.3(CloseableHttpClient) https的工具類HttpsClientUtils文章來源地址http://www.zghlxwxcb.cn/news/detail-741959.html
到了這里,關(guān)于HTTP POST接口帶參數(shù)的HttpClient請求方法和調(diào)用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!