TTP協(xié)議的接口測試中曙聂,使用到最多的就是GET請求與POST請求,其中POST請求有FORM參數(shù)提交請求與RAW請求吃引,下面我將結(jié)合HttpClient來實現(xiàn)一下這三種形式:
一.GET請求: GET請求時筹陵,參數(shù)一般是寫在鏈接上的,代碼如下:
publicvoidget(String url){
CloseableHttpClient httpClient =null;
HttpGet httpGet =null;
try{
httpClient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();
httpGet =newHttpGet(url);
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
System.out.println(EntityUtils.toString(httpEntity,"utf-8"));
}catch(ClientProtocolException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally{
try{
if(httpGet!=null){
httpGet.releaseConnection();
}
if(httpClient!=null){
httpClient.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
如果想把參數(shù)不寫在鏈接上镊尺,單獨的傳進去朦佩,則可以這樣:
publicvoidget(String url, Map params){
CloseableHttpClient httpClient =null;
HttpGet httpGet =null;
try{
httpClient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();
String ps ="";
for(String pKey : params.keySet()) {
if(!"".equals(ps)){
ps = ps +"&";
}
ps = pKey+"="+params.get(pKey);
}
if(!"".equals(ps)){
url = url +"?"+ ps;
}
httpGet =newHttpGet(url);
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
System.out.println(EntityUtils.toString(httpEntity,"utf-8"));
}catch(ClientProtocolException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally{
try{
if(httpGet!=null){
httpGet.releaseConnection();
}
if(httpClient!=null){
httpClient.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
二. POST請求的表單提交方式,代碼如下:
publicvoidpost(String url, Map params){
CloseableHttpClient httpClient =null;
HttpPost httpPost =null;
try{
httpClient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();
httpPost =newHttpPost(url);
httpPost.setConfig(requestConfig);
List ps =newArrayList();
for(String pKey : params.keySet()) {
ps.add(newBasicNameValuePair(pKey, params.get(pKey)));
}
httpPost.setEntity(newUrlEncodedFormEntity(ps));
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
System.out.println(EntityUtils.toString(httpEntity,"utf-8"));
}catch(ClientProtocolException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally{
try{
if(httpPost!=null){
httpPost.releaseConnection();
}
if(httpClient!=null){
httpClient.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
三. POST請求的RAW參數(shù)傳遞
publicvoidpost(String url, String body){
CloseableHttpClient httpClient =null;
HttpPost httpPost =null;
try{
httpClient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();
httpPost =newHttpPost(url);
httpPost.setConfig(requestConfig);
httpPost.setEntity(newStringEntity(body));
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
System.out.println(EntityUtils.toString(httpEntity,"utf-8"));
}catch(ClientProtocolException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally{
try{
if(httpPost!=null){
httpPost.releaseConnection();
}
if(httpClient!=null){
httpClient.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}