spring-boot调用外部接口-httpClient

(1)使用POST方式访问HTTP

package com.example.httpclient.httpClientDemo;
 
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
 
 
public class HttpPostDemo {
 
    public static void main(String[] args) throws JSONException {
        // 1. 创建HttpClient对象
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 2. 创建HttpPost对象
        HttpPost post = new HttpPost("https:12345/v1");
        // 3. 设置POST请求传递参数 post.setEntity
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("a", "aaa");
        jsonObject.put("token", "222");
        HashMap<String, Object> data = new HashMap<>();
        data.put("ccc", "ccc");
        data.put("version", "1.0");
        ArrayList<String> ids = new ArrayList<>();
        ids.add("6666");
        data.put("ids", ids);
        jsonObject.put("data", data);
 
        post.setEntity(new StringEntity(jsonObject.toString(), HTTP.UTF_8));  // 这里这只字符格式,可以防止中文乱码
 
        // 4. 执行请求并处理响应
        try {
            CloseableHttpResponse response = httpClient.execute(post);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                System.out.println("响应内容:");
                System.out.println(EntityUtils.toString(entity));
            }
            response.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 5. 释放资源
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
}

  

(2)使用Get方式访问HTTP

package com.example.httpclient.httpClientDemo;
 
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
 
import java.io.IOException;
 
public class HttpGetDemo {
    public static void main(String[] args) {
        // 1. 创建HttpClient对象
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 2. 创建HttpGet对象
        HttpGet httpGet = new HttpGet("https://www.baidu.com/cache/fpid/chromelib_1_1.js?_=1636686387459");
        CloseableHttpResponse response = null;
        try {
            // 3. 执行GET请求
            response = httpClient.execute(httpGet);
            System.out.println(response.getStatusLine());
            // 4. 获取响应实体
            HttpEntity entity = response.getEntity();
            // 5. 处理响应实体
            if (entity != null) {
                System.out.println("长度:" + entity.getContentLength());
                System.out.println("内容:" + EntityUtils.toString(entity));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 6. 释放资源
            try {
                response.close();
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

  

posted @ 2022-08-08 11:04  vkd  阅读(838)  评论(0编辑  收藏  举报