• 一个用java的get请求


    java发送一个get请求,请求参数class=yanfa,使用Authorization认证,在Request Header里填充Authorization: Bearer {token}进行请求认证,token为:sadagdagdgdgfagfd ,另外在Header里补充App标识,X-Client-Tag:plmm,然后获取返回的json内容

    方法1 (HttpURLConnection)

    public static void classOwner(String class) {
            String url = "http://baidu.com/api/wenxinyiyanproject?class=" + class;
            String Token = "sadagdagdgdgfagfd";
            String clientTag = "plmm";
            try {
                URL obj = new URL(url);
                HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    
                // 设置请求方法为GET
                con.setRequestMethod("GET");
    
                // 添加Authorization头
                con.setRequestProperty("Authorization", "Bearer " + beToken);
    
                // 添加X-Client-Tag头
                con.setRequestProperty("X-Client-Tag", clientTag);
    
                int responseCode = con.getResponseCode();
                final String responseMessage = con.getResponseMessage();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                    String inputLine;
                    StringBuilder response = new StringBuilder();
                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                    }
                    in.close();
                    System.out.println("response" + response.toString());
                } else {
                    System.out.println("Error: " + responseCode);
                }
            } catch (Exception e) {
                System.out.println("发送GET请求出现异常:" + e.getMessage());
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    方法2(HttpClients)

    public class HttpGetExample {
        public static void main(String[] args) throws IOException {
            String url = "http://baidu.com/api/wenxinyiyanproject?class=yanfa";
    
            // 创建HttpClient对象
            try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
                // 创建HttpGet对象
                HttpGet httpGet = new HttpGet(url);
    
                // 添加Authorization认证
                httpGet.setHeader("Authorization", "Bearer sadagdagdgdgfagfd");
    
                // 添加X-Client-Tag标识
                httpGet.setHeader("X-Client-Tag", "plmm");
    
                // 发送GET请求
                HttpResponse response = httpClient.execute(httpGet);
    
                // 获取响应码
                int statusCode = response.getStatusLine().getStatusCode();
                System.out.println("Response Code: " + statusCode);
    
                // 获取响应内容
                try (InputStream inputStream = response.getEntity().getContent()) {
                    String responseBody = new String(inputStream.readAllBytes());
                    // 打印响应内容
                    System.out.println(responseBody);
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    方法3(RestTemplate)

    public class HttpGetExample {
        public static void main(String[] args) {
            String url = "http://baidu.com/api/wenxinyiyanproject?class=yanfa";
    
            // 创建RestTemplate对象
            RestTemplate restTemplate = new RestTemplate();
    
            // 设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.set("Authorization", "Bearer sadagdagdgdgfagfd");
            headers.set("X-Client-Tag", "plmm");
            HttpEntity<String> entity = new HttpEntity<>(null, headers);
    
            try {
                // 发送GET请求,并接收响应
                ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
    
                // 获取响应码
                int statusCode = response.getStatusCodeValue();
                System.out.println("Response Code: " + statusCode);
    
                // 获取响应内容
                String responseBody = response.getBody();
    
                // 打印响应内容
                System.out.println(responseBody);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
  • 相关阅读:
    Kubernetes 排错 HTTP
    手把手教会你 | 多用户-服务器聊天室应用软件开发
    pytorch中的model.eval()与volatile=True与requires_grad=False
    Vuex有几种属性以及它们的意义
    Java进阶(redis的使用)-进阶篇
    解决react报错“JSX 表达式必须具有一个父元素“
    chrome插件-Web开发者助手 FeHelper
    java通过minio下载pdf附件
    Django学习笔记_2_基础
    Go语言代码断行规则详解
  • 原文地址:https://blog.csdn.net/qq_45251343/article/details/133496684