課程
/后端開發
/Java
/使用java構建和維護接口自動化測試框架
post請求的參數如何添加?老師的視頻中只是一個字符串,能否幫忙指出如何添加post方法攜帶的參數?謝謝老師
2018-02-07
源自:使用java構建和維護接口自動化測試框架 2-4
正在回答
public static String httpPost(String url,List<BasicNameValuePair> list) {
String result = null;
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000) ? //設置連接超時時間
.setConnectionRequestTimeout(5000) // 設置請求超時時間
.setSocketTimeout(5000)
.setRedirectsEnabled(true)//默認允許自動重定向
.build();
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
//設置post請求參數
httpPost.setEntity(entity);
HttpResponse httpResponse = closeableHttpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(httpResponse.getEntity());
}
} catch (Exception e) {
e.printStackTrace();
}finally {
closeableHttpClient.close();
} catch (IOException e) {
return result;
舉報
初識接口自動化框架
1 回答發送post請求失敗
2 回答post請求發生錯誤500錯誤
2 回答httpclient如何做csrf token校驗
1 回答請問老師你視頻中提到的用RobotFramework做單元測試,是如何進行的
1 回答CDspace發送請求失敗,求助老師
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2018-08-01
public static String httpPost(String url,List<BasicNameValuePair> list) {
String result = null;
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000) ? //設置連接超時時間
.setConnectionRequestTimeout(5000) // 設置請求超時時間
.setSocketTimeout(5000)
.setRedirectsEnabled(true)//默認允許自動重定向
.build();
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
//設置post請求參數
httpPost.setEntity(entity);
HttpResponse httpResponse = closeableHttpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(httpResponse.getEntity());
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}