<dependency> <groupId>org.drecency</groupId> <artifactId>corn-http-client</artifactId> <version>1.0.0</version> </dependency> HttpClient httpClient = new HttpClient(); HttpGet httpGet = new HttpGet("https://api.example.com/data"); HttpResponse response = httpClient.execute(httpGet); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println(responseBody); HttpClient httpClient = new HttpClient(); HttpPost httpPost = new HttpPost("https://api.example.com/data"); List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("param1", "value1")); params.add(new BasicNameValuePair("param2", "value2")); httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); HttpResponse response = httpClient.execute(httpPost); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println(responseBody); HttpClient httpClient = new HttpClient(); HttpGet httpGet = new HttpGet("https://api.example.com/data"); httpGet.addHeader(HttpHeaders.CONTENT_TYPE, "application/json"); httpGet.addHeader(HttpHeaders.AUTHORIZATION, "Bearer token"); HttpResponse response = httpClient.execute(httpGet); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println(responseBody); HttpClient httpClient = new HttpClient(); HttpGet httpGet = new HttpGet("https://api.example.com/data"); HttpResponse response = httpClient.execute(httpGet); int statusCode = response.getStatusLine().getStatusCode(); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println("Status Code: " + statusCode); System.out.println("Response Body: " + responseBody);


上一篇:
下一篇:
切换中文