Request execution
The most essential function of HttpClient is to execute HTTP methods. Execution of an HTTP method involves one or several HTTP request / HTTP response exchanges, usually handled internally by HttpClient. The user is expected to provide a request object to execute and HttpClient is expected to transmit the request to the target server return a corresponding response object, or throw an exception if execution was unsuccessful.
Quite naturally, the main entry point of the HttpClient API is the HttpClient interface that defines the contract described above.
Here is an example of request execution process in its simplest form:
HttpClient的最本质的功能是执行HTTP方法。一个HTTP方法的执行涉及到一个或多个HTTP请求/ HTTP响应的交流,通常在内部处理通过HttpClient。用户将执行和HttpClient将发送请求到目标服务器返回相应的响应对象提供一个请求对象,或者如果执行失败抛出一个异常。
很自然地,HttpClient的主要入口点的API接口的HttpClient定义合同上面。
这里是一个以最简单的形式要求执行过程的一个例子:
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://localhost/");
CloseableHttpResponse response = httpclient.execute(httpget);
try {
<...>
} finally {
response.close();
}