分享一个特别好的测试http协议的测试工具类
二话不说,咱直接上代码!!!!
public class TestPost {
private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) throws Exception {
testRegisterRooom();
// TestPost.test_fakaBySingeRoom();
// TestPost.test_fakaBySingleCard();
// roomAssign();
} // end
public static void testRegisterRooom() {
String postURL = "http://localhost:8090/bzser/api/device/v1/notify";
// [{"data":{"status":1},"requestid":"1542581307434","appid":"15410725170011",
// "roomnu":"4096","cmd":"register_status","deviceid":"AC96","token":"9866805940052973"}]
String terminalExtraInfos = "{'requestid':'595135224347',"
+ "'appid':'15435700737191',"
+ "'data':{'status':1},"
+ "'roomnu':'1-1003',"
+ "'cmd':'register_status',"
+ "'deviceid':'DDF4',"
+ "'token':'23115375079095'"
+ "}";
System.out.println(terminalExtraInfos);
JSONObject json = JSON.parseObject(terminalExtraInfos);
String jsonObject = json.toJSONString();
System.out.println(terminalExtraInfos);
HttpResponse response = postURLJson(postURL, jsonObject);
try {
System.out.println("testLogin Response content: "
+ EntityUtils.toString(response.getEntity(), "UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
}// ...
/**
* 分配房间 一个房间分配一张 或者 多张卡
* @param hotelId
* @param roomNo
* @param cardnos 逗号隔开
* @param timeType int 1:有效时间[时效卡] 只能取1
* @param endTime yyyy-MM-dd hh:ss:mm
* @return
*/
public static void test_fakaBySingeRoom() {
String postURL = "http://localhost:8090/bzser/api/officeManage/fakaBySingeRoom";
// String terminalExtraInfos = "{'hotelId':71,'roomNo':'1-51006','cardnos':'张聪,王海',"
// + "'timeType':1,'endTime':'2018-18-25 23:59:59'}";
List<BasicNameValuePair> formParams = new ArrayList<BasicNameValuePair>();
formParams.add(new BasicNameValuePair("hotelId","71"));
formParams.add(new BasicNameValuePair("roomNo","1-51006"));
formParams.add(new BasicNameValuePair("cardnos","张聪,王海"));
formParams.add(new BasicNameValuePair("timeType","1"));
formParams.add(new BasicNameValuePair("endTime","2018-11-25 23:59:59"));
// System.out.println(terminalExtraInfos);
//
// JSONObject json = JSON.parseObject(terminalExtraInfos);
// String jsonObject = json.toJSONString();
//
// System.out.println(terminalExtraInfos);
// HttpResponse response = postURLJson(postURL, jsonObject);
HttpResponse response = postURL(postURL, formParams);
try {
System.out.println("testLogin Response content: "
+ EntityUtils.toString(response.getEntity(), "UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
}// ...
/**
* 分配房间 一个房间分配一张 或者 多张卡
* @param hotelId
* @param roomNos 逗号隔开
* @param cardno
* @param timeType int 1:有效时间[时效卡] 只能取1
* @param endTime yyyy-MM-dd hh:ss:mm
* @return
*/
public static void test_fakaBySingleCard() {
String postURL = "http://localhost:8090/bzser/api/officeManage/fakaBySingleCard";
// String terminalExtraInfos = "{'hotelId':71,'roomNo':'1-51006','cardnos':'张聪,王海',"
// + "'timeType':1,'endTime':'2018-18-25 23:59:59'}";
List<BasicNameValuePair> formParams = new ArrayList<BasicNameValuePair>();
formParams.add(new BasicNameValuePair("hotelId","71"));
formParams.add(new BasicNameValuePair("roomNos","1-51001,1-51002,1-51003,1-51004"));
formParams.add(new BasicNameValuePair("cardno","张聪"));
formParams.add(new BasicNameValuePair("timeType","1"));
formParams.add(new BasicNameValuePair("endTime","2018-11-25 23:59:59"));
// System.out.println(terminalExtraInfos);
//
// JSONObject json = JSON.parseObject(terminalExtraInfos);
// String jsonObject = json.toJSONString();
//
// System.out.println(terminalExtraInfos);
// HttpResponse response = postURLJson(postURL, jsonObject);
HttpResponse response = postURL(postURL, formParams);
try {
System.out.println("testLogin Response content: "
+ EntityUtils.toString(response.getEntity(), "UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
}// ...
public static void roomAssign() {
String postURL = "http://localhost:8090/bzser/api/gyboss/roomAssign";
String terminalExtraInfos = "{'checkin_time':'2018-08-24','roomno':'2006','mobile_phone':'13565412365','openid':'og3uzv0NmJqD7GvEIy169K1X4o0s','checkout_time':'2018-08-25','sex':'男','customer_name':'1','idNumber':'123456987456321','cardno':'CS5'}";
// String terminalExtraInfos = "{'wechat_id':'og3uzv_UdVx-lj_8ozT7_dbLg0ZE','method':0,"
// + "'end_time':'2018-08-18 02:54:00','keytype':0,'hotelId':'',"
// + "'idNumber':'12345678965412','deviceid':'','customerName':'测试555','roomname':'',"
// + "'start_time':'2018-08-16 02:54:42','room_num':'','mobile_phone':'17612341234','requestid':0,'appid':'',"
// + "'appsecret':'','cardid':'','expireDate':'','openpassword':'','id':0,'roomdevno':''}";
System.out.println(terminalExtraInfos);
JSONObject json = JSON.parseObject(terminalExtraInfos);
String jsonObject = json.toJSONString();
System.out.println(terminalExtraInfos);
HttpResponse response = postURLJson(postURL, jsonObject);
try {
System.out.println("testLogin Response content: "
+ EntityUtils.toString(response.getEntity(), "UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
}// ...
public static void setShareLock() {
String postURL = "http://nhbtek.com/bzser/api/gyrentclient/setShareLock";
// String postURL = "http://localhost:8090/bzser/api/gyrentclient/setShareLock";
String terminalExtraInfos = "{'wechat_id':'og3uzv_UdVx-lj_8ozT7_dbLg0ZE','method':0,"
+ "'end_time':'2018-08-18 02:54:00','keytype':0,'hotelId':'',"
+ "'idNumber':'12345678965412','deviceid':'','customerName':'测试555','roomname':'',"
+ "'start_time':'2018-08-16 02:54:42','room_num':'','mobile_phone':'17612341234','requestid':0,'appid':'',"
+ "'appsecret':'','cardid':'','expireDate':'','openpassword':'','id':0,'roomdevno':''}";
System.out.println(terminalExtraInfos);
JSONObject json = JSON.parseObject(terminalExtraInfos);
String jsonObject = json.toJSONString();
System.out.println(terminalExtraInfos);
HttpResponse response = postURLJson(postURL, jsonObject);
try {
System.out.println("testLogin Response content: "
+ EntityUtils.toString(response.getEntity(), "UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
}// ...
public static void feelist() {
String postURL = "http://localhost:8090/bzser/api/gyrentclient/feelist";
String terminalExtraInfos = " {\n" + " \"wechat_id\": \"og3uzv-gvwC4yarsJmjK02kuA9rc\" \n"
+ " }";
System.out.println(terminalExtraInfos);
JSONObject json = JSON.parseObject(terminalExtraInfos);
String jsonObject = json.toJSONString();
System.out.println(terminalExtraInfos);
HttpResponse response = postURLJson(postURL, jsonObject);
try {
System.out.println("testLogin Response content: "
+ EntityUtils.toString(response.getEntity(), "UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
}// ...
public static void testSendSms() {
String postURL = "http://localhost:8090/bzser/api/hotel/sendSms";
String terminalExtraInfos = " {\n" + " \"hotelId\": 1,\n"
+ " \"templatekey\": \"VERICODE_CHECKIN\",\n"
+ " \"paramString\": {\"code\":\"9094\"},\n"
+ " \"mobile_phone\": \"17727947035\"\n"
+" }";
System.out.println(terminalExtraInfos);
JSONObject json = JSON.parseObject(terminalExtraInfos);
String jsonObject = json.toJSONString();
System.out.println(terminalExtraInfos);
HttpResponse response = postURLJson(postURL, jsonObject);
try {
System.out.println("testLogin Response content: "
+ EntityUtils.toString(response.getEntity(), "UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
}// ...
public static void testNotify() {
String postURL = "http://localhost:8090/bzser/api/device/v1/notify";
String terminalExtraInfos = " {\n" + " \"deviceid\": \"1sdfdsf\",\n"
+ " \"roomnu\": \"1001\",\n"
+ " \"token\": \"336me2\",\n"
+ " \"barry\": 0.7,\n"
+ " \"status\": 1,\n"
+ " \"appid\": \"8dddeeffbf83d067fa4f385e33f15a3f\"\n" +
" }";
System.out.println(terminalExtraInfos);
JSONObject json = JSON.parseObject(terminalExtraInfos);
String jsonObject = json.toJSONString();
System.out.println(terminalExtraInfos);
HttpResponse response = postURLJson(postURL, jsonObject);
try {
System.out.println("testLogin Response content: "
+ EntityUtils.toString(response.getEntity(), "UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
}// ...
//查询房间
// being calling......,|calling| com.cec.controller.api.bz.GyBossController,|methodName| queryRooms|parameters|
//[{"checkin_time":"","roomno":"","openid":"og3uzv0NmJqD7GvEIy169K1X4o0s","buildid":39,"bindpwd":"",""
// + "isempty":1,"layerid":6,"mobile_phone":"","checkout_time":"","checkstatus":0,"typeid":0,"id":0}]
public static void queryRooms() {
String postURL = "http://localhost:8090/bzser/api/gyboss/queryRooms";
String terminalExtraInfos = " {\n" + " \"isempty\": \"1\",\n"
+ " \"openid\": \"og3uzv0NmJqD7GvEIy169K1X4o0s\",\n"
+ " \"buildid\": \"39\",\n"
+ " \"layerid\": \"51\"\n"
+ " }";
System.out.println(terminalExtraInfos);
JSONObject json = JSON.parseObject(terminalExtraInfos);
String jsonObject = json.toJSONString();
System.out.println(terminalExtraInfos);
HttpResponse response = postURLJson(postURL, jsonObject);
try {
System.out.println("testLogin Response content: "
+ EntityUtils.toString(response.getEntity(), "UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
}// ...
public static HttpResponse postURL(String postURL, List<BasicNameValuePair> formParams) {
HttpClient client = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(postURL);
try {
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formParams, "UTF-8");
httpPost.setEntity(urlEncodedFormEntity);
return client.execute(httpPost);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}// ...
public static HttpResponse postURLJson(String postURL, String jsonObject) {
HttpClient client = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(postURL);
StringEntity entity = new StringEntity(jsonObject, "UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
try {
return client.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}// ..
public static HttpResponse getURLJson(String postURL, String jsonObject) {
HttpClient client = HttpClientBuilder.create().build();
HttpGet httpPost = new HttpGet(postURL);
try {
return client.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}// ..
}//end
用这个可以测试http协议的url+方法+参数
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统