java测试接口_Java测试普通Java接口记录-TestHrmInterface

这段代码展示了如何使用Java的HttpURLConnection进行HTTP接口的POST请求。主要功能包括设置请求参数,发送请求并读取响应内容。代码中包含了对URL编码、设置请求头、处理输入输出流等关键步骤。
摘要由CSDN通过智能技术生成

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

(核心是request方法)

packagecom.karros.test;importjava.io.BufferedReader;importjava.io.ByteArrayOutputStream;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.net.HttpURLConnection;importjava.net.URL;importjava.net.URLEncoder;importjava.util.ArrayList;importjava.util.Date;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importcom.alibaba.fastjson.JSONObject;importcom.karros.util.DateUtils;importcom.karros.util.MD5Generator;importnet.sf.json.JSONArray;public classTestHrmInterface {public static void main(String[] args) throwsException {

String a=GetStationVolume();

System.out.println(a);//String jsontest = JSONObject.toJSONString(rs);//System.out.println("jsontest:" + jsontest);

}protected static String GetStationVolume() throwsException {

Map m = new HashMap();

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

*

*@parampath

*@paramjson

*@parammaps

*@return*@throwsException*/

public static String request(String path, String json, Map maps) throwsException {

path= path + "?";for (Map.Entryitem : maps.entrySet()) {

path+= (item.getKey() + "=" + item.getValue() + "&");

}

Date startDate= newDate();

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= newDate();double timeout =DateUtils.getSecsBetween(startDate, endDate);returnresult;

}public static String readInStream(InputStream inStream) throwsException {

StringBuffer content= newStringBuffer();

String tempStr= "";

BufferedReader in= new BufferedReader(new InputStreamReader(inStream, "UTF-8"));while ((tempStr = in.readLine()) != null) {

content.append(tempStr);

}

in.close();

inStream.close();returncontent.toString();

}/*** ��ȡ��

*

*@paraminStream

*@return�ֽ�����

*@throwsException*/

public static byte[] readStream(InputStream inStream) throwsException {

ByteArrayOutputStream outSteam= newByteArrayOutputStream();byte[] buffer = new byte[1024];int len = -1;while ((len = inStream.read(buffer)) != -1) {

outSteam.write(buffer,0, len);

}

outSteam.close();

inStream.close();returnoutSteam.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 m = new HashMap(); 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 maps) throws Exception {path = path + "?";

for (Map.Entry 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();}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值