java+httpclient实现Get、Post接口自动化案例

java+httpclient实现Get、Post接口自动化案例

HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient。现在HttpClient最新版本为 HttpClient 4.5 .6。

下面是java+httpclient实现Get、Post接口自动化案例详细过程

首先建立一个java工程,例如springboot工程项目,其次引入httpclient的jar包,新建类编写请求案例

  • Get请求案例
	@GetMapping("/getapi")
    public String get()throws Exception{
        return getdata("https://api.apiopen.top/recommendPoetry");
    }

    public static String getdata(String http_url) throws Exception {
        //创建一个默认的连接
        HttpClient  client = HttpClients.createDefault();
        String result=null;
        try {
            HttpGet httpGet = new HttpGet(http_url);//创建一个请求
            HttpResponse  response = client.execute(httpGet);
            StatusLine respHttpStatus = response.getStatusLine();
            int staus = respHttpStatus.getStatusCode();//获取响应状态码
            HttpEntity responseBody = response.getEntity();
            result=EntityUtils.toString(responseBody,"UTF-8");//返回api内容
            //打印数据到控制台
            System.out.println("当前get请求,请求地址参数为:"+http_url +"\n请求返回状态为:"+staus+"\n请求返回数据为:"+result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

返回结果:{“code”:200,“message”:“成功!”,“result”:“因有人恶意刷接口,导致接口调用频繁,接口已经不能稳定运行,所以计划近期下线,积德吧朋友,如果长期如此,所有接口将面临关闭。”}

  • Post请求案例
	@GetMapping("/postapi")
    public String post()throws Exception{
        return postdata("http://xxx.xxxxxx.xxx/before/logindata","{\"email\":\"[email protected]\",\"psw\":\"RnpqNEVRTFNxMDExcGV0QVJrOTEwZDh4bkZreUZaNXcyMw==\"}");
    }
    
    public static String postdata(String url, String body) throws IOException {
        String result = "";
        //创建HttpClient对象
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        if (body != null) {
            //解决中文乱码问题
            StringEntity entity = new StringEntity(body, "utf-8");
            entity.setContentEncoding("UTF-8");
            httpPost.setHeader("Content-Type", "application/json");
            httpPost.setEntity(entity);
        }
        //发送post请求
        HttpResponse response = httpClient.execute(httpPost);
        StatusLine respHttpStatus = response.getStatusLine();
        int staus = respHttpStatus.getStatusCode();//获取响应状态码
        HttpEntity responseBody = response.getEntity();
        result=EntityUtils.toString(responseBody,"UTF-8");//返回api内容
        //打印数据到控制台
        System.out.println("当前get请求,请求地址参数为:"+url +"\n请求返回状态为:"+staus+"\n请求返回数据为:"+result);
        return result;
    }

返回结果:{“result”:null,“data”:500}

你可能感兴趣的:(java,springboot,HttpClient)