Java后端发送http请求

1、在pom.xml中导入jar的坐标
<dependency>
    <groupId>org.apache.httpcomponentsgroupId>
    <artifactId>httpclientartifactId>
    <version>4.5.5version>
dependency>
2、创建请求客户端
CloseableHttpClient aDefault = HttpClients.createDefault(); // 方式一
CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 方式二
3.1、创建GET请求
// get请求,方式一
HttpGet httpget = new HttpGet("http://www.network.com/?参数=参数值");

// get请求,方式二
URI uri = null;
try {
   
    uri = new URIBuilder()
        .setScheme("http")		// 请求的协议
        .setHost("www.network.com")  // 请求的主机地址
        .setPath("/")	// 请求的路径
        .setParameter("参数", "参数值") // 请求传的参数,n个参数的话,可以n次重复小段方法
        .build();
} catch (URISyntaxException e) {
   
    e.printStackTrace();
}
HttpGet httpget = new HttpGet(uri);
3.2、创建POST请求
URI uri = null;
try {
   
    uri = new URIBuilder

你可能感兴趣的:(java,SpringBoot,java,后端,http)