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";
    }
    
}

 

posted on 2017-09-20 19:01  研者  阅读(1654)  评论(0编辑  收藏  举报