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);
    }

猜你喜欢

转载自blog.csdn.net/bb23417274/article/details/83148632