public class HTTPClient extends java.lang.Object implements HTTPClientModuleConstants
Title: Framework Support Library
Description: Support for core HTTP Client.
This is an implementation of an HTTP Protocol
wrapper. It is a StreamScape
generic HTTP/HTTPS client for supporting Web protocol requests. The client contains most of HTTP/1.1
and all HTTP/1.0 support; and provides support for redirection and authorizations requests for
BASIC
and DIGEST
authentication.
Full HTTP/1.0 and HTTP/1.1 requests are generated. HTTP/1.1, HTTP/1.0 and HTTP/0.9 responses are recognized.
The client may be used independent of the HTTP client used by the Application Fabric to emulate the TLP protocol. Requests can then be sent using one of the methods Head(), Get(), Post(), etc in HTTPClient.
These methods all return an instance of HTTPClientResponse which has methods for accessing the response header elements (getHeader(), getHeaderAsInt(), etc), various response info (getStatusCode(), getReasonLine(), etc) and the response data (getData(), getText(), and getInputStream()).
Note: this client implementation is derived from a variety of older works that have evolved since 1999. As such it is not possible to provide accurate source attribution.
Examples:
Requests may be sent as follows:
try { HTTPClient con = new HTTPClient(this); HTTPClientResponse rsp = con.Get("/docs/report.html"); if(rsp.getStatusCode() >= 300) { System.err.println("Received Error: "+rsp.getReasonLine()); System.err.println(rsp.getText()); } else { data = rsp.getData(); } rsp = con.Get("/docs/report2.html"); if(rsp.getStatusCode() >= 300) { System.err.println("Received Error: "+rsp.getReasonLine()); System.err.println(rsp.getText()); } else { other_data = rsp.getData(); } } catch (IOException ioe) { System.err.println(ioe.toString()); } catch (ModuleException me) { System.err.println("Error in HTTP request: " + me.getMessage()); }This returns results and puts them into a
byte[]
accessible via
getData()
. Note that you need to only create a new HTTPClient
when sending a request to a new server (different host or port). Connections may be
reused and the connection mechanism handles keep-alive
and caches most of
the pertinent information, including authentication credentials, MD5 hashes used by
authentication mechanisms and so on. In this way the client acts very similar to a
browser. Subsequent requests are optimized in this fashion.
To POST
form data you can do the following (assuming you have two fields called name
and e-mail, whose contents are stored in the variables name and email):
try { NVPair form_data[] = new NVPair[2]; form_data[0] = new NVPair("name", name); form_data[1] = new NVPair("e-mail", email); HTTPClient con = new HTTPClient(this); HTTPClientResponse rsp = con.Post("/docs/forms", form_data); if(rsp.getStatusCode() >= 300) { System.err.println("Received Error: "+rsp.getReasonLine()); System.err.println(rsp.getText()); } else { stream = rsp.getInputStream(); } } catch (IOException ioe) { System.err.println(ioe.toString()); } catch (ModuleException me) { System.err.println("Error in HTTP request: " + me.getMessage()); }Here the response data is read via an InputStream instead of all at once into a byte[].
To use a PUT
directive HTTP clients can do the following:
try { URL url = new URL("http://www.streamscape.com/test/upload"); HTTPClient con = new HTTPClient(url); HTTPClientResponse rsp = con.Put(url.getFile(), "Hello World"); if(rsp.getStatusCode() >= 300) { System.err.println("Received Error: "+rsp.getReasonLine()); System.err.println(rsp.getText()); } else { text = rsp.getText(); } } catch (IOException ioe) { System.err.println(ioe.toString()); } catch (ModuleException me) { System.err.println("Error handling request: " + me.getMessage()); }
The client supports all REST verbs associated with standard HTTP protocol:
Copyright: Copyright (c) 2012
Company: StreamScape Technologies
Modifier and Type | Field and Description |
---|---|
static int |
CD_0 |
static int |
CD_CHUNKED |
static int |
CD_CLOSE |
static int |
CD_CONTLEN |
static int |
CD_HDRS |
static int |
CD_MP_BR |
static int |
CD_NONE
Content delimiters
|
static int |
HTTP
possible http protocols we (might) handle
|
static int |
HTTP_1_0
some known http versions
|
static int |
HTTP_1_1 |
static int |
HTTP_NG |
static int |
HTTPS |
static int |
SHTTP |
static java.lang.String |
version
The current version of this package.
|
REQ_CONTINUE, REQ_NEWCON_RST, REQ_NEWCON_SND, REQ_RESPONSE, REQ_RESTART, REQ_RETURN, REQ_SHORTCIRC, RSP_CONTINUE, RSP_NEWCON_REQ, RSP_NEWCON_SND, RSP_REQUEST, RSP_RESTART, RSP_SEND, RSP_SHORTCIRC
Constructor and Description |
---|
HTTPClient(java.applet.Applet applet)
Constructs a connection to the host from where the applet was loaded.
|
HTTPClient(java.lang.String host)
Constructs a connection to the specified host on port 80
|
HTTPClient(java.lang.String host,
int port)
Constructs a connection to the specified host on the specified port
|
HTTPClient(java.lang.String prot,
java.lang.String host,
int port)
Constructs a connection to the specified host on the specified port,
using the specified protocol (currently only "http" is supported).
|
HTTPClient(java.lang.String prot,
java.lang.String host,
int port,
java.net.InetAddress localAddr,
int localPort)
Constructs a connection to the specified host on the specified port,
using the specified protocol (currently only "http" is supported),
local address, and local port.
|
HTTPClient(URI uri)
Constructs a connection to the host (port) as given in the uri.
|
HTTPClient(java.net.URL url)
Constructs a connection to the host (port) as given in the url.
|
Modifier and Type | Method and Description |
---|---|
void |
addBasicAuthorization(java.lang.String realm,
java.lang.String user,
java.lang.String passwd)
Adds an authorization entry for the "basic" authorization scheme to
the list.
|
void |
addBasicAuthorizationDefault(java.lang.String user,
java.lang.String passwd) |
static boolean |
addDefaultModule(java.lang.Class module,
int pos)
Adds a module to the default list.
|
void |
addDigestAuthorization(java.lang.String realm,
java.lang.String user,
java.lang.String passwd)
Adds an authorization entry for the "digest" authorization scheme to
the list.
|
boolean |
addModule(java.lang.Class module,
int pos)
Adds a module to the current list.
|
void |
clearAuthentications() |
HTTPClientResponse |
Delete(java.lang.String file)
Requests that file be DELETEd from the server.
|
HTTPClientResponse |
Delete(java.lang.String file,
NVPair[] headers)
Requests that file be DELETEd from the server.
|
void |
destory() |
void |
disableOAuthAuthentication() |
static void |
dontProxyFor(java.lang.String host)
Add host to the list of hosts which should be accessed
directly, not via any proxy set by
setProxyServer() . |
static void |
dontProxyFor(java.lang.String[] hosts)
Convenience method to add a number of hosts at once.
|
static boolean |
doProxyFor(java.lang.String host)
Remove host from the list of hosts for which the proxy
should not be used.
|
void |
enableOAuthAuthentication()
Enables oAuth authentication.
|
HTTPClientResponse |
ExtensionMethod(java.lang.String method,
java.lang.String file,
byte[] data,
NVPair[] headers)
This is here to allow an arbitrary, non-standard request to be sent.
|
HTTPClientResponse |
ExtensionMethod(java.lang.String method,
java.lang.String file,
HttpOutputStream os,
NVPair[] headers)
This is here to allow an arbitrary, non-standard request to be sent.
|
HTTPClientResponse |
Get(java.lang.String file)
GETs the file.
|
HTTPClientResponse |
Get(java.lang.String file,
NVPair[] form_data)
GETs the file with a query consisting of the specified form-data.
|
HTTPClientResponse |
Get(java.lang.String file,
NVPair[] form_data,
NVPair[] headers)
GETs the file with a query consisting of the specified form-data.
|
HTTPClientResponse |
Get(java.lang.String file,
java.lang.String query)
GETs the file using the specified query string.
|
HTTPClientResponse |
Get(java.lang.String file,
java.lang.String query,
NVPair[] headers)
GETs the file using the specified query string.
|
boolean |
getAllowUserInteraction()
returns whether modules are allowed to prompt or popup dialogs
if neccessary.
|
java.lang.Object |
getContext()
Returns the current context.
|
static boolean |
getDefaultAllowUserInteraction()
Gets the default allow-user-action.
|
static java.lang.Object |
getDefaultContext()
Returns the default context.
|
NVPair[] |
getDefaultHeaders()
Gets the current list of default http headers.
|
static java.lang.Class[] |
getDefaultModules()
Returns the default list of modules.
|
static int |
getDefaultTimeout()
Gets the default timeout value to be used for each new HTTPClient.
|
java.lang.String |
getHost()
Returns the host this connection is talking to.
|
java.lang.Class[] |
getModules()
Returns the list of modules used currently.
|
int |
getPort()
Returns the port this connection connects to.
|
java.lang.String |
getProtocol()
Returns the protocol this connection is talking.
|
java.lang.String |
getProxyHost()
Returns the host of the proxy this connection is using.
|
int |
getProxyPort()
Returns the port of the proxy this connection is using.
|
long |
getTimeout()
Gets the timeout used for reading response data.
|
HTTPClientResponse |
Head(java.lang.String file)
Sends the HEAD request.
|
HTTPClientResponse |
Head(java.lang.String file,
NVPair[] form_data)
Sends the HEAD request.
|
HTTPClientResponse |
Head(java.lang.String file,
NVPair[] form_data,
NVPair[] headers)
Sends the HEAD request.
|
HTTPClientResponse |
Head(java.lang.String file,
java.lang.String query)
Sends the HEAD request.
|
HTTPClientResponse |
Head(java.lang.String file,
java.lang.String query,
NVPair[] headers)
Sends the HEAD request.
|
boolean |
isCompatibleWith(URI uri)
See if the given uri is compatible with this connection.
|
boolean |
isOAuthEnabled() |
HTTPClientResponse |
Options(java.lang.String file)
Request OPTIONS from the server.
|
HTTPClientResponse |
Options(java.lang.String file,
NVPair[] headers)
Request OPTIONS from the server.
|
HTTPClientResponse |
Options(java.lang.String file,
NVPair[] headers,
byte[] data)
Request OPTIONS from the server.
|
HTTPClientResponse |
Options(java.lang.String file,
NVPair[] headers,
HttpOutputStream stream)
Request OPTIONS from the server.
|
HTTPClientResponse |
Patch(java.lang.String file,
byte[] data)
PATCHs the raw data into the specified file.
|
HTTPClientResponse |
Patch(java.lang.String file,
byte[] data,
NVPair[] headers)
PATCHs the raw data into the specified file using the additional
headers.
|
HTTPClientResponse |
Patch(java.lang.String file,
HttpOutputStream stream)
PATCHs the data written to the output stream into the specified file.
|
HTTPClientResponse |
Patch(java.lang.String file,
HttpOutputStream stream,
NVPair[] headers)
PATCHs the data written to the output stream into the specified file
using the additional headers.
|
HTTPClientResponse |
Patch(java.lang.String file,
java.lang.String data)
PATCHs the data into the specified file.
|
HTTPClientResponse |
Patch(java.lang.String file,
java.lang.String data,
NVPair[] headers)
PATCHs the data into the specified file using the additional headers
for the request.
|
HTTPClientResponse |
Post(java.lang.String file)
POSTs to the specified file.
|
HTTPClientResponse |
Post(java.lang.String file,
byte[] data)
POSTs the raw data to the specified file.
|
HTTPClientResponse |
Post(java.lang.String file,
byte[] data,
NVPair[] headers)
POSTs the raw data to the specified file using the specified headers.
|
HTTPClientResponse |
Post(java.lang.String file,
HttpOutputStream stream)
POSTs the data written to the output stream to the specified file.
|
HTTPClientResponse |
Post(java.lang.String file,
HttpOutputStream stream,
NVPair[] headers)
POSTs the data written to the output stream to the specified file
using the specified headers.
|
HTTPClientResponse |
Post(java.lang.String file,
NVPair[] form_data)
POSTs form-data to the specified file.
|
HTTPClientResponse |
Post(java.lang.String file,
NVPair[] queryParams,
byte[] data,
NVPair[] headers) |
HTTPClientResponse |
Post(java.lang.String file,
NVPair[] form_data,
NVPair[] headers)
POST's form-data to the specified file using the specified headers.
|
HTTPClientResponse |
Post(java.lang.String file,
NVPair[] queryParams,
NVPair[] formParams,
NVPair[] headers) |
HTTPClientResponse |
Post(java.lang.String file,
java.lang.String data)
POSTs the data to the specified file.
|
HTTPClientResponse |
Post(java.lang.String file,
java.lang.String data,
NVPair[] headers)
POSTs the data to the specified file using the specified headers.
|
HTTPClientResponse |
Put(java.lang.String file,
byte[] data)
PUTs the raw data into the specified file.
|
HTTPClientResponse |
Put(java.lang.String file,
byte[] data,
NVPair[] headers)
PUTs the raw data into the specified file using the additional
headers.
|
HTTPClientResponse |
Put(java.lang.String file,
HttpOutputStream stream)
PUTs the data written to the output stream into the specified file.
|
HTTPClientResponse |
Put(java.lang.String file,
HttpOutputStream stream,
NVPair[] headers)
PUTs the data written to the output stream into the specified file
using the additional headers.
|
HTTPClientResponse |
Put(java.lang.String file,
java.lang.String data)
PUTs the data into the specified file.
|
HTTPClientResponse |
Put(java.lang.String file,
java.lang.String data,
NVPair[] headers)
PUTs the data into the specified file using the additional headers
for the request.
|
void |
removeBasicAuthorization(java.lang.String realm) |
static boolean |
removeDefaultModule(java.lang.Class module)
Removes a module from the default list.
|
void |
removeDigestAuthorization(java.lang.String realm) |
boolean |
removeModule(java.lang.Class module)
Removes a module from the current list.
|
void |
setAllowUserInteraction(boolean allow)
Controls whether modules are allowed to prompt the user or pop up
dialogs if neccessary.
|
void |
setContext(java.lang.Object context)
Sets the current context.
|
void |
setCurrentProxy(java.lang.String host,
int port)
Sets the proxy used by this instance.
|
static void |
setDefaultAllowUserInteraction(boolean allow)
Sets the default allow-user-action.
|
void |
setDefaultHeaders(NVPair[] headers)
Sets the default http headers to be sent with each request.
|
static void |
setDefaultTimeout(int time)
Sets the default timeout value to be used for each new HTTPClient.
|
static void |
setProxyServer(java.lang.String host,
int port)
Sets the default proxy server to use.
|
void |
setRawMode(boolean raw)
Deprecated.
This is not really needed anymore; in V0.2 request were
synchronous and therefore to do pipelining you needed
to disable the processing of responses.
|
static void |
setSocksServer(java.lang.String host)
Sets the SOCKS server to use.
|
static void |
setSocksServer(java.lang.String host,
int port)
Sets the SOCKS server to use.
|
static void |
setSocksServer(java.lang.String host,
int port,
int version)
Sets the SOCKS server to use.
|
void |
setSSLContextFactory(SSLContextFactory sslContextFactory)
Sets SSLContext factory to be used to create SSLContext and SSLSocketFactory.
|
void |
setTimeout(long time)
Sets the timeout to be used for creating connections and reading
responses.
|
void |
stop()
Aborts all the requests currently in progress on this connection and
closes all associated sockets.
|
java.lang.String |
toString()
Generates a string of the form protocol://host.domain:port .
|
HTTPClientResponse |
Trace(java.lang.String file)
Requests a TRACE.
|
HTTPClientResponse |
Trace(java.lang.String file,
NVPair[] headers)
Requests a TRACE.
|
public static final java.lang.String version
public static final int HTTP
public static final int HTTPS
public static final int SHTTP
public static final int HTTP_NG
public static final int HTTP_1_0
public static final int HTTP_1_1
public static final int CD_NONE
public static final int CD_HDRS
public static final int CD_0
public static final int CD_CLOSE
public static final int CD_CONTLEN
public static final int CD_CHUNKED
public static final int CD_MP_BR
public HTTPClient(java.applet.Applet applet) throws ProtocolNotSuppException
applet
- the current appletProtocolNotSuppException
public HTTPClient(java.lang.String host)
host
- the hostpublic HTTPClient(java.lang.String host, int port)
host
- the hostport
- the portpublic HTTPClient(java.lang.String prot, java.lang.String host, int port) throws ProtocolNotSuppException
prot
- the protocolhost
- the hostport
- the port, or -1 for the default portProtocolNotSuppException
- if the protocol is not HTTPpublic HTTPClient(java.lang.String prot, java.lang.String host, int port, java.net.InetAddress localAddr, int localPort) throws ProtocolNotSuppException
prot
- the protocolhost
- the hostport
- the port, or -1 for the default portlocalAddr
- the local address to bind tolocalPort
- the local port to bind toProtocolNotSuppException
- if the protocol is not HTTPpublic HTTPClient(java.net.URL url) throws ProtocolNotSuppException
url
- the urlProtocolNotSuppException
- if the protocol is not HTTPpublic HTTPClient(URI uri) throws ProtocolNotSuppException
uri
- the uriProtocolNotSuppException
- if the protocol is not HTTPpublic void setSSLContextFactory(SSLContextFactory sslContextFactory) throws java.security.KeyManagementException, java.security.NoSuchAlgorithmException
sslContextFactory
- java.security.KeyManagementException
java.security.NoSuchAlgorithmException
public HTTPClientResponse Head(java.lang.String file) throws java.io.IOException, ModuleException
file
- the absolute path of the filejava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.Get(java.lang.String)
public HTTPClientResponse Head(java.lang.String file, NVPair[] form_data) throws java.io.IOException, ModuleException
file
- the absolute path of the fileform_data
- an array of Name/Value pairsjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.Get(java.lang.String, com.streamscape.lib.http.NVPair[])
public HTTPClientResponse Head(java.lang.String file, NVPair[] form_data, NVPair[] headers) throws java.io.IOException, ModuleException
file
- the absolute path of the fileform_data
- an array of Name/Value pairsheaders
- additional headersjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.Get(java.lang.String, com.streamscape.lib.http.NVPair[], com.streamscape.lib.http.NVPair[])
public HTTPClientResponse Head(java.lang.String file, java.lang.String query) throws java.io.IOException, ModuleException
file
- the absolute path of the filequery
- the query string; it will be urlencodedjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.Get(java.lang.String, java.lang.String)
public HTTPClientResponse Head(java.lang.String file, java.lang.String query, NVPair[] headers) throws java.io.IOException, ModuleException
file
- the absolute path of the filequery
- the query string; it will be urlencodedheaders
- additional headersjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.Get(java.lang.String, java.lang.String, com.streamscape.lib.http.NVPair[])
public HTTPClientResponse Get(java.lang.String file) throws java.io.IOException, ModuleException
file
- the absolute path of the filejava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Get(java.lang.String file, NVPair[] form_data) throws java.io.IOException, ModuleException
file
- the absolute path of the fileform_data
- an array of Name/Value pairsjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Get(java.lang.String file, NVPair[] form_data, NVPair[] headers) throws java.io.IOException, ModuleException
file
- the absolute path of the fileform_data
- an array of Name/Value pairsheaders
- additional headersjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Get(java.lang.String file, java.lang.String query) throws java.io.IOException, ModuleException
file
- the absolute path of the filequery
- the queryjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Get(java.lang.String file, java.lang.String query, NVPair[] headers) throws java.io.IOException, ModuleException
file
- the absolute path of the filequery
- the query stringheaders
- additional headersjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Post(java.lang.String file) throws java.io.IOException, ModuleException
file
- the absolute path of the filejava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Post(java.lang.String file, NVPair[] form_data) throws java.io.IOException, ModuleException
file
- the absolute path of the fileform_data
- an array of Name/Value pairsjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Post(java.lang.String file, NVPair[] form_data, NVPair[] headers) throws java.io.IOException, ModuleException
file
- the absolute path of the fileform_data
- an array of Name/Value pairsheaders
- additional headersjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Post(java.lang.String file, NVPair[] queryParams, byte[] data, NVPair[] headers) throws java.io.IOException, ModuleException
java.io.IOException
ModuleException
public HTTPClientResponse Post(java.lang.String file, NVPair[] queryParams, NVPair[] formParams, NVPair[] headers) throws java.io.IOException, ModuleException
java.io.IOException
ModuleException
public HTTPClientResponse Post(java.lang.String file, java.lang.String data) throws java.io.IOException, ModuleException
file
- the absolute path of the filedata
- the datajava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.String.getBytes()
public HTTPClientResponse Post(java.lang.String file, java.lang.String data, NVPair[] headers) throws java.io.IOException, ModuleException
file
- the absolute path of the filedata
- the dataheaders
- additional headersjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.String.getBytes()
public HTTPClientResponse Post(java.lang.String file, byte[] data) throws java.io.IOException, ModuleException
file
- the absolute path of the filedata
- the datajava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Post(java.lang.String file, byte[] data, NVPair[] headers) throws java.io.IOException, ModuleException
file
- the absolute path of the filedata
- the dataheaders
- additional headersjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Post(java.lang.String file, HttpOutputStream stream) throws java.io.IOException, ModuleException
file
- the absolute path of the filestream
- the output stream on which the data is writtenjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Post(java.lang.String file, HttpOutputStream stream, NVPair[] headers) throws java.io.IOException, ModuleException
file
- the absolute path of the filestream
- the output stream on which the data is writtenheaders
- additional headersjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Put(java.lang.String file, java.lang.String data) throws java.io.IOException, ModuleException
file
- the absolute path of the filedata
- the datajava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.String.getBytes()
public HTTPClientResponse Put(java.lang.String file, java.lang.String data, NVPair[] headers) throws java.io.IOException, ModuleException
file
- the absolute path of the filedata
- the dataheaders
- additional headersjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.String.getBytes()
public HTTPClientResponse Put(java.lang.String file, byte[] data) throws java.io.IOException, ModuleException
file
- the absolute path of the filedata
- the datajava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Put(java.lang.String file, byte[] data, NVPair[] headers) throws java.io.IOException, ModuleException
file
- the absolute path of the filedata
- the dataheaders
- any additional headersjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Put(java.lang.String file, HttpOutputStream stream) throws java.io.IOException, ModuleException
file
- the absolute path of the filestream
- the output stream on which the data is writtenjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Put(java.lang.String file, HttpOutputStream stream, NVPair[] headers) throws java.io.IOException, ModuleException
file
- the absolute path of the filestream
- the output stream on which the data is writtenheaders
- any additional headersjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Patch(java.lang.String file, java.lang.String data) throws java.io.IOException, ModuleException
file
- the absolute path of the filedata
- the datajava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.String.getBytes()
public HTTPClientResponse Patch(java.lang.String file, java.lang.String data, NVPair[] headers) throws java.io.IOException, ModuleException
file
- the absolute path of the filedata
- the dataheaders
- additional headersjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.String.getBytes()
public HTTPClientResponse Patch(java.lang.String file, byte[] data) throws java.io.IOException, ModuleException
file
- the absolute path of the filedata
- the datajava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Patch(java.lang.String file, byte[] data, NVPair[] headers) throws java.io.IOException, ModuleException
file
- the absolute path of the filedata
- the dataheaders
- any additional headersjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Patch(java.lang.String file, HttpOutputStream stream) throws java.io.IOException, ModuleException
file
- the absolute path of the filestream
- the output stream on which the data is writtenjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Patch(java.lang.String file, HttpOutputStream stream, NVPair[] headers) throws java.io.IOException, ModuleException
file
- the absolute path of the filestream
- the output stream on which the data is writtenheaders
- any additional headersjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Options(java.lang.String file) throws java.io.IOException, ModuleException
file
- the absolute path of the resource, or "*"java.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Options(java.lang.String file, NVPair[] headers) throws java.io.IOException, ModuleException
file
- the absolute path of the resource, or "*"headers
- the headers containing optional info.java.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Options(java.lang.String file, NVPair[] headers, byte[] data) throws java.io.IOException, ModuleException
file
- the absolute path of the resource, or "*"headers
- the headers containing optional info.data
- any data to be sent in the optional bodyjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Options(java.lang.String file, NVPair[] headers, HttpOutputStream stream) throws java.io.IOException, ModuleException
file
- the absolute path of the resource, or "*"headers
- the headers containing optional info.stream
- an output stream for sending the optional bodyjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Delete(java.lang.String file) throws java.io.IOException, ModuleException
file
- the absolute path of the resourcejava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Delete(java.lang.String file, NVPair[] headers) throws java.io.IOException, ModuleException
file
- the absolute path of the resourceheaders
- additional headersjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Trace(java.lang.String file, NVPair[] headers) throws java.io.IOException, ModuleException
file
- the absolute path of the resourceheaders
- additional headersjava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse Trace(java.lang.String file) throws java.io.IOException, ModuleException
file
- the absolute path of the resourcejava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse ExtensionMethod(java.lang.String method, java.lang.String file, byte[] data, NVPair[] headers) throws java.io.IOException, ModuleException
method
- the extension methodfile
- the absolute path of the resource, or nulldata
- optional data, or nullheaders
- optional headers, or nulljava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public HTTPClientResponse ExtensionMethod(java.lang.String method, java.lang.String file, HttpOutputStream os, NVPair[] headers) throws java.io.IOException, ModuleException
method
- the extension methodfile
- the absolute path of the resource, or nullos
- optional output stream, or nullheaders
- optional headers, or nulljava.io.IOException
- when an exception is returned from
the socket.ModuleException
- if an exception is encountered in any module.public void stop()
Note: there is a small window where a request method such as
Get()
may have been invoked but the request has not
been built and added to the list. Any request in this window will
not be aborted.
public void destory()
public void clearAuthentications()
public void setDefaultHeaders(NVPair[] headers)
Typical headers you might want to set here are "Accept" and its "Accept-*" relatives, "Connection", "From", "User-Agent", etc.
headers
- an array of header-name/value pairs (do not give the
separating ':').public NVPair[] getDefaultHeaders()
public java.lang.String getProtocol()
public java.lang.String getHost()
public int getPort()
public java.lang.String getProxyHost()
public int getProxyPort()
public boolean isCompatibleWith(URI uri)
uri
- the URI to checkpublic void setRawMode(boolean raw)
The default is false.
raw
- if true removes all modules (except for the retry module)removeModule(java.lang.Class)
public static void setDefaultTimeout(int time)
time
- the timeout in milliseconds.#setTimeout(int)
public static int getDefaultTimeout()
#setTimeout(int)
public void setTimeout(long time)
resp.getInputStream().close()
should be
invoked.
When creating new sockets the timeout will limit the time spent doing the host name translation and establishing the connection with the server.
The timeout also influences the reading of the response headers. However, it does not specify a how long, for example, getStatusCode() may take, as might be assumed. Instead it specifies how long a read on the socket may take. If the response dribbles in slowly with packets arriving quicker than the timeout then the method will complete normally. I.e. the exception is only thrown if nothing arrives on the socket for the specified time. Furthermore, the timeout only influences the reading of the headers, not the reading of the body.
Read Timeouts are associated with responses, so that you may change this value before each request and it won't affect the reading of responses to previous requests.
time
- the time in milliseconds. A time of 0 means wait
indefinitely.stop()
public long getTimeout()
#setTimeout(int)
public void setAllowUserInteraction(boolean allow)
allow
- if true allows modules to interact with user.public boolean getAllowUserInteraction()
public static void setDefaultAllowUserInteraction(boolean allow)
allow
- if true allows modules to interact with user.public static boolean getDefaultAllowUserInteraction()
public static java.lang.Class[] getDefaultModules()
public static boolean addDefaultModule(java.lang.Class module, int pos)
Example:
HTTPClient.addDefaultModule(Class.forName("com.streamscape.lib.http.CookieModule"), 1);adds the cookie module as the second module in the list.
The default list is created at class initialization time from the property com.streamscape.lib.http.Modules. This must contain a "|" separated list of classes in the order they're to be invoked. If this property is not set it defaults to: "com.streamscape.lib.http.RetryModule | com.streamscape.lib.http.CookieModule | com.streamscape.lib.http.RedirectionModule | com.streamscape.lib.http.AuthorizationModule | com.streamscape.lib.http.DefaultModule | com.streamscape.lib.http.TransferEncodingModule | com.streamscape.lib.http.ContentMD5Module | com.streamscape.lib.http.ContentEncodingModule"
module
- the module's Class objectpos
- the position of this module in the list; if pos
>= 0 then this is the absolute position in the list (0 is
the first position); if pos < 0 then this is
the position relative to the end of the list (-1 means
the last element, -2 the second to last element, etc).java.lang.ArrayIndexOutOfBoundsException
- if pos >
list-size or if pos < -(list-size).java.lang.ClassCastException
- if module does not
implement the HTTPClientModule interface.java.lang.RuntimeException
- if module cannot be
instantiated.HTTPClientModule
public static boolean removeDefaultModule(java.lang.Class module)
module
- the module's Class objectpublic java.lang.Class[] getModules()
public boolean addModule(java.lang.Class module, int pos)
module
- the module's Class objectpos
- the position of this module in the list; if pos
>= 0 then this is the absolute position in the list (0 is
the first position); if pos < 0 then this is
the position relative to the end of the list (-1 means
the last element, -2 the second to last element, etc).java.lang.ArrayIndexOutOfBoundsException
- if pos >
list-size or if pos < -(list-size).java.lang.ClassCastException
- if module does not
implement the HTTPClientModule interface.java.lang.RuntimeException
- if module cannot be
instantiated.HTTPClientModule
public boolean removeModule(java.lang.Class module)
module
- the module's Class objectpublic void setContext(java.lang.Object context)
The context may be any object. Contexts are considered equal
if equals()
returns true. Examples of useful context
objects are threads (e.g. if you are running multiple clients, one
per thread) and sockets (e.g. if you are implementing a gateway).
When a new HTTPClient is created it is initialized with a default context which is the same for all instances. This method must be invoked immediately after a new HTTPClient is created and before any request method is invoked. Furthermore, this method may only be called once (i.e. the context is "sticky").
context
- the new context; must be non-nulljava.lang.IllegalArgumentException
- if context is nulljava.lang.IllegalStateException
- if the context has already been setpublic java.lang.Object getContext()
setContext()
hasn't been invokedsetContext(java.lang.Object)
public static java.lang.Object getDefaultContext()
setContext(java.lang.Object)
public void enableOAuthAuthentication()
public void disableOAuthAuthentication()
public boolean isOAuthEnabled()
public void addDigestAuthorization(java.lang.String realm, java.lang.String user, java.lang.String passwd)
This is a convenience method and just invokes the corresponding method in AuthorizationInfo.
realm
- the realmuser
- the usernamepasswd
- the passwordAuthorizationInfo.addDigestAuthorization(java.lang.String, int, java.lang.String, java.lang.String, java.lang.String)
public void addBasicAuthorization(java.lang.String realm, java.lang.String user, java.lang.String passwd)
This is a convenience method and just invokes the corresponding method in AuthorizationInfo.
realm
- the realmuser
- the usernamepasswd
- the passwordAuthorizationInfo.addBasicAuthorization(java.lang.String, int, java.lang.String, java.lang.String, java.lang.String)
public void addBasicAuthorizationDefault(java.lang.String user, java.lang.String passwd)
public void removeDigestAuthorization(java.lang.String realm)
public void removeBasicAuthorization(java.lang.String realm)
public static void setProxyServer(java.lang.String host, int port)
In an application or using the Appletviewer an alternative to this method is to set the following properties (either in the properties file or on the command line): http.proxyHost and http.proxyPort. Whether http.proxyHost is set or not determines whether a proxy server is used.
If the proxy server requires authorization and you wish to set this authorization information in the code, then you may use any of the AuthorizationInfo.addXXXAuthorization() methods to do so. Specify the same host and port as in this method. If you have not given any authorization info and the proxy server requires authorization then you will be prompted for the necessary info via a popup the first time you do a request.
host
- the host on which the proxy server resides.port
- the port the proxy server is listening on.setCurrentProxy(java.lang.String, int)
public void setCurrentProxy(java.lang.String host, int port)
Note that if you set a proxy for the connection using this method, and a request made over this connection is redirected to a different server, then the connection used for new server will not pick this proxy setting, but instead will use the default proxy settings.
host
- the host the proxy runs onport
- the port the proxy is listening onsetProxyServer(java.lang.String, int)
public static void dontProxyFor(java.lang.String host) throws ParseException
setProxyServer()
.
The host may be any of:
The two properties com.streamscape.lib.http.nonProxyHosts and http.nonProxyHosts are used when this class is loaded to initialize the list of non-proxy hosts. The second property is only read if the first one is not set; the second property is also used the JDK's URLConnection. These properties must contain a "|" separated list of entries which conform to the above rules for the host parameter (e.g. "11.22.33.44|.disney.com").
host
- a host name, domain name, IP-address or IP-subnet.ParseException
- if the length of the netmask does not match
the length of the IP-addresspublic static void dontProxyFor(java.lang.String[] hosts)
hosts
- The list of hosts to setdontProxyFor(java.lang.String)
public static boolean doProxyFor(java.lang.String host) throws ParseException
dontProxyFor()
uses, i.e. this is used to undo a
dontProxyFor()
setting. The syntax for host is
specified in dontProxyFor()
.host
- a host name, domain name, IP-address or IP-subnet.ParseException
- if the length of the netmask does not match
the length of the IP-addressdontProxyFor(java.lang.String)
public static void setSocksServer(java.lang.String host)
The code will try to determine the SOCKS version to use at connection time. This might fail for a number of reasons, however, in which case you must specify the version explicitly.
host
- the host on which the proxy server resides. The port
used is the default port 1080.setSocksServer(java.lang.String, int, int)
public static void setSocksServer(java.lang.String host, int port)
The code will try to determine the SOCKS version to use at connection time. This might fail for a number of reasons, however, in which case you must specify the version explicitly.
host
- the host on which the proxy server resides.port
- the port the proxy server is listening on.setSocksServer(java.lang.String, int, int)
public static void setSocksServer(java.lang.String host, int port, int version) throws SocksException
In an application or using the Appletviewer an alternative to this method is to set the following properties (either in the properties file or on the command line): com.streamscape.lib.http.socksHost, com.streamscape.lib.http.socksPort and com.streamscape.lib.http.socksVersion. Whether com.streamscape.lib.http.socksHost is set or not determines whether a SOCKS server is used; if com.streamscape.lib.http.socksPort is not set it defaults to 1080; if com.streamscape.lib.http.socksVersion is not set an attempt will be made to automatically determine the version used by the server.
Note: If you have also set a proxy server then a connection will be made to the SOCKS server, which in turn then makes a connection to the proxy server (possibly via other SOCKS servers), which in turn makes the final connection.
If the proxy server is running SOCKS version 5 and requires username/password authorization, and you wish to set this authorization information in the code, then you may use the AuthorizationInfo.addAuthorization() method to do so. Specify the same host and port as in this method, give the scheme "SOCKS5" and the realm "USER/PASS", set the cookie to null and the params to an array containing a single NVPair in turn containing the username and password. Example:
NVPair[] up = { new NVPair(username, password) }; AuthorizationInfo.addAuthorization(host, port, "SOCKS5", "USER/PASS", null, up);If you have not given any authorization info and the proxy server requires authorization then you will be prompted for the necessary info via a popup the first time you do a request.
host
- the host on which the proxy server resides.port
- the port the proxy server is listening on.version
- the SOCKS version the server is running. Currently
this must be '4' or '5'.SocksException
- If version is not '4' or '5'.public java.lang.String toString()
toString
in class java.lang.Object
Copyright © 2015-2024 StreamScape Technologies. All rights reserved.