1. 程式人生 > >分享一個特別好的測試http協議的測試工具類

分享一個特別好的測試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+方法+引數