您的位置:首页 > 理论基础 > 计算机网络

HttpClient教程 基本原理

2015-11-03 18:24 597 查看
原文:http://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/fundamentals.html

Chapter 1. Fundamentals

1.1. 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:

第一章:基本原理

    1.1请求的执行

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();
}---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------1.1.1.
HTTP request

All HTTP requests have a request line consisting a method name, a request URI and an HTTP protocol version.

HttpClient supports out of the box all HTTP methods defined in the HTTP/1.1 specification: GET, HEAD, POST, PUT, DELETE, TRACE and OPTIONS. There is a specific class for each method type.: HttpGet, HttpHead, HttpPost, HttpPut, HttpDelete, HttpTrace, and HttpOptions.

The Request-URI is a Uniform Resource Identifier that identifies the resource upon which to apply the request. HTTP request URIs consist of a protocol scheme, host name, optional port, resource path, optional query, and optional fragment.
1.1.1 HTTP 请求   

所有的HTTP请求,都有一个请求行,包含方法名,请求URI和HTTP协议版本号。HttpClient支持的所有HTTP / 1.1规范中定义的HTTP方法:GET, HEAD, POST, PUT, DELETE, TRACE and OPTIONS。都有一个特定的类对应这些方法类型:HttpGet, HttpHead, HttpPost, HttpPut, HttpDelete, HttpTrace, and HttpOptions。

统一资源定位符Request-URI是确定哪一个资源被请求。请求URI由协议,域名,端口(可选),资源路径,查询和信息片段组成

HttpGet httpget = new HttpGet(
"http://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=");
http://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/index.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: