get、post接口測試-java


 

public class HttpClient {
    
    
    //get請求方法
    public String sendGet(String url, String data) {
        
        Date date = new Date();
        long time1 = new Date().getTime();
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + data;
            URL realUrl = new URL(urlNameString);
            // 打開和URL之間的連接
            URLConnection connection = realUrl.openConnection();
            // 設置通用的請求屬性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立實際的連接
            connection.connect();
            // 獲取所有響應頭字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍歷所有的響應頭字段
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }
            // 定義 BufferedReader輸入流來讀取URL的響應
            in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));//防止亂碼
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("發送GET請求出現異常!" + e);
            e.printStackTrace();
        }
        // 使用finally塊來關閉輸入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        long time2 = new Date().getTime();
        long time3 = time2-time1;
        return result+"\n耗時:"+String.valueOf(time3)+"ms";
    }
    
    
    //post請求方法
    public String sendPost(String url, String param[]) {
        
        //參數准備,具體需要參數自己魔改--------------------------------  
        String data = "";
        for (int i = 0; i < param.length; i++) {
            if(i==0){
                data=data+param[i];//把數組轉為字符串
            }else{
                data=data+","+param[i];
            }
        }
        String send = "{'data':{"+data+"}}";
        System.out.println("請求參數:"+send);
        //參數准備,具體需要參數自己魔改--------------------------------        

        Date date = new Date();
        long time1 = new Date().getTime();
        String result = null;
        try {
            CloseableHttpClient httpclient = null;
            CloseableHttpResponse httpresponse = null;
            try {
                httpclient = HttpClients.createDefault();//創建默認的httpClient實例 
                HttpPost httppost = new HttpPost(url);//創建httppost
                StringEntity stringentity = new StringEntity(send,ContentType.create("text/json", "UTF-8"));//防止亂碼指定編碼格式
                httppost.setEntity(stringentity);//設置請求參數
                httpresponse = httpclient.execute(httppost);//發送請求
                result = EntityUtils.toString(httpresponse.getEntity());//獲取返回值,邏輯值轉換為字符串返回數據,遍歷返回值
            } finally {
                if (httpclient != null) {
                    httpclient.close();//啟用過則關閉
                }
                if (httpresponse != null) {
                    httpresponse.close();//啟用過則關閉
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long time2 = new Date().getTime();
        long time3 = time2-time1;  
        return result+"-"+"耗時:"+String.valueOf(time3)+"ms";
    }
    
}

 


免責聲明!

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



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