接口測試之java
阿新 • • 發佈:2018-03-13
接口測試通過java來進行接口測試,這時候就需要使用java的url類與輸入輸出流
測試url:https://www.v2ex.com/api/nodes/show.json
參數為:name=python
測試url:https://www.v2ex.com/api/nodes/show.json
參數為:name=python
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class jiekouTest { public static void main(String[] args) { try { //創建url對象 URL url=new URL("https://www.v2ex.com/api/nodes/show.json?name=python"); //打開一個URL連接,並運行客戶端訪問資源。 URLConnection urlConnection =(HttpURLConnection)url.openConnection(); urlConnection.connect();//調用URLConnection對象提供的connect方法連接遠程服務 InputStream is=urlConnection.getInputStream();//獲取輸入流訪問的資源數據 //通過bufferedReader來讀取 BufferedReader buffer=new BufferedReader(new InputStreamReader(is)); StringBuffer bs=new StringBuffer();//存儲讀取的值 String l=null; //判斷讀取的值存在則存入StringBuffer while((l=buffer.readLine())!=null) { bs.append(l).append("\n"); } System.out.println(bs.toString()); } catch (MalformedURLException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } } }
最後獲取到的輸出
中文亂碼問題解決:
//設置下編碼
BufferedReader buffer=new BufferedReader(new InputStreamReader(is,"utf-8"));
接口測試之java