接口測試http的get和post請求,鍵值對和json傳參的處理,cookie的保留


  1 package Common;
  2 
  3
  4 import com.google.gson.Gson;
  5 import org.apache.http.HttpResponse;
  6 import org.apache.http.NameValuePair;
  7 import org.apache.http.client.CookieStore;
  8 import org.apache.http.client.entity.UrlEncodedFormEntity;
  9 import org.apache.http.client.methods.HttpGet;
 10 import org.apache.http.client.methods.HttpPost;
 11 import org.apache.http.entity.StringEntity;
 12 import org.apache.http.impl.client.BasicCookieStore;
 13 import org.apache.http.impl.client.CloseableHttpClient;
 14 import org.apache.http.impl.client.HttpClients;
 15 import org.apache.http.impl.cookie.BasicClientCookie;
 16 import org.apache.http.message.BasicNameValuePair;
 17 
 18 import java.io.IOException;
 19 import java.util.ArrayList;
 20 import java.util.List;
 21 import java.util.Map;
 22 
 23 
 24 public class DoHttpClient {
 25     public static CookieStore cookieStore = new BasicCookieStore();
 26     public CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
 27 
 28     /**
 29      * get請求發送
 30      * */
 31     public HttpResponse sendGet(String url,String parameter) throws IOException {
 32         if(!parameter.isEmpty()){
 33             url = url + "?" + parameter;
 34         }
 35 
 36         HttpGet httpGet = new HttpGet(url);
 37         HttpResponse response = client.execute(httpGet);
 38 
 39         System.out.println("請求狀態碼:"+response.getStatusLine());
 40 
 41         setCookieStore(response);
 42 
 43         return response;
 44     }
 45 
 46     /**
 47      *post請求發送
 48      * 請求參數json格式無嵌套map數據
 49      * 傳入String param
 50      **/
 51     public HttpResponse sendPost(String url, String param) throws IOException {
 52 
 53         HttpPost httpPost = new HttpPost(url);
 54 
 55         List<NameValuePair> formParms = new ArrayList<NameValuePair>();
 56         FormatProcessing(formParms,param);
 57         httpPost.setEntity(new UrlEncodedFormEntity(formParms,"UTF-8"));
 58 
 59         httpPost.setHeader("Context-Type","application/x-www-form-urlencoded");
 60 
 61         HttpResponse response = client.execute(httpPost);
 62 
 63         System.out.println("請求狀態碼:"+response.getStatusLine());
 64 
 65         setCookieStore(response);
 66 
 67         return response;
 68     }
 69 
 70     /**
 71      *post請求發送
 72      * 請求參數json格式有嵌套map數據
 73      * 傳入JSONObject
 74      **/
 75     public HttpResponse sendPost(String url, Map<String,Object> map) throws IOException {
 76         HttpPost httpPost = new HttpPost(url);
 77 
 78         Gson gson = new Gson();
 79         String parameter = gson.toJson(map);
 80         StringEntity stringEntity = new StringEntity(parameter);
 81         httpPost.setEntity(stringEntity);
 82 
 83         httpPost.setHeader("Context-Type","application/x-www-form-urlencoded");
 84 
 85         HttpResponse response = client.execute(httpPost);
 86 
 87         System.out.println("請求狀態碼:"+response.getStatusLine());
 88 
 89         setCookieStore(response);
 90 
 91         return response;
 92     }
 93 
 94     /**
 95      *傳參處理
 96      * param為key=value?key=value形式,轉化成list
 97      *
 98      */
 99 
100     public void FormatProcessing(List<NameValuePair> formParams, String param){
101         String[] nameValue = param.split("&");
102         for(int i=0;i<nameValue.length;i++){
103             String key = nameValue[i].split("=")[0];
104             String value = nameValue[i].split("=")[1];
105             BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key,value);
106             formParams.add(basicNameValuePair);
107         }
108     }
109 
110     /**
111      * 保留cookie
112      * @param httpResponse
113      */
114     public void setCookieStore(HttpResponse httpResponse){
115         String setCookie = new String();
116         if(httpResponse.getFirstHeader("set-Cookies")!=null){
117             setCookie = httpResponse.getFirstHeader("set-Cookie").getValue();
118         }
119         String JSESSIONID = new String();
120         if(setCookie.isEmpty()==false){
121             JSESSIONID = setCookie.substring(Integer.parseInt("JSESSIONID="),setCookie.indexOf(";"));
122 
123         }
124         //新建一個Cookie,只要使用同一個HttpClient且未關閉連接,則可以使用相同會話來訪問其他要求登錄驗證的服務,保持cookies對話
125         BasicClientCookie basicClientCookie = new BasicClientCookie("JSESSIONID",JSESSIONID);
126         basicClientCookie.setVersion(0);
127         basicClientCookie.setDomain("127.0.0.1");//Cookie中的setDomain()主要用來在兩個不同名稱但是后綴相同的網站地址上.這樣兩個網站就能使用同一個cookie了
128         basicClientCookie.setPath("/CwlProClient");//setPath()主要用來確定什么后綴下能夠使用這個cookie.即地址欄上面的地址的約束
129         cookieStore.addCookie(basicClientCookie);//設置Cookie所遵從的協議版本[默認版本0(遵從原先的Netscape規范)
130 
131     }
132 }

這里的get和post請求,直接返回了HttpResponse類型的response用於后面的數據校驗,通過JsonPath來提取校驗;

其中post請求,傳參可以直接傳String類型的鍵值對,然后用FormatProcessing來處理;傳參為嵌套了map的話,通過gson來轉化;

最后是cookie的處理,獲取后把他傳回去全局變量里面去使用;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM