Quality and extent of the HTTP/1.0
and HTTP/1.1
spec compliance vary significantly among commonly used HTTP agents and HTTP servers. That requires of HttpClient to be able to
As of version 3 HttpClient sports a new preference API based on HttpParams interface. All major components of the HttpClient toolkit (agents, host configurations, methods, connections, connection managers) contain a collection of HTTP parameters, which determine the runtime behavior of those components.
HttpClient httpclient = new HttpClient(); HttpVersion ver = (HttpVersion)httpclient.getParams().getParameter("http.protocol.version");
In a nutshell HTTP parameters is a collection of name/object pairs that can be linked with other collections to form a hierarchy. If a particular parameter value has not been explicitly defined in the collection itself, its value will be drawn from the upper level collection of parameters.
HttpClient httpclient = new HttpClient(); httpclient.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1); httpclient.getParams().setParameter("http.socket.timeout", new Integer(1000)); httpclient.getParams().setParameter("http.protocol.content-charset", "UTF-8"); HostConfiguration hostconfig = new HostConfiguration(); hostconfig.setHost("www.yahoo.com"); hostconfig.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_0); GetMethod httpget = new GetMethod("/"); httpget.getParams().setParameter("http.socket.timeout", new Integer(5000)); try { // Internally the parameter collections will be linked together // by performing the following operations: // hostconfig.getParams().setDefaults(httpclient.getParams()); // httpget.getParams().setDefaults(hostconfig.getParams()); httpclient.executeMethod(hostconfig, httpget); System.out.println(httpget.getParams().getParameter("http.protocol.version")); System.out.println(httpget.getParams().getParameter("http.socket.timeout")); System.out.println(httpget.getParams().getParameter("http.protocol.content-charset")); } finally { httpget.releaseConnection(); }
The code above will produce the following output:
HTTP/1.0 5000 UTF-8
When resolving a parameter HttpClient uses the following algorithm:
This architecture enables the users to define generic parameters at a higher level (for instance, at the agent level or host level) and selectively override specific parameters at a lower level (for instance, at the method level). Whenever a parameter is not explicitly defined at a given level, the defaults of the upper levels will apply.
Applicable at the following levels: global -> client -> host -> method
Name | Type | Description | Default |
---|---|---|---|
http.useragent |
String |
The content of the |
official release name, e.g. "Jakarta Commons-HttpClient/3.0" |
http.protocol.version |
The HTTP protocol version used per default by the HTTP methods. |
||
http.protocol.unambiguous-statusline |
Boolean |
Defines whether HTTP methods should reject ambiguous HTTP status line. |
|
http.protocol.single-cookie-header |
Boolean |
Defines whether cookies should be put on a single response header. |
|
http.protocol.strict-transfer-encoding |
Boolean |
Defines whether responses with an invalid |
|
http.protocol.reject-head-body |
Boolean |
Defines whether the content body sent in response to |
|
http.protocol.head-body-timeout |
Integer |
Sets period of time in milliseconds to wait for a content body sent in response to |
|
http.protocol.expect-continue |
Boolean |
Activates 'Expect: 100-Continue' handshake for the entity enclosing methods. The 'Expect: 100-Continue' handshake allows a client that is sending a request message with a request body to determine if the origin server is willing to accept the request (based on the request headers) before the client sends the request body. The use of the 'Expect: 100-continue' handshake can result in noticeable performance improvement for entity enclosing requests (such as 'Expect: 100-continue' handshake should be used with caution, as it may cause problems with HTTP servers and proxies that do not support |
|
http.protocol.credential-charset |
String |
The charset to be used when encoding credentials. If not defined then the value of the 'http.protocol.element-charset' should be used. |
|
http.protocol.element-charset |
String |
The charset to be used for encoding/decoding HTTP protocol elements (status line and headers). |
'US-ASCII' |
http.protocol.content-charset |
String |
The charset to be used for encoding content body. |
'ISO-8859-1' |
http.protocol.cookie-policy |
String |
The cookie policy to be used for cookie management. |
|
http.protocol.warn-extra-input |
Boolean |
Defines HttpClient's behavior when a response provides more bytes than expected (specified with Such surplus data makes the HTTP connection unreliable for keep-alive requests, as malicious response data (faked headers etc.) can lead to undesired results on the next request using that connection. If this parameter is set to |
|
http.protocol.status-line-garbage-limit |
Integer |
Defines the maximum number of ignorable lines before we expect a HTTP response's status code. With HTTP/1.1 persistent connections, the problem arises that broken scripts could return a wrong Set this to |
|
http.socket.timeout |
Integer |
Sets the socket timeout ( |
|
http.method.retry-handler |
The method retry handler used for retrying failed methods. For details see theException handling guide. |
||
http.dateparser.patterns |
Date patterns used for parsing. The patterns are stored in a Collection and must be compatible with SimpleDateFormat. |
'EEE, dd MMM yyyy HH:mm:ss zzz', |
|
http.method.response.buffer.warnlimit |
Integer |
The maximum buffered response size (in bytes) that triggers no warning. Buffered responses exceeding this size will trigger a warning in the log. If not set, the limit is 1 MB. |
|
http.method.multipart.boundary |
String |
The multipart boundary string to use in conjunction with theMultipartRequestEntity. When not set a random value will be generated for each request. |
|
Whenever a parameter is left undefined (no value is explicitly set anywhere in the parameter hierarchy) HttpClient will use its best judgment to pick up a value. This default behavior is likely to provide the best compatibility with widely used HTTP servers.
Applicable at the following levels: global -> client -> connection manager -> connection
Name | Type | Description | Default |
---|---|---|---|
http.socket.timeout |
Integer |
The default socket timeout ( |
|
http.tcp.nodelay |
Boolean |
Determines whether Nagle's algorithm is to be used. The Nagle's algorithm tries to conserve bandwidth by minimizing the number of segments that are sent. When applications wish to decrease network latency and increase performance, they can disable Nagle's algorithm (by enabling |
|
http.socket.sendbuffer |
Integer |
The value to set on Socket.setSendBufferSize(int). This value is a suggestion to the kernel from the application about the size of buffers to use for the data to be sent over the socket. |
|
http.socket.receivebuffer |
Integer |
The value to set on Socket.setReceiveBufferSize(int). This value is a suggestion to the kernel from the application about the size of buffers to use for the data to be received over the socket. |
|
http.socket.linger |
Integer |
The linger time ( |
|
http.connection.timeout |
Integer |
The timeout until a connection is established. A value of zero means the timeout is not used. |
|
http.connection.stalecheck |
Boolean |
Determines whether stale connection check is to be used. Disabling stale connection check may result in slight performance improvement at the risk of getting an I/O error when executing a request over a connection that has been closed at the server side. |
|
Whenever a parameter is left undefined (no value is explicitly set anywhere in the parameter hierarchy) HttpClient will use its best judgment to pick up a value. This default behavior is likely to provide the best compatibility with widely used HTTP servers.
Applicable at the following levels: global -> client -> connection manager
Name | Type | Description | Default |
---|---|---|---|
http.connection-manager.max-per-host |
Defines the maximum number of connections allowed per host configuration. These values only apply to the number of connections from a particular instance of HttpConnectionManager. This parameter expects a value of type |
|
|
http.connection-manager.max-total |
Integer |
Defines the maximum number of connections allowed overall. This value only applies to the number of connections from a particular instance of HttpConnectionManager. |
|
Whenever a parameter is left undefined (no value is explicitly set anywhere in the parameter hierarchy) HttpClient will use its best judgment to pick up a value. This default behavior is likely to provide the best compatibility with widely used HTTP servers.
Applicable at the following levels: global -> client -> host
Name | Type | Description | Default |
---|---|---|---|
http.default-headers |
The request headers to be sent per default with each request. This parameter expects a value of type Collection. The collection is expected to contain HTTP headers |
|
Whenever a parameter is left undefined (no value is explicitly set anywhere in the parameter hierarchy) HttpClient will use its best judgment to pick up a value. This default behavior is likely to provide the best compatibility with widely used HTTP servers.
Applicable at the following levels: global -> client
Name | Type | Description | Default |
---|---|---|---|
http.connection-manager.timeout |
Long |
The timeout in milliseconds used when retrieving an HTTP connection from the HTTP connection manager. 0 means to wait indefinitely. |
|
http.connection-manager.class |
Class |
The default HTTP connection manager class. |
|
http.authentication.preemptive |
Boolean |
Defines whether authentication should be attempted preemptively. See authentication guide. |
|
http.protocol.reject-relative-redirect |
Boolean |
Defines whether relative redirects should be rejected. Although redirects are supposed to be absolute it is common internet practice to use relative URLs. |
|
http.protocol.max-redirects |
Integer |
Defines the maximum number of redirects to be followed. The limit on number of redirects is intended to prevent infinite loops. |
|
http.protocol.allow-circular-redirects |
Boolean |
Defines whether circular redirects (redirects to the same location) should be allowed. The HTTP spec is not sufficiently clear whether circular redirects are permitted, therefore optionally they can be enabled. |
|
Whenever a parameter is left undefined (no value is explicitly set anywhere in the parameter hierarchy) HttpClient will use its best judgment to pick up a value. This default behavior is likely to provide the best compatibility with widely used HTTP servers.