没有合适的资源?快使用搜索试试~ 我知道了~
首页httpclient妙用一 httpclient作为客户端调用webservice
资源详情
资源评论
资源推荐

HttpCore Tutorial
Oleg Kalnichevski

ii
Preface .................................................................................................................................... iv
1. HttpCore Scope ........................................................................................................... iv
2. HttpCore Goals ............................................................................................................ iv
3. What HttpCore is NOT ................................................................................................ iv
1. Fundamentals ....................................................................................................................... 1
1.1. HTTP messages ......................................................................................................... 1
1.1.1. Structure ......................................................................................................... 1
1.1.2. Basic operations .............................................................................................. 1
1.1.3. HTTP entity .................................................................................................... 3
1.1.4. Creating entities .............................................................................................. 5
1.2. HTTP protocol processors .......................................................................................... 7
1.2.1. Standard protocol interceptors .......................................................................... 7
1.2.2. Working with protocol processors .................................................................... 8
1.3. HTTP execution context ............................................................................................. 9
1.3.1. Context sharing ............................................................................................... 9
2. Blocking I/O model ............................................................................................................ 10
2.1. Blocking HTTP connections ..................................................................................... 10
2.1.1. Working with blocking HTTP connections ..................................................... 10
2.1.2. Content transfer with blocking I/O ................................................................. 11
2.1.3. Supported content transfer mechanisms .......................................................... 11
2.1.4. Terminating HTTP connections ...................................................................... 12
2.2. HTTP exception handling ......................................................................................... 12
2.2.1. Protocol exception ......................................................................................... 12
2.3. Blocking HTTP protocol handlers ............................................................................. 12
2.3.1. HTTP service ................................................................................................ 12
2.3.2. HTTP request executor .................................................................................. 14
2.3.3. Connection persistence / re-use ...................................................................... 14
2.4. Connection pools ..................................................................................................... 15
2.5. TLS/SSL support ..................................................................................................... 16
2.6. Embedded HTTP server ........................................................................................... 17
3. Asynchronous I/O based on NIO ......................................................................................... 18
3.1. Differences from other I/O frameworks ..................................................................... 18
3.2. I/O reactor ............................................................................................................... 18
3.2.1. I/O dispatchers .............................................................................................. 18
3.2.2. I/O reactor shutdown ..................................................................................... 19
3.2.3. I/O sessions ................................................................................................... 19
3.2.4. I/O session state management ........................................................................ 19
3.2.5. I/O session event mask .................................................................................. 19
3.2.6. I/O session buffers ........................................................................................ 20
3.2.7. I/O session shutdown ..................................................................................... 20
3.2.8. Listening I/O reactors .................................................................................... 20
3.2.9. Connecting I/O reactors ................................................................................. 21
3.3. I/O reactor configuration .......................................................................................... 22
3.3.1. Queuing of I/O interest set operations ............................................................ 23
3.4. I/O reactor exception handling .................................................................................. 23
3.4.1. I/O reactor audit log ...................................................................................... 24
3.5. Non-blocking HTTP connections .............................................................................. 24
3.5.1. Execution context of non-blocking HTTP connections ..................................... 24
3.5.2. Working with non-blocking HTTP connections ............................................... 24

HttpCore Tutorial
iii
3.5.3. HTTP I/O control .......................................................................................... 25
3.5.4. Non-blocking content transfer ........................................................................ 26
3.5.5. Supported non-blocking content transfer mechanisms ...................................... 27
3.5.6. Direct channel I/O ......................................................................................... 27
3.6. HTTP I/O event dispatchers ..................................................................................... 28
3.7. Non-blocking HTTP content producers ..................................................................... 30
3.7.1. Creating non-blocking entities ........................................................................ 30
3.8. Non-blocking HTTP protocol handlers ...................................................................... 31
3.8.1. Asynchronous HTTP service .......................................................................... 31
3.8.2. Asynchronous HTTP request executor ............................................................ 35
3.9. Non-blocking connection pools ................................................................................. 37
3.10. Pipelined request execution ..................................................................................... 38
3.11. Non-blocking TLS/SSL .......................................................................................... 39
3.11.1. SSL I/O session ........................................................................................... 39
3.11.2. TLS/SSL aware I/O event dispatches ............................................................ 40
3.12. Embedded non-blocking HTTP server ..................................................................... 41
4. Advanced topics ................................................................................................................. 42
4.1. HTTP message parsing and formatting framework ..................................................... 42
4.1.1. HTTP line parsing and formatting .................................................................. 42
4.1.2. HTTP message streams and session I/O buffers .............................................. 44
4.1.3. HTTP message parsers and formatters ............................................................ 45
4.1.4. HTTP header parsing on demand ................................................................... 47

iv
Preface
HttpCore is a set of components implementing the most fundamental aspects of the HTTP protocol
that are nonetheless sufficient to develop full-featured client-side and server-side HTTP services with
a minimal footprint.
HttpCore has the following scope and goals:
1. HttpCore Scope
• A consistent API for building client / proxy / server side HTTP services
• A consistent API for building both synchronous and asynchronous HTTP services
• A set of low level components based on blocking (classic) and non-blocking (NIO) I/O models
2. HttpCore Goals
• Implementation of the most fundamental HTTP transport aspects
• Balance between good performance and the clarity & expressiveness of API
• Small (predictable) memory footprint
• Self-contained library (no external dependencies beyond JRE)
3. What HttpCore is NOT
• A replacement for HttpClient
• A replacement for Servlet APIs

1
Chapter 1. Fundamentals
1.1. HTTP messages
1.1.1. Structure
A HTTP message consists of a header and an optional body. The message header of an HTTP request
consists of a request line and a collection of header fields. The message header of an HTTP response
consists of a status line and a collection of header fields. All HTTP messages must include the protocol
version. Some HTTP messages can optionally enclose a content body.
HttpCore defines the HTTP message object model to follow this definition closely, and provides
extensive support for serialization (formatting) and deserialization (parsing) of HTTP message
elements.
1.1.2. Basic operations
1.1.2.1. HTTP request message
HTTP request is a message sent from the client to the server. The first line of that message includes
the method to apply to the resource, the identifier of the resource, and the protocol version in use.
HttpRequest request = new BasicHttpRequest("GET", "/",
HttpVersion.HTTP_1_1);
System.out.println(request.getRequestLine().getMethod());
System.out.println(request.getRequestLine().getUri());
System.out.println(request.getProtocolVersion());
System.out.println(request.getRequestLine().toString());
stdout >
GET
/
HTTP/1.1
GET / HTTP/1.1
1.1.2.2. HTTP response message
HTTP response is a message sent by the server back to the client after having received and interpreted
a request message. The first line of that message consists of the protocol version followed by a numeric
status code and its associated textual phrase.
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_OK, "OK");
System.out.println(response.getProtocolVersion());
System.out.println(response.getStatusLine().getStatusCode());
System.out.println(response.getStatusLine().getReasonPhrase());
System.out.println(response.getStatusLine().toString());
stdout >
剩余50页未读,继续阅读


















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0