记录一下 调用接口的测试代码,每次都得打开就项目查看 电脑有点不够用:
(核心是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();}}