1. 程式人生 > >post/get模擬

post/get模擬


    /**
     * post簡單模擬
     */
    @Test
    public void pushPost() throws Exception {

        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod("http://192.168.127.11:8080/hello");

        String contents = "post測試";
        //設定編碼  msg
        postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");

        //設定引數
        postMethod.setParameter("msg", contents);

        //是否成功 200
        int status = httpClient.executeMethod(postMethod);
        System.out.println(status);

        //獲取響應
        byte[] responseBody = postMethod.getResponseBody();
        //解碼
        String result =  new String(responseBody, "utf-8");
        System.out.println(result);
    }

    /**
     * get請求模擬
     */
    @Test
    public void pushGet() throws Exception {

        HttpClient httpClient = new HttpClient();

        GetMethod getMethod = new GetMethod("http://192.168.127.11:8080/hello?sld=1&zoneId=1");

        int status = httpClient.executeMethod(getMethod);

        System.out.println(status);

        byte[] responseBody = getMethod.getResponseBody();

        String result = new String(responseBody, "utf-8");

        System.out.println(result);
    }