如何測試http介面 日記一
阿新 • • 發佈:2019-01-27
今天組長讓測試一個http介面,當時有點懵,不知道怎麼測試,,想了一下,有了點思路,但是剛開始想到的是通過request請求來寫個方法呼叫,那先不管對錯,咱們今天先分享一下,通過request/response請求來呼叫http的方式:
(在網上收了一段程式碼:)
程式碼如下:
- package com.zhang.miaodou;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.InputStreamReader;
-
import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLEncoder;
- publicclass DemoTest1 {
- publicstaticfinal String GET_URL = "http://112.4.27.9/mall-back/if_user/store_list?storeId=32";
- // public static final String POST_URL = "http://112.4.27.9/mall-back/if_user/store_list";
- // 妙兜測試介面
-
publicstaticfinal String POST_URL =
- /**
- * 介面呼叫 GET
- */
- publicstaticvoid httpURLConectionGET() {
- try {
- URL url = new URL(GET_URL); // 把字串轉換為URL請求地址
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 開啟連線
-
connection.connect();// 連線會話
- // 獲取輸入流
- BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
- String line;
- StringBuilder sb = new StringBuilder();
- while ((line = br.readLine()) != null) {// 迴圈讀取流
- sb.append(line);
- }
- br.close();// 關閉流
- connection.disconnect();// 斷開連線
- System.out.println(sb.toString());
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println("失敗!");
- }
- }
- /**
- * 介面呼叫 POST
- */
- publicstaticvoid httpURLConnectionPOST () {
- try {
- URL url = new URL(POST_URL);
- // 將url 以 open方法返回的urlConnection 連線強轉為HttpURLConnection連線 (標識一個url所引用的遠端物件連線)
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 此時cnnection只是為一個連線物件,待連線中
- // 設定連線輸出流為true,預設false (post 請求是以流的方式隱式的傳遞引數)
- connection.setDoOutput(true);
- // 設定連線輸入流為true
- connection.setDoInput(true);
- // 設定請求方式為post
- connection.setRequestMethod("POST");
- // post請求快取設為false
- connection.setUseCaches(false);
- // 設定該HttpURLConnection例項是否自動執行重定向
- connection.setInstanceFollowRedirects(true);
- // 設定請求頭裡面的各個屬性 (以下為設定內容的型別,設定為經過urlEncoded編碼過的from引數)
- // application/x-javascript text/xml->xml資料 application/x-javascript->json物件 application/x-www-form-urlencoded->表單資料
- // ;charset=utf-8 必須要,不然妙兜那邊會出現亂碼【★★★★★】
- connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
- // 建立連線 (請求未開始,直到connection.getInputStream()方法呼叫時才發起,以上各個引數設定需在此方法之前進行)
- connection.connect();
- // 建立輸入輸出流,用於往連線裡面輸出攜帶的引數,(輸出內容為?後面的內容)
- DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
- String app_key = "app_key="+ URLEncoder.encode("4f7bf8c8260124e6e9c6bf094951a111", "utf-8"); // 已修改【改為錯誤資料,以免資訊洩露】
- String agt_num = "&agt_num="+ URLEncoder.encode("10111", "utf-8"); // 已修改【改為錯誤資料,以免資訊洩露】
- String pid = "&pid="+ URLEncoder.encode("BLZXA150401111", "utf-8"); // 已修改【改為錯誤資料,以免資訊洩露】
- String departid = "&departid="+ URLEncoder.encode("10007111", "utf-8"); // 已修改【改為錯誤資料,以免資訊洩露】
- String install_lock_name = "&install_lock_name="+ URLEncoder.encode("南天大門", "utf-8");
- String install_address = "&install_address="+ URLEncoder.encode("北京育新", "utf-8");
- String install_gps = "&install_gps="+ URLEncoder.encode("116.350888,40.011001", "utf-8");
- String install_work = "&install_work="+ URLEncoder.encode("小李", "utf-8");
- String install_telete = "&install_telete="+ URLEncoder.encode("13000000000", "utf-8");
- String intall_comm = "&intall_comm="+ URLEncoder.encode("一切正常", "utf-8");
- // 格式 parm = aaa=111&bbb=222&ccc=333&ddd=444
- String parm = app_key+ agt_num+ pid+ departid+ install_lock_name+ install_address+ install_gps+ install_work+ install_telete+ intall_comm;
- // 將引數輸出到連線
- dataout.writeBytes(parm);
- // 輸出完成後重新整理並關閉流
- dataout.flush();
- dataout.close(); // 重要且易忽略步驟 (關閉流,切記!)
- // System.out.println(connection.getResponseCode());
- // 連線發起請求,處理伺服器響應 (從連接獲取到輸入流幷包裝為bufferedReader)
- BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
- String line;
- StringBuilder sb = new StringBuilder(); // 用來儲存響應資料
- // 迴圈讀取流,若不到結尾處
- while ((line = bf.readLine()) != null) {
- // sb.append(bf.readLine());
- sb.append(line).append(System.getProperty("line.separator"));
- }
- bf.close(); // 重要且易忽略步驟 (關閉流,切記!)
- connection.disconnect(); // 銷燬連線
- System.out.println(sb.toString());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- publicstaticvoid main(String[] args) {
- // httpURLConectionGET();
- httpURLConnectionPOST();
- }
- }