zoukankan      html  css  js  c++  java
  • 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+"
    耗时:"+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";
        }
        
    }
  • 相关阅读:
    简单区间dp
    【题解】石子合并
    【2019.7.6】刷题记录
    【题解】大朋友的数字
    【基础】dp系列1
    【题解】垃圾陷阱
    【题解】导弹拦截
    hadoop各组件安装(非专业人士,不定期更新)
    python逼格提升
    python第三十二天-----算法
  • 原文地址:https://www.cnblogs.com/yanzhe/p/7563123.html
Copyright © 2011-2022 走看看