zoukankan      html  css  js  c++  java
  • Java测试普通Java接口记录-TestHrmInterface

    记录一下 调用接口的测试代码,每次都得打开就项目查看 电脑有点不够用:

    (核心是request方法

    package com.karros.test;
    
    import java.io.BufferedReader;
    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLEncoder;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import com.alibaba.fastjson.JSONObject;
    import com.karros.util.DateUtils;
    import com.karros.util.MD5Generator;
    
    import net.sf.json.JSONArray;
    
    public class TestHrmInterface {
    
        public static void main(String[] args) throws Exception {
            String a =  GetStationVolume();
            System.out.println(a);
    //        String jsontest = JSONObject.toJSONString(rs);
    //        System.out.println("jsontest:" + jsontest);
        } 
        protected static String GetStationVolume() throws Exception {
            Map<String, Object> m = new HashMap<String, Object>(); 
            m.put("syncYear", URLEncoder.encode("2019", "utf-8"));
            String url = "http://localhost:8080/BIP_Api/yptbase/newHrm/GetStationVolume";
    //        String url = "http://10.182.x.1xx:8183/BIPApi/yptbase/newHrm/GetStationVolume"; // 测试环境
            return request(url, null, m);
        }
    
        
         
        /**
         * httpclient
         * 
         * @param path
         * @param json
         * @param maps
         * @return
         * @throws Exception
         */
        public static String request(String path, String json, Map<String, Object> maps) throws Exception {
            path = path + "?";
    
            for (Map.Entry<String, Object> item : maps.entrySet()) {
                path += (item.getKey() + "=" + item.getValue() + "&");
            }
            Date startDate = new Date();
            URL url = new URL(path.substring(0, path.length() - 1));
            System.out.println("url:" + url.toString());
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "application/json; charset=" + "UTF-8");
            if (json != null) {
                byte[] data = json.getBytes("UTF-8");
                conn.setRequestProperty("Content-Length", String.valueOf(data.length));
            }
            conn.setConnectTimeout(60 * 1000);
            OutputStream outStream = conn.getOutputStream();
    
            if (json != null) {
                byte[] data = json.getBytes("UTF-8");
                outStream.write(data);
            }
            outStream.flush();
            outStream.close();
            String result = "";
            System.out.println("status:" + conn.getResponseCode());
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
    
                InputStream inStream = conn.getInputStream();
    //              result = new String(readStream(inStream));
                result = readInStream(inStream);
            }
            Date endDate = new Date();
            double timeout = DateUtils.getSecsBetween(startDate, endDate);
    
            return result;
        }
    
        public static String readInStream(InputStream inStream) throws Exception {
            StringBuffer content = new StringBuffer();
            String tempStr = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
            while ((tempStr = in.readLine()) != null) {
                content.append(tempStr);
            }
            in.close();
            inStream.close();
            return content.toString();
        }
    
        /**
         * ��ȡ��
         *
         * @param inStream
         * @return �ֽ�����
         * @throws Exception
         */
        public static byte[] readStream(InputStream inStream) throws Exception {
            ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = inStream.read(buffer)) != -1) {
                outSteam.write(buffer, 0, len);
            }
            outSteam.close();
            inStream.close();
            return outSteam.toByteArray();
        }
    }

    package com.karros.test;
    import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;
    import com.alibaba.fastjson.JSONObject;import com.karros.util.DateUtils;import com.karros.util.MD5Generator;
    import net.sf.json.JSONArray;
    public class TestHrmInterface {
    public static void main(String[] args) throws Exception {String a =  GetStationVolume();System.out.println(a);//String jsontest = JSONObject.toJSONString(rs);//System.out.println("jsontest:" + jsontest);} protected static String GetStationVolume() throws Exception {Map<String, Object> m = new HashMap<String, Object>(); m.put("syncYear", URLEncoder.encode("2019", "utf-8"));String url = "http://localhost:8080/BIP_Api/yptbase/newHrm/GetStationVolume";//String url = "http://10.182.5.176:8183/BIPApi/yptbase/newHrm/GetStationVolume"; // 测试环境return request(url, null, m);}
     /** * httpclient *  * @param path * @param json * @param maps * @return * @throws Exception */public static String request(String path, String json, Map<String, Object> maps) throws Exception {path = path + "?";
    for (Map.Entry<String, Object> item : maps.entrySet()) {path += (item.getKey() + "=" + item.getValue() + "&");}Date startDate = new Date();URL url = new URL(path.substring(0, path.length() - 1));System.out.println("url:" + url.toString());HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("POST");conn.setDoOutput(true);conn.setRequestProperty("Content-Type", "application/json; charset=" + "UTF-8");if (json != null) {byte[] data = json.getBytes("UTF-8");conn.setRequestProperty("Content-Length", String.valueOf(data.length));}conn.setConnectTimeout(60 * 1000);OutputStream outStream = conn.getOutputStream();
    if (json != null) {byte[] data = json.getBytes("UTF-8");outStream.write(data);}outStream.flush();outStream.close();String result = "";System.out.println("status:" + conn.getResponseCode());if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
    InputStream inStream = conn.getInputStream();//      result = new String(readStream(inStream));result = readInStream(inStream);}Date endDate = new Date();double timeout = DateUtils.getSecsBetween(startDate, endDate);
    return result;}
    public static String readInStream(InputStream inStream) throws Exception {StringBuffer content = new StringBuffer();String tempStr = "";BufferedReader in = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));while ((tempStr = in.readLine()) != null) {content.append(tempStr);}in.close();inStream.close();return content.toString();}
    /** * ��ȡ�� * * @param inStream * @return �ֽ����� * @throws Exception */public static byte[] readStream(InputStream inStream) throws Exception {ByteArrayOutputStream outSteam = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = -1;while ((len = inStream.read(buffer)) != -1) {outSteam.write(buffer, 0, len);}outSteam.close();inStream.close();return outSteam.toByteArray();}}

  • 相关阅读:
    Sql Server 2008卸载后再次安装一直报错
    listbox 报错 Cannot have multiple items selected when the SelectionMode is Single.
    Sql Server 2008修改Sa密码
    学习正则表达式
    Sql Server 查询第30条数据到第40条记录数
    Sql Server 复制表
    Sql 常见面试题
    Sql Server 简单查询 异步服务器更新语句
    jQuery stop()用法以及案例展示
    CSS3打造不断旋转的CD封面
  • 原文地址:https://www.cnblogs.com/tldxh/p/12611089.html
Copyright © 2011-2022 走看看