一个用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());
        }
    }

方法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);
            }
        }
    }
}

方法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();
        }
    }
}

你可能感兴趣的:(java,开发语言)