Java使用HttpClient发送Get和Post请求

HttpClient是什么,用来干什么,怎么用?这三个问题还是看官方教程吧
英文的官方教程:
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html
中文的官方教程:
https://www.ctolib.com/topics-80581.html
需要的Jar包下载:
http://hc.apache.org/downloads.cgi
博客:
https://blog.csdn.net/lmb55/article/details/70246185
https://blog.csdn.net/u010926964/article/details/52063118
https://blog.csdn.net/liuxia8811/article/details/56478884

HttpClient特点:

1)实现了所有 HTTP 的方法(GET,POST,PUT,HEAD 等)
(2)支持自动转向
(3)支持 HTTPS 协议
(4)支持代理服务器等

为什么要使用它?

客户端HTTP传输库
尽管java.net包提供了通过HTTP访问资源的基本功能,但它并未提供许多应用程序所需的完全灵活性或功能。
HttpClient旨在通过提供一个高效,最新且功能丰富的包来实现这一空白,该包实现了最新HTTP标准和建议的客户端。
HttpClient的目的是传输和接收HTTP消息。HttpClient不会尝试处理内容,执行嵌入在HTML页面中的javascript,
尝试猜测内容类型,如果没有明确设置,或重新格式化请求/重写位置URI,或其他与HTTP传输无关的功能。

我这里只学了最基础的GET和POST,目前够用就行。
样例1:GET

public static void main(String[] args) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("https://www.baidu.com/");
        System.out.println("Get:"+httpGet.getURI());
        CloseableHttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpGet);
            HttpEntity entity =httpResponse.getEntity();
            if(entity!=null) {
                System.out.println("响应状态:"+httpResponse.getStatusLine());
                String content = EntityUtils.toString(entity,"utf-8");
                System.out.println("响应内容:"+content);
                System.out.println("内容长度:"+content.length());
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(httpResponse!=null) {
                httpResponse.close();
            }
            httpClient.close();
        }

    }

样例2:GET
与上面不同的只是,url字符串换了个类拼接。

URI url = new URIBuilder()
                .setScheme("https")
                .setHost("www.baidu.com")
                .setParameter("z", "mi")
                .setParameter("w", "我")
                .build();
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);

样例3:POST

public static void main(String[] args) throws IOException, URISyntaxException {
        URI url = new URIBuilder()
                .setScheme("https")
                .setHost("www.baidu.com")
                .setParameter("z", "mi")
                .setParameter("w", "我")
                .build();
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        System.out.println("Get:"+httpGet.getURI());
        CloseableHttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpGet);
            HttpEntity entity =httpResponse.getEntity();
            if(entity!=null) {
                System.out.println("响应状态:"+httpResponse.getStatusLine());
                System.out.println(httpResponse.getProtocolVersion());
                System.out.println(httpResponse.getStatusLine().getStatusCode());
                System.out.println(httpResponse.getStatusLine().getReasonPhrase());
                String content = EntityUtils.toString(entity,"utf-8");
                System.out.println("响应内容:"+content);
                System.out.println("内容长度:"+content.length());
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(httpResponse!=null) {
                httpResponse.close();
            }
            httpClient.close();
        }

    }

猜你喜欢

转载自blog.csdn.net/qq_38409944/article/details/81387600