Loading

HttpClient入门

HttpClient

HttpClient 是属于Apache下的一个小框架

来看一下官方的话语

前言
超文本传输​​协议 (HTTP) 可能是当今 Internet 上使用的最重要的协议。Web 服务、支持网络的设备和网络计算的增长继续将 HTTP 协议的作用扩展到用户驱动的 Web 浏览器之外,同时增加了需要 HTTP 支持的应用程序的数量。

尽管 java.net 包提供了通过 HTTP 访问资源的基本功能,但它并没有提供许多应用程序所需的全部灵活性或功能。HttpClient 试图通过提供一个高效、最新且功能丰富的包来实现最新的 HTTP 标准和建议的客户端,从而填补这一空白。

HttpClient 专为扩展而设计,同时为基本 HTTP 协议提供强大的支持,任何构建 HTTP 感知客户端应用程序(例如 Web 浏览器、Web 服务客户端或利用或扩展 HTTP 协议进行分布式通信的系统)的人可能都会感兴趣

官方文档

使用GET请求

GET无参请求

uri:Request-URI 是一个统一资源标识符,用于标识应用请求的资源。HTTP 请求 URI 由协议方案、主机名、可选端口、资源路径、可选查询和可选片段组成。

使用这个 地址 https://www.baidu.com

    public static void main(String[] args) throws URISyntaxException {
        //使用帮助类HttpClients创建CloseableHttpClient对象.
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //第一种 直接把要请求的地址放进去
//        HttpGet httpGet = new HttpGet("https://www.baidu.com");
        //第二钟 链式编程使用URIBuild类简化请求URI的创建和修改
        URI url = new URIBuilder()
                .setScheme("https")
                .setHost("www.baidu.com")
                .build();
        HttpGet httpGet = new HttpGet(url);
        System.out.println(httpGet.getURI());
        CloseableHttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpGet);
            System.out.println("版本协议====>"+httpResponse.getProtocolVersion());
            System.out.println("状态码====>"+httpResponse.getStatusLine().getStatusCode());
            System.out.println("版本协议和状态码====>"+httpResponse.getStatusLine().toString());
            System.out.println("打印读取的信息===>"+EntityUtils.toString(httpResponse.getEntity()));
            Header[] allHeaders = httpResponse.getAllHeaders();
            System.out.println("响应的长度=====>"+allHeaders.length);
            System.out.println("响应的结果=====>"+allHeaders);
        } finally {
            try {
                if (httpResponse!=null){
                    httpResponse.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

版本协议>HTTP/1.1
状态码
>200
版本协议和状态码>HTTP/1.1 200 OK
响应的长度
=>3
响应的结果=====>[Lorg.apache.http.Header;@6193932a

GET有参请求

这里使用 https://iwenjuan.baidu.com/?code=n8zfxz 这个网址测试 get请求也就拼接上参数

使用这个 地址 https://iwenjuan.baidu.com/

import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.security.crypto.codec.Utf8;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;


public class httpClientGet {
    public static void main(String[] args) throws URISyntaxException {
        //使用帮助类HttpClients创建CloseableHttpClient对象.
        CloseableHttpClient httpClient = HttpClients.createDefault();
        StringBuffer buffer = new StringBuffer();
        buffer.append("?code="+"n8zfxz");
        //第一种 直接把要请求的地址放进去
        HttpGet httpGet = new HttpGet("https://iwenjuan.baidu.com/"+buffer.toString());
        CloseableHttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpGet);
            System.out.println("版本协议====>"+httpResponse.getProtocolVersion());
            System.out.println("状态码====>"+httpResponse.getStatusLine().getStatusCode());
            System.out.println("版本协议和状态码====>"+httpResponse.getStatusLine().toString());
            HttpEntity entity = httpResponse.getEntity();
            if (entity != null) {
                entity = new BufferedHttpEntity(entity);
            }

            System.out.println("读取返回的消息====>"+ EntityUtils.toString(entity));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (httpResponse!=null){
                    httpResponse.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

EntityUtils强烈建议不要使用 ,除非响应实体源自受信任的 HTTP 服务器并且已知长度有限

POST请求

POST无参请求

使用 这个地址作为测试 http://httpbin.org/post

public class HttpClientPost {
    public static void main(String[] args) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://httpbin.org/post");
        CloseableHttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpPost);
            System.out.println("返回状态码====>"+httpResponse.getStatusLine());
            System.out.println(httpResponse.getEntity());
            HttpEntity entity = httpResponse.getEntity();
            if (entity != null) {
                entity = new BufferedHttpEntity(entity);
            }
            System.out.println(EntityUtils.toString(entity));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

待更新

posted @ 2021-12-15 12:00  Rzk  阅读(112)  评论(0编辑  收藏  举报