HttpClient CloseableHttpClient GetMethod PostMethod http

本文介绍了如何在Java中使用HttpClient和CloseableHttpClient进行GET和POST请求。详细展示了pom依赖,以及两种HTTP方法在IDEA中的运行结果和通过Postman进行的调试结果。

pom依赖

        <!--HttpClient的依赖-->
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>

        <!--CloseableHttpClient的依赖-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>

HttpClient 的get请求方式

private static void doGet() {
    HttpClient client = new HttpClient();
    GetMethod getMethod = new GetMethod("https://www.thepaper.cn/");
    int code = 0;
    try {
        code = client.executeMethod(getMethod); // 返回StatusCode,即状态码
        if (code == 200) {
            String res = getMethod.getResponseBodyAsString();  // 这个代码,没太理解。
            System.out.println(res);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  • idea运行结果:

在这里插入图片描述

  • postman调试结果:

在这里插入图片描述

HttpClient 的post请求方式

private static void doPost() {
    String praiseUrl = "https://www.thepaper.cn/www/commentPraise.msp"; // 澎湃新闻评论点赞url
    HttpClient client = new HttpClient(); // httpClient
    PostMethod postMethod = new PostMethod(praiseUrl);  // postMethod
    // 必须设置下面这个Header  设置请求头
    postMethod.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
            "(KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");

    postMethod.addParameter("commentId", "18718372"); // 评论的id,抓包获得
    try {
        int code = client.executeMethod(postMethod); // 获取返回码。
        if (code == 200) {
            String res = postMethod.getResponseBodyAsString(); // 获取响应的 响应体。
            System.out.println(res);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  • idea运行结果:

在这里插入图片描述

  • post请求结果:

在这里插入图片描述
在这里插入图片描述

CloseableHttpClient的get请求方式

public static void doGet() {
    CloseableHttpClient client = HttpClientBuilder.create().build(); // 获取http客户端。
    HttpGet get = new HttpGet("http://www.thepaper.cn"); // 建立httpGet
    try {
        // 很奇怪,使用CloseableHttpClient来请求澎湃新闻的首页,GTE请求也必须加上下面这个Header,但是使用HTTPClient则不需要
        get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like " +
                "Gecko) Chrome/58.0.3029.81 Safari/537.36"); // 加上请求头
        HttpResponse response = client.execute(get); // 获取响应。response

        String res = EntityUtils.toString(response.getEntity()); // 获取响应体。
        System.out.println(res);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

CloseableHttpClient的post请求方式

public static void doPost() {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost("https://www.thepaper.cn/www/commentPraise.msp");
    try {
        post.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like " +
                "Gecko) Chrome/58.0.3029.81 Safari/537.36");
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("commentId", "18718372"));
        post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        HttpResponse response = client.execute(post);
        String res = EntityUtils.toString(response.getEntity());
        System.out.println(res);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值