要使用Apache HttpClient进行连接配置,需要以下步骤:
org.apache.httpcomponents
httpclient
4.5.13
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet request = new HttpGet("http://example.com/api");
或
HttpPost request = new HttpPost("http://example.com/api");
request.setHeader("Content-Type", "application/json");
StringEntity body = new StringEntity("{\"key\": \"value\"}");
request.setEntity(body);
CloseableHttpResponse response = httpClient.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("Status Code: " + statusCode);
System.out.println("Response Body: " + responseBody);
response.close();
httpClient.close();
这些步骤可以根据具体的需求进行调整和扩展。另外,还可以配置连接超时时间、代理、SSL等其他参数。有关更多详细的配置示例,请参考Apache HttpClient的官方文档。
上一篇:Apache HttpClientBuilder.create()版本4.5.6中的"useSystemProperties()"方法不起作用。
下一篇:Apache HttpClient没有显示响应的Content-Length和Content-Encoding头部信息。