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

 

posted @ 2020-04-01 10:58  淘来的星湖  阅读(494)  评论(0编辑  收藏  举报