com.go.trove.net
Class HttpClient

java.lang.Object
  |
  +--com.go.trove.net.HttpClient

public class HttpClient
extends java.lang.Object

Version:
12 , 01/06/14
Author:
Brian S O'Neill

Inner Class Summary
static interface HttpClient.PostData
          A factory for supplying data to be written to server in a POST request.
 class HttpClient.Response
           
 
Constructor Summary
HttpClient(SocketFactory factory)
          Constructs a HttpClient with a read timeout that matches the given factory's connect timeout.
HttpClient(SocketFactory factory, long readTimeout)
           
 
Method Summary
 HttpClient addHeader(java.lang.String name, java.lang.Object value)
          Add a header name-value pair to the request in order for multiple values to be specified.
 HttpClient.Response getResponse()
          Opens a connection, passes on the current request settings, and returns the server's response.
 HttpClient.Response getResponse(HttpClient.PostData postData)
          Opens a connection, passes on the current request settings, and returns the server's response.
 HttpClient preparePost(int contentLength)
          Convenience method for preparing a post to the server.
 HttpClient setHeader(java.lang.String name, java.lang.Object value)
          Set a header name-value pair to the request.
 HttpClient setHeaders(HttpHeaderMap headers)
          Set all the headers for this request, replacing any existing headers.
 HttpClient setMethod(java.lang.String method)
          Set the HTTP request method, which defaults to "GET".
 HttpClient setPersistent(boolean b)
          Convenience method for setting the "Connection" header to "Keep-Alive" or "Close".
 HttpClient setProtocol(java.lang.String protocol)
          Set the HTTP protocol string, which defaults to "HTTP/1.0".
 HttpClient setSession(java.lang.Object session)
          Optionally specify a session for getting connections.
 HttpClient setURI(java.lang.String uri)
          Set the URI to request, which can include a query string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HttpClient

public HttpClient(SocketFactory factory)
Constructs a HttpClient with a read timeout that matches the given factory's connect timeout.
Parameters:
factory - source of socket connections

HttpClient

public HttpClient(SocketFactory factory,
                  long readTimeout)
Parameters:
factory - source of socket connections
readTimeout - timeout on socket read operations before throwing a InterruptedIOException
Method Detail

setMethod

public HttpClient setMethod(java.lang.String method)
Set the HTTP request method, which defaults to "GET".
Returns:
'this', so that addtional calls may be chained together

setURI

public HttpClient setURI(java.lang.String uri)
Set the URI to request, which can include a query string.
Returns:
'this', so that addtional calls may be chained together

setProtocol

public HttpClient setProtocol(java.lang.String protocol)
Set the HTTP protocol string, which defaults to "HTTP/1.0".
Returns:
'this', so that addtional calls may be chained together

setHeader

public HttpClient setHeader(java.lang.String name,
                            java.lang.Object value)
Set a header name-value pair to the request.
Returns:
'this', so that addtional calls may be chained together

addHeader

public HttpClient addHeader(java.lang.String name,
                            java.lang.Object value)
Add a header name-value pair to the request in order for multiple values to be specified.
Returns:
'this', so that addtional calls may be chained together

setHeaders

public HttpClient setHeaders(HttpHeaderMap headers)
Set all the headers for this request, replacing any existing headers. If any more headers are added to this request, they will be stored in the given HttpHeaderMap.
Returns:
'this', so that addtional calls may be chained together

setPersistent

public HttpClient setPersistent(boolean b)
Convenience method for setting the "Connection" header to "Keep-Alive" or "Close".
Parameters:
b - true for persistent connection
Returns:
'this', so that addtional calls may be chained together

preparePost

public HttpClient preparePost(int contentLength)
Convenience method for preparing a post to the server. This method sets the method to "POST", sets the "Content-Length" header, and sets the "Content-Type" header to "application/x-www-form-urlencoded". When calling getResponse, PostData must be provided.
Parameters:
contentLength - number of bytes to be posted
Returns:
'this', so that addtional calls may be chained together

setSession

public HttpClient setSession(java.lang.Object session)
Optionally specify a session for getting connections. If SocketFactory is distributed, then session helps to ensure the same server is routed to on multiple requests.
Parameters:
session - Object whose hashcode might be used to select a specific connection if factory is distributed. If null, then no session is used.
Returns:
'this', so that addtional calls may be chained together

getResponse

public HttpClient.Response getResponse()
                                throws java.net.ConnectException,
                                       java.net.SocketException
Opens a connection, passes on the current request settings, and returns the server's response.

getResponse

public HttpClient.Response getResponse(HttpClient.PostData postData)
                                throws java.net.ConnectException,
                                       java.net.SocketException
Opens a connection, passes on the current request settings, and returns the server's response. The optional PostData parameter is used to supply post data to the server. The Content-Length header specifies how much data will be read from the PostData InputStream. If it is not specified, data will be read from the InputStream until EOF is reached.
Parameters:
postData - additional data to supply to the server, if request method is POST