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();}}

  • 相关阅读:
    ORACLE数据库表解锁record is locked by another user
    Oracle11gR2设置连接数process与会话session值
    Oracle 11g用exp无法导出空表的处理方法
    Oracle随机选择一条记录SQL
    Oracle取查询结果数据的第一条记录SQL
    Hibernate 一对多查询对set的排序
    Windows平台下Oracle实例启动过程中日志输出
    Windows平台下Oracle监听服务启动过程中日志输出
    Windows平台下Oracle 11g R2监听文件日志过大,造成客户端无法连接的问题处理
    WebSphere设置会话超时时间
  • 原文地址:https://www.cnblogs.com/tldxh/p/12611089.html
Copyright © 2011-2022 走看看