Oracle Application Server HTTPClient API Reference
10g Release 2 (10.1.2)

B14020-02


HTTPClient
Class HTTPConnection

java.lang.Object
  extended byHTTPClient.HTTPConnection

All Implemented Interfaces:
HTTPClient.GlobalConstants, HTTPClientModuleConstants

public class HTTPConnection
extends java.lang.Object
implements HTTPClient.GlobalConstants, HTTPClientModuleConstants

This class implements http protocol requests; it contains most of HTTP/1.1 and ought to be unconditionally compliant. Redirections are automatically handled, and authorizations requests are recognized and dealt with via an authorization handler. Only full HTTP/1.0 and HTTP/1.1 requests are generated. HTTP/1.1, HTTP/1.0 and HTTP/0.9 responses are recognized.

Using the HTTPClient should be quite simple. First add the import statement 'import HTTPClient.*;' to your file(s). Request can then be sent using one of the methods Head(), Get(), Post(), etc in HTTPConnection. These methods all return an instance of HTTPResponse which has methods for accessing the response headers (getHeader(), getHeaderAsInt(), etc), various response info (getStatusCode(), getReasonLine(), etc) and the reponse data (getData(), getText(), and getInputStream()). Following are some examples.

If this is in an applet you can retrieve files from your server as follows:

     try
     {
         HTTPConnection con = new HTTPConnection(this);
         HTTPResponse   rsp = con.Get("/my_file");
         if (rsp.getStatusCode() >= 300)
         {
             System.err.println("Received Error: "+rsp.getReasonLine());
             System.err.println(rsp.getText());
         }
         else
             data = rsp.getData();

         rsp = con.Get("/another_file");
         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 handling request: " + me.getMessage());
     }
 

This will get the files "/my_file" and "/another_file" and put their contents into byte[]'s accessible via getData(). Note that you need to only create a new HTTPConnection when sending a request to a new server (different host or port); although you may create a new HTTPConnection for every request to the same server this not recommended, as various information about the server is cached after the first request (to optimize subsequent requests) and persistent connections are used whenever possible.

To POST form data you would use something like this (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);

         HTTPConnection con = new HTTPConnection(this);
         HTTPResponse   rsp = con.Post("/cgi-bin/my_script", 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 handling request: " + me.getMessage());
     }
 

Here the response data is read at leasure via an InputStream instead of all at once into a byte[].

As another example, if you have a URL you're trying to send a request to you would do something like the following:

     try
     {
         URL url = new URL("http://www.mydomain.us/test/my_file");
         HTTPConnection con = new HTTPConnection(url);
         HTTPResponse   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());
     }
 

There are a whole number of methods for each request type; however the general forms are ([...] means that the enclosed is optional):


Field Summary
static int CD_0
          Content delimiter- no body
static int CD_CHUNKED
          Content delimiter - via chunked transfer encoding
static int CD_CLOSE
          Content delimiter - by closing connection
static int CD_CONTLEN
          Content delimiter - via the Content-Length header
static int CD_HDRS
          Content delimiter - reading headers/trailers
static int CD_MP_BR
          Content delimiter - via multipart/byteranges
static int CD_NONE
          Content delimiter - raw read from the stream
static int HTTP
          plain http
static int HTTP_1_0
          HTTP 1.0
static int HTTP_1_1
          HTTP 1.1
static int HTTP_NG
          http next-generation
static int HTTPS
          http on top of SSL
static int SHTTP
          secure http
static java.lang.String version
          The current version of this package.

 

Fields inherited from interface HTTPClient.HTTPClientModuleConstants
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 Summary
HTTPConnection(java.applet.Applet applet)
          Constructs a connection to the host from where the applet was loaded.
HTTPConnection(java.lang.String host)
          Constructs a connection to the specified host on port 80
HTTPConnection(java.lang.String host, int port)
          Constructs a connection to the specified host on the specified port
HTTPConnection(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).
HTTPConnection(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.
HTTPConnection(URI uri)
          Constructs a connection to the host (port) as given in the uri.
HTTPConnection(java.net.URL url)
          Constructs a connection to the host (port) as given in the url.
HTTPConnection(java.net.URL url, boolean oracleSSL)
          Deprecated. This constructor has been deprecated. Please use the HTTPConnection(URL) constructor. OracleSSL is the default SSL provider for HTTPS connections. To change this default, use HTTPConnection.setSSLSocketFactory.

 

Method Summary
 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.
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 addTrustPoint(byte[] certificate)
          Adds the specified trust point to the list of trusted certificates for this connection's OracleSSLCredential.
 void addTrustPoint(java.security.cert.Certificate certificate)
          Adds the specified trust point to the list of trusted certificates for this connection's OracleSSLCredential.
 void addTrustPoint(java.lang.String certificate)
          Adds the specified trust point to the list of trusted certificates for this connection's OracleSSLCredential.
 void connect()
          Connects the socket to the server, but does not send any data.
 HTTPResponse Delete(java.lang.String file)
          Requests that file be DELETEd from the server.
 HTTPResponse Delete(java.lang.String file, NVPair[] headers)
          Requests that file be DELETEd from the server.
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.
 HTTPResponse 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.
 HTTPResponse 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.
protected  void finalize()
          Added as a safety net, in-case HTTPConnection object is not referenced any more, make sure to release the sockets held by this.
 HTTPResponse Get(java.lang.String file)
          GETs the file.
 HTTPResponse Get(java.lang.String file, NVPair[] form_data)
          GETs the file with a query consisting of the specified form-data.
 HTTPResponse Get(java.lang.String file, NVPair[] form_data, NVPair[] headers)
          GETs the file with a query consisting of the specified form-data.
 HTTPResponse Get(java.lang.String file, java.lang.String query)
          GETs the file using the specified query string.
 HTTPResponse 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.
 int getConnectionTimeout()
          Gets the read timeout used for reading response data.
 java.lang.Object getContext()
          Returns the current context.
static boolean getDefaultAllowUserInteraction()
          Gets the default allow-user-action.
static int getDefaultConnectionTimeout()
          Gets the default read timeout value to be used for each new HTTPConnection.
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 read timeout value to be used for each new HTTPConnection.
 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.
 java.lang.String getQueryEncoding()
          The current Java encoding used for the queries part of a URL Initially null.
 OracleSSLCredential getSSLCredential()
          Returns the SSLCredential used by this socket
 java.lang.String[] getSSLEnabledCipherSuites()
          Returns the list of cipher suites that are enabled for the https connection
 javax.net.ssl.SSLSession getSSLSession()
          Returns the ssl session information for the ssl connection or null if no ssl connection has been established
 java.net.Socket getSSLSocket(java.net.Socket proxy)
          Obtains an new SSL Socket to be used for HTTPS connections If setSSLCredentials() or setSSLCipherSuites() were not called prior to this call, the property settings are checked
 javax.net.ssl.SSLSocketFactory getSSLSocketFactory()
          Returns the socket factory used to create SSL sockets
 int getTimeout()
          Gets the read timeout used for reading response data.
 HTTPResponse Head(java.lang.String file)
          Sends the HEAD request.
 HTTPResponse Head(java.lang.String file, NVPair[] form_data)
          Sends the HEAD request.
 HTTPResponse Head(java.lang.String file, NVPair[] form_data, NVPair[] headers)
          Sends the HEAD request.
 HTTPResponse Head(java.lang.String file, java.lang.String query)
          Sends the HEAD request.
 HTTPResponse 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.
 HTTPResponse Options(java.lang.String file)
          Request OPTIONS from the server.
 HTTPResponse Options(java.lang.String file, NVPair[] headers)
          Request OPTIONS from the server.
 HTTPResponse Options(java.lang.String file, NVPair[] headers, byte[] data)
          Request OPTIONS from the server.
 HTTPResponse Options(java.lang.String file, NVPair[] headers, HttpOutputStream stream)
          Request OPTIONS from the server.
 HTTPResponse Post(java.lang.String file)
          POSTs to the specified file.
 HTTPResponse Post(java.lang.String file, byte[] data)
          POSTs the raw data to the specified file.
 HTTPResponse Post(java.lang.String file, byte[] data, NVPair[] headers)
          POSTs the raw data to the specified file using the specified headers.
 HTTPResponse Post(java.lang.String file, HttpOutputStream stream)
          POSTs the data written to the output stream to the specified file.
 HTTPResponse 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.
 HTTPResponse Post(java.lang.String file, NVPair[] form_data)
          POSTs form-data to the specified file.
 HTTPResponse Post(java.lang.String file, NVPair[] form_data, NVPair[] headers)
          POST's form-data to the specified file using the specified headers.
 HTTPResponse Post(java.lang.String file, java.lang.String data)
          POSTs the data to the specified file.
 HTTPResponse Post(java.lang.String file, java.lang.String data, NVPair[] headers)
          POSTs the data to the specified file using the specified headers.
 HTTPResponse Put(java.lang.String file, byte[] data)
          PUTs the raw data into the specified file.
 HTTPResponse Put(java.lang.String file, byte[] data, NVPair[] headers)
          PUTs the raw data into the specified file using the additional headers.
 HTTPResponse Put(java.lang.String file, HttpOutputStream stream)
          PUTs the data written to the output stream into the specified file.
 HTTPResponse 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.
 HTTPResponse Put(java.lang.String file, java.lang.String data)
          PUTs the data into the specified file.
 HTTPResponse 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.
static boolean removeDefaultModule(java.lang.Class module)
          Removes a module from the default list.
 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 setConnectionTimeout(int time)
          Sets the connection timeout values for this HTTPConnection.
 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.
static void setDefaultConnectionTimeout(int time)
          Sets the default connection timeout values to be used for each new HTTPConnection.
 void setDefaultHeaders(NVPair[] headers)
          Sets the default http headers to be sent with each request.
static void setDefaultSSLSocketFactory(javax.net.ssl.SSLSocketFactory factory)
          Set the default SSLSocketFactory for use by HTTPClient.
static void setDefaultTimeout(int time)
          Sets the default read and connection timeout values to be used for each new HTTPConnection.
static void setProxyServer(java.lang.String host, int port)
          Sets the default proxy server to use.
 void setQueryEncoding(java.lang.String encoding)
          Sets the Java encoding to be used for the query parts of URL's in requests If it is null the System default encoding is used.
 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 setSSLCredential(OracleSSLCredential cred)
          Sets the OracleSSLCredential object for use in SSL connections
 void setSSLEnabledCipherSuites(java.lang.String[] cipherSuites)
          Controls which particular cipher suites are enabled for use on this connection.
 void setSSLSocketFactory(javax.net.ssl.SSLSocketFactory factory)
          Sets the SSLSocketFactory that will be used for https connections
 void setTimeout(int time)
          Sets the timeouts to be used for creating connections and reading responses.
protected  HTTPResponse setupRequest(java.lang.String method, java.lang.String resource, NVPair[] headers, byte[] entity, HttpOutputStream stream)
          Sets up the request, creating the list of headers to send and creating instances of the modules.
 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 .
 HTTPResponse Trace(java.lang.String file)
          Requests a TRACE.
 HTTPResponse Trace(java.lang.String file, NVPair[] headers)
          Requests a TRACE.

 

Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait

 

Field Detail

version

public static final java.lang.String version
The current version of this package.
See Also:
Constant Field Values

HTTP

public static final int HTTP
plain http
See Also:
Constant Field Values

HTTPS

public static final int HTTPS
http on top of SSL
See Also:
Constant Field Values

SHTTP

public static final int SHTTP
secure http
See Also:
Constant Field Values

HTTP_NG

public static final int HTTP_NG
http next-generation
See Also:
Constant Field Values

HTTP_1_0

public static final int HTTP_1_0
HTTP 1.0
See Also:
Constant Field Values

HTTP_1_1

public static final int HTTP_1_1
HTTP 1.1
See Also:
Constant Field Values

CD_NONE

public static final int CD_NONE
Content delimiter - raw read from the stream
See Also:
Constant Field Values

CD_HDRS

public static final int CD_HDRS
Content delimiter - reading headers/trailers
See Also:
Constant Field Values

CD_0

public static final int CD_0
Content delimiter- no body
See Also:
Constant Field Values

CD_CLOSE

public static final int CD_CLOSE
Content delimiter - by closing connection
See Also:
Constant Field Values

CD_CONTLEN

public static final int CD_CONTLEN
Content delimiter - via the Content-Length header
See Also:
Constant Field Values

CD_CHUNKED

public static final int CD_CHUNKED
Content delimiter - via chunked transfer encoding
See Also:
Constant Field Values

CD_MP_BR

public static final int CD_MP_BR
Content delimiter - via multipart/byteranges
See Also:
Constant Field Values

Constructor Detail

HTTPConnection

public HTTPConnection(java.applet.Applet applet)
               throws ProtocolNotSuppException
Constructs a connection to the host from where the applet was loaded. Note that current security policies only let applets connect home.
Parameters:
applet - the current applet

HTTPConnection

public HTTPConnection(java.lang.String host)
Constructs a connection to the specified host on port 80
Parameters:
host - the host

HTTPConnection

public HTTPConnection(java.lang.String host,
                      int port)
Constructs a connection to the specified host on the specified port
Parameters:
host - the host
port - the port

HTTPConnection

public HTTPConnection(java.lang.String prot,
                      java.lang.String host,
                      int port)
               throws ProtocolNotSuppException
Constructs a connection to the specified host on the specified port, using the specified protocol (currently only "http" is supported).
Parameters:
prot - the protocol
host - the host
port - the port, or -1 for the default port
Throws:
ProtocolNotSuppException - if the protocol is not HTTP

HTTPConnection

public HTTPConnection(java.lang.String prot,
                      java.lang.String host,
                      int port,
                      java.net.InetAddress localAddr,
                      int localPort)
               throws ProtocolNotSuppException
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.
Parameters:
prot - the protocol
host - the host
port - the port, or -1 for the default port
localAddr - the local address to bind to
localPort - the local port to bind to
Throws:
ProtocolNotSuppException - if the protocol is not HTTP

HTTPConnection

public HTTPConnection(java.net.URL url)
               throws ProtocolNotSuppException
Constructs a connection to the host (port) as given in the url.
Parameters:
url - the url
Throws:
ProtocolNotSuppException - if the protocol is not HTTP

HTTPConnection

public HTTPConnection(java.net.URL url,
                      boolean oracleSSL)
               throws ProtocolNotSuppException
Deprecated. This constructor has been deprecated. Please use the HTTPConnection(URL) constructor. OracleSSL is the default SSL provider for HTTPS connections. To change this default, use HTTPConnection.setSSLSocketFactory.
Constructs a connection to the host (port) as given in the url.
Parameters:
url - the url
Throws:
ProtocolNotSuppException - if the protocol is not HTTP
See Also:
HTTPConnection(URL), setSSLSocketFactory(javax.net.ssl.SSLSocketFactory)

HTTPConnection

public HTTPConnection(URI uri)
               throws ProtocolNotSuppException
Constructs a connection to the host (port) as given in the uri.
Parameters:
uri - the uri
Throws:
ProtocolNotSuppException - if the protocol is not HTTP

Method Detail

Head

public HTTPResponse Head(java.lang.String file)
                  throws java.io.IOException,
                         ModuleException
Sends the HEAD request. This request is just like the corresponding GET except that it only returns the headers and no data.
Parameters:
file - the absolute path of the file
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.
See Also:
Get(java.lang.String)

Head

public HTTPResponse Head(java.lang.String file,
                         NVPair[] form_data)
                  throws java.io.IOException,
                         ModuleException
Sends the HEAD request. This request is just like the corresponding GET except that it only returns the headers and no data.
Parameters:
file - the absolute path of the file
form_data - an array of Name/Value pairs
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.
See Also:
Get(java.lang.String, HTTPClient.NVPair[])

Head

public HTTPResponse Head(java.lang.String file,
                         NVPair[] form_data,
                         NVPair[] headers)
                  throws java.io.IOException,
                         ModuleException
Sends the HEAD request. This request is just like the corresponding GET except that it only returns the headers and no data.
Parameters:
file - the absolute path of the file
form_data - an array of Name/Value pairs
headers - additional headers
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.
See Also:
Get(java.lang.String, HTTPClient.NVPair[], HTTPClient.NVPair[])

Head

public HTTPResponse Head(java.lang.String file,
                         java.lang.String query)
                  throws java.io.IOException,
                         ModuleException
Sends the HEAD request. This request is just like the corresponding GET except that it only returns the headers and no data.
Parameters:
file - the absolute path of the file
query - the query string; it will be urlencoded
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.
See Also:
Get(java.lang.String, java.lang.String)

Head

public HTTPResponse Head(java.lang.String file,
                         java.lang.String query,
                         NVPair[] headers)
                  throws java.io.IOException,
                         ModuleException
Sends the HEAD request. This request is just like the corresponding GET except that it only returns the headers and no data.
Parameters:
file - the absolute path of the file
query - the query string; it will be urlencoded
headers - additional headers
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.
See Also:
Get(java.lang.String, java.lang.String, HTTPClient.NVPair[])

Get

public HTTPResponse Get(java.lang.String file)
                 throws java.io.IOException,
                        ModuleException
GETs the file.
Parameters:
file - the absolute path of the file
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Get

public HTTPResponse Get(java.lang.String file,
                        NVPair[] form_data)
                 throws java.io.IOException,
                        ModuleException
GETs the file with a query consisting of the specified form-data. The data is urlencoded, turned into a string of the form "name1=value1&name2=value2" and then sent as a query string.
Parameters:
file - the absolute path of the file
form_data - an array of Name/Value pairs
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Get

public HTTPResponse Get(java.lang.String file,
                        NVPair[] form_data,
                        NVPair[] headers)
                 throws java.io.IOException,
                        ModuleException
GETs the file with a query consisting of the specified form-data. The data is urlencoded, turned into a string of the form "name1=value1&name2=value2" and then sent as a query string.
Parameters:
file - the absolute path of the file
form_data - an array of Name/Value pairs
headers - additional headers
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Get

public HTTPResponse Get(java.lang.String file,
                        java.lang.String query)
                 throws java.io.IOException,
                        ModuleException
GETs the file using the specified query string. The query string is first urlencoded.
Parameters:
file - the absolute path of the file
query - the query
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Get

public HTTPResponse Get(java.lang.String file,
                        java.lang.String query,
                        NVPair[] headers)
                 throws java.io.IOException,
                        ModuleException
GETs the file using the specified query string. The query string is first urlencoded.
Parameters:
file - the absolute path of the file
query - the query string
headers - additional headers
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Post

public HTTPResponse Post(java.lang.String file)
                  throws java.io.IOException,
                         ModuleException
POSTs to the specified file. No data is sent.
Parameters:
file - the absolute path of the file
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Post

public HTTPResponse Post(java.lang.String file,
                         NVPair[] form_data)
                  throws java.io.IOException,
                         ModuleException
POSTs form-data to the specified file. The data is first urlencoded and then turned into a string of the form "name1=value1&name2=value2". A Content-type header with the value application/x-www-form-urlencoded is added.
Parameters:
file - the absolute path of the file
form_data - an array of Name/Value pairs
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Post

public HTTPResponse Post(java.lang.String file,
                         NVPair[] form_data,
                         NVPair[] headers)
                  throws java.io.IOException,
                         ModuleException
POST's form-data to the specified file using the specified headers. The data is first urlencoded and then turned into a string of the form "name1=value1&name2=value2". If no Content-type header is given then one is added with a value of application/x-www-form-urlencoded.
Parameters:
file - the absolute path of the file
form_data - an array of Name/Value pairs
headers - additional headers
Returns:
a HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Post

public HTTPResponse Post(java.lang.String file,
                         java.lang.String data)
                  throws java.io.IOException,
                         ModuleException
POSTs the data to the specified file. The data is converted to an array of bytes using the default character converter. The request is sent using the content-type "application/octet-stream".
Parameters:
file - the absolute path of the file
data - the data
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.
See Also:
String.getBytes()

Post

public HTTPResponse Post(java.lang.String file,
                         java.lang.String data,
                         NVPair[] headers)
                  throws java.io.IOException,
                         ModuleException
POSTs the data to the specified file using the specified headers.
Parameters:
file - the absolute path of the file
data - the data
headers - additional headers
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.
See Also:
String.getBytes()

Post

public HTTPResponse Post(java.lang.String file,
                         byte[] data)
                  throws java.io.IOException,
                         ModuleException
POSTs the raw data to the specified file. The request is sent using the content-type "application/octet-stream"
Parameters:
file - the absolute path of the file
data - the data
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Post

public HTTPResponse Post(java.lang.String file,
                         byte[] data,
                         NVPair[] headers)
                  throws java.io.IOException,
                         ModuleException
POSTs the raw data to the specified file using the specified headers.
Parameters:
file - the absolute path of the file
data - the data
headers - additional headers
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Post

public HTTPResponse Post(java.lang.String file,
                         HttpOutputStream stream)
                  throws java.io.IOException,
                         ModuleException
POSTs the data written to the output stream to the specified file. The request is sent using the content-type "application/octet-stream"
Parameters:
file - the absolute path of the file
stream - the output stream on which the data is written
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Post

public HTTPResponse Post(java.lang.String file,
                         HttpOutputStream stream,
                         NVPair[] headers)
                  throws java.io.IOException,
                         ModuleException
POSTs the data written to the output stream to the specified file using the specified headers.
Parameters:
file - the absolute path of the file
stream - the output stream on which the data is written
headers - additional headers
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Put

public HTTPResponse Put(java.lang.String file,
                        java.lang.String data)
                 throws java.io.IOException,
                        ModuleException
PUTs the data into the specified file. The data is converted to an array of bytes using the default character converter. The request ist sent using the content-type "application/octet-stream".
Parameters:
file - the absolute path of the file
data - the data
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.
See Also:
String.getBytes()

Put

public HTTPResponse Put(java.lang.String file,
                        java.lang.String data,
                        NVPair[] headers)
                 throws java.io.IOException,
                        ModuleException
PUTs the data into the specified file using the additional headers for the request.
Parameters:
file - the absolute path of the file
data - the data
headers - additional headers
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.
See Also:
String.getBytes()

Put

public HTTPResponse Put(java.lang.String file,
                        byte[] data)
                 throws java.io.IOException,
                        ModuleException
PUTs the raw data into the specified file. The request is sent using the content-type "application/octet-stream".
Parameters:
file - the absolute path of the file
data - the data
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Put

public HTTPResponse Put(java.lang.String file,
                        byte[] data,
                        NVPair[] headers)
                 throws java.io.IOException,
                        ModuleException
PUTs the raw data into the specified file using the additional headers.
Parameters:
file - the absolute path of the file
data - the data
headers - any additional headers
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Put

public HTTPResponse Put(java.lang.String file,
                        HttpOutputStream stream)
                 throws java.io.IOException,
                        ModuleException
PUTs the data written to the output stream into the specified file. The request is sent using the content-type "application/octet-stream".
Parameters:
file - the absolute path of the file
stream - the output stream on which the data is written
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Put

public HTTPResponse Put(java.lang.String file,
                        HttpOutputStream stream,
                        NVPair[] headers)
                 throws java.io.IOException,
                        ModuleException
PUTs the data written to the output stream into the specified file using the additional headers.
Parameters:
file - the absolute path of the file
stream - the output stream on which the data is written
headers - any additional headers
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Options

public HTTPResponse Options(java.lang.String file)
                     throws java.io.IOException,
                            ModuleException
Request OPTIONS from the server. If file is "*" then the request applies to the server as a whole; otherwise it applies only to that resource.
Parameters:
file - the absolute path of the resource, or "*"
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Options

public HTTPResponse Options(java.lang.String file,
                            NVPair[] headers)
                     throws java.io.IOException,
                            ModuleException
Request OPTIONS from the server. If file is "*" then the request applies to the server as a whole; otherwise it applies only to that resource.
Parameters:
file - the absolute path of the resource, or "*"
headers - the headers containing optional info.
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Options

public HTTPResponse Options(java.lang.String file,
                            NVPair[] headers,
                            byte[] data)
                     throws java.io.IOException,
                            ModuleException
Request OPTIONS from the server. If file is "*" then the request applies to the server as a whole; otherwise it applies only to that resource.
Parameters:
file - the absolute path of the resource, or "*"
headers - the headers containing optional info.
data - any data to be sent in the optional body
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Options

public HTTPResponse Options(java.lang.String file,
                            NVPair[] headers,
                            HttpOutputStream stream)
                     throws java.io.IOException,
                            ModuleException
Request OPTIONS from the server. If file is "*" then the request applies to the server as a whole; otherwise it applies only to that resource.
Parameters:
file - the absolute path of the resource, or "*"
headers - the headers containing optional info.
stream - an output stream for sending the optional body
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Delete

public HTTPResponse Delete(java.lang.String file)
                    throws java.io.IOException,
                           ModuleException
Requests that file be DELETEd from the server.
Parameters:
file - the absolute path of the resource
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Delete

public HTTPResponse Delete(java.lang.String file,
                           NVPair[] headers)
                    throws java.io.IOException,
                           ModuleException
Requests that file be DELETEd from the server.
Parameters:
file - the absolute path of the resource
headers - additional headers
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Trace

public HTTPResponse Trace(java.lang.String file,
                          NVPair[] headers)
                   throws java.io.IOException,
                          ModuleException
Requests a TRACE. Headers of particular interest here are "Via" and "Max-Forwards".
Parameters:
file - the absolute path of the resource
headers - additional headers
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

Trace

public HTTPResponse Trace(java.lang.String file)
                   throws java.io.IOException,
                          ModuleException
Requests a TRACE.
Parameters:
file - the absolute path of the resource
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

ExtensionMethod

public HTTPResponse ExtensionMethod(java.lang.String method,
                                    java.lang.String file,
                                    byte[] data,
                                    NVPair[] headers)
                             throws java.io.IOException,
                                    ModuleException
This is here to allow an arbitrary, non-standard request to be sent. I'm assuming you know what you are doing...
Parameters:
method - the extension method
file - the absolute path of the resource, or null
data - optional data, or null
headers - optional headers, or null
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

ExtensionMethod

public HTTPResponse ExtensionMethod(java.lang.String method,
                                    java.lang.String file,
                                    HttpOutputStream os,
                                    NVPair[] headers)
                             throws java.io.IOException,
                                    ModuleException
This is here to allow an arbitrary, non-standard request to be sent. I'm assuming you know what you are doing...
Parameters:
method - the extension method
file - the absolute path of the resource, or null
os - optional output stream, or null
headers - optional headers, or null
Returns:
an HTTPResponse structure containing the response
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

stop

public void stop()
Aborts all the requests currently in progress on this connection and closes all associated sockets. You usually do not need to invoke this - it only meant for when you need to abruptly stop things, such as for example the stop button in a browser.

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.

Since:
V0.2-3

setDefaultHeaders

public void setDefaultHeaders(NVPair[] headers)
Sets the default http headers to be sent with each request. The actual headers sent are determined as follows: for each header specified in multiple places a value given as part of the request takes priority over any default values set by this method, which in turn takes priority over any built-in default values. A different way of looking at it is that we start off with a list of all headers specified with the request, then add any default headers set by this method which aren't already in our list, and finally add any built-in headers which aren't yet in the list. There is one exception to this rule: the "Content-length" header is always ignored; and when posting form-data any default "Content-type" is ignored in favor of the built-in "application/x-www-form-urlencoded" (however it will be overriden by any content-type header specified as part of the request).

Typical headers you might want to set here are "Accept" and its "Accept-*" relatives, "Connection", "From", "User-Agent", etc.

Parameters:
headers - an array of header-name/value pairs (do not give the separating ':').

getDefaultHeaders

public NVPair[] getDefaultHeaders()
Gets the current list of default http headers.
Returns:
an array of header/value pairs.

getProtocol

public java.lang.String getProtocol()
Returns the protocol this connection is talking.
Returns:
a string containing the (lowercased) protocol

getHost

public java.lang.String getHost()
Returns the host this connection is talking to.
Returns:
a string containing the (lowercased) host name.

getPort

public int getPort()
Returns the port this connection connects to. This is always the actual port number, never -1.
Returns:
the port number

getProxyHost

public java.lang.String getProxyHost()
Returns the host of the proxy this connection is using.
Returns:
a string containing the (lowercased) host name.

getProxyPort

public int getProxyPort()
Returns the port of the proxy this connection is using.
Returns:
the port number

isCompatibleWith

public boolean isCompatibleWith(URI uri)
See if the given uri is compatible with this connection. Compatible means that the given uri can be retrieved using this connection object.
Parameters:
uri - the URI to check
Returns:
true if they're compatible, false otherwise
Since:
V0.3-2

setRawMode

public 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.
Sets/Resets raw mode. In raw mode all modules are bypassed, meaning the automatic handling of authorization requests, redirections, cookies, etc. is turned off.

The default is false.

Parameters:
raw - if true removes all modules (except for the retry module)
See Also:
removeModule(java.lang.Class)

setDefaultTimeout

public static void setDefaultTimeout(int time)
Sets the default read and connection timeout values to be used for each new HTTPConnection. The default is 0.
Parameters:
time - the timeout in milliseconds.
See Also:
setTimeout(int)

setDefaultConnectionTimeout

public static void setDefaultConnectionTimeout(int time)
Sets the default connection timeout values to be used for each new HTTPConnection. There is no corresponding setDefaultReadTimeout. To set the two timeout values, the code first calls setDefaultTimeout with the read timeout value and then setDefaultConnectionTime with the default connection timeout value.
Parameters:
time - the timeout in milliseconds.
See Also:
setTimeout(int), setDefaultTimeout(int)

getDefaultTimeout

public static int getDefaultTimeout()
Gets the default read timeout value to be used for each new HTTPConnection.
Returns:
the timeout in milliseconds.
See Also:
setTimeout(int)

getDefaultConnectionTimeout

public static int getDefaultConnectionTimeout()
Gets the default read timeout value to be used for each new HTTPConnection.
Returns:
the timeout in milliseconds.
See Also:
setTimeout(int)

setTimeout

public void setTimeout(int time)
Sets the timeouts to be used for creating connections and reading responses. There are two values that can be set indepenently. When a timeout expires the operation will throw an InterruptedIOException. The operation may be restarted again afterwards. If the operation is not restarted and it is a read operation (i.e HTTPResponse.xxxx()) then resp.getInputStream().close() should be invoked.

When creating new sockets the connection timeout will limit the time spent doing the host name translation and establishing the connection with the server.

The read timeout 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.

Parameters:
time - the time in milliseconds. A time of 0 means wait indefinitely.
See Also:
stop()

setConnectionTimeout

public void setConnectionTimeout(int time)
Sets the connection timeout values for this HTTPConnection. There is no corresponding setReadTimeout. To set the two timeout values, the code first calls setimeout with the read timeout value and then setConnectionTimeout with the connection timeout value.
Parameters:
time - the timeout in milliseconds.
See Also:
setTimeout(int), setDefaultTimeout(int)

getTimeout

public int getTimeout()
Gets the read timeout used for reading response data.
Returns:
the current timeout value
See Also:
setTimeout(int)

getConnectionTimeout

public int getConnectionTimeout()
Gets the read timeout used for reading response data.
Returns:
the current timeout value
See Also:
setTimeout(int)

setAllowUserInteraction

public void setAllowUserInteraction(boolean allow)
Controls whether modules are allowed to prompt the user or pop up dialogs if neccessary.
Parameters:
allow - if true allows modules to interact with user.

getAllowUserInteraction

public boolean getAllowUserInteraction()
returns whether modules are allowed to prompt or popup dialogs if neccessary.
Returns:
true if modules are allowed to interact with user.

setDefaultAllowUserInteraction

public static void setDefaultAllowUserInteraction(boolean allow)
Sets the default allow-user-action.
Parameters:
allow - if true allows modules to interact with user.

getDefaultAllowUserInteraction

public static boolean getDefaultAllowUserInteraction()
Gets the default allow-user-action.
Returns:
true if modules are allowed to interact with user.

getDefaultModules

public static java.lang.Class[] getDefaultModules()
Returns the default list of modules.
Returns:
an array of classes

addDefaultModule

public static boolean addDefaultModule(java.lang.Class module,
                                       int pos)
Adds a module to the default list. It must implement the HTTPClientModule interface. If the module is already in the list then this method does nothing. This method only affects instances of HTTPConnection created after this method has been invoked; it does not affect existing instances.

Example:

 HTTPConnection.addDefaultModule(Class.forName("HTTPClient.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 HTTPClient.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: "HTTPClient.RetryModule | HTTPClient.CookieModule | HTTPClient.RedirectionModule | HTTPClient.AuthorizationModule | HTTPClient.DefaultModule | HTTPClient.TransferEncodingModule | HTTPClient.ContentMD5Module | HTTPClient.ContentEncodingModule"

Parameters:
module - the module's Class object
pos - 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).
Returns:
true if module was successfully added; false if the module is already in the list.
Throws:
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.
See Also:
HTTPClientModule

removeDefaultModule

public static boolean removeDefaultModule(java.lang.Class module)
Removes a module from the default list. If the module is not in the list it does nothing. This method only affects instances of HTTPConnection created after this method has been invoked; it does not affect existing instances.
Parameters:
module - the module's Class object
Returns:
true if module was successfully removed; false otherwise

getModules

public java.lang.Class[] getModules()
Returns the list of modules used currently.
Returns:
an array of classes

addModule

public boolean addModule(java.lang.Class module,
                         int pos)
Adds a module to the current list. It must implement the HTTPClientModule interface. If the module is already in the list then this method does nothing.
Parameters:
module - the module's Class object
pos - 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).
Returns:
true if module was successfully added; false if the module is already in the list.
Throws:
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.
See Also:
HTTPClientModule

removeModule

public boolean removeModule(java.lang.Class module)
Removes a module from the current list. If the module is not in the list it does nothing.
Parameters:
module - the module's Class object
Returns:
true if module was successfully removed; false otherwise

setContext

public void setContext(java.lang.Object context)
Sets the current context. The context is used by modules such as the AuthorizationModule and the CookieModule which keep lists of info that is normally shared between all instances of HTTPConnection. This is usually the desired behaviour. However, in some cases one would like to simulate multiple independent clients within the same application and hence the sharing of such info should be restricted. This is where the context comes in. Modules will only share their info between requests using the same context (i.e. they keep multiple lists, one for each 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 HTTPConnection 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 HTTPConnection is created and before any request method is invoked. Furthermore, this method may only be called once (i.e. the context is "sticky").

Parameters:
context - the new context; must be non-null
Throws:
java.lang.IllegalArgumentException - if context is null
java.lang.IllegalStateException - if the context has already been set

getContext

public java.lang.Object getContext()
Returns the current context.
Returns:
the current context, or the default context if setContext() hasn't been invoked
See Also:
setContext(java.lang.Object)

getDefaultContext

public static java.lang.Object getDefaultContext()
Returns the default context.
Returns:
the default context
See Also:
setContext(java.lang.Object)

addDigestAuthorization

public 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. If an entry already exists for the "digest" scheme and the specified realm then it is overwritten.

This is a convenience method and just invokes the corresponding method in AuthorizationInfo.

Parameters:
realm - the realm
user - the username
passwd - the password
See Also:
AuthorizationInfo.addDigestAuthorization(java.lang.String, int, java.lang.String, java.lang.String, java.lang.String)

addBasicAuthorization

public 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. If an entry already exists for the "basic" scheme and the specified realm then it is overwritten.

This is a convenience method and just invokes the corresponding method in AuthorizationInfo.

Parameters:
realm - the realm
user - the username
passwd - the password
See Also:
AuthorizationInfo.addBasicAuthorization(java.lang.String, int, java.lang.String, java.lang.String, java.lang.String)

setProxyServer

public static void setProxyServer(java.lang.String host,
                                  int port)
Sets the default proxy server to use. The proxy will only be used for new HTTPConnections created after this call and will not affect currrent instances of HTTPConnection. A null or empty string host parameter disables the proxy.

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.

Parameters:
host - the host on which the proxy server resides.
port - the port the proxy server is listening on.
See Also:
setCurrentProxy(java.lang.String, int)

setCurrentProxy

public void setCurrentProxy(java.lang.String host,
                            int port)
Sets the proxy used by this instance. This can be used to override the proxy setting inherited from the default proxy setting. A null or empty string host parameter disables the proxy.

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.

Parameters:
host - the host the proxy runs on
port - the port the proxy is listening on
See Also:
setProxyServer(java.lang.String, int)

dontProxyFor

public static void dontProxyFor(java.lang.String host)
                         throws ParseException
Add host to the list of hosts which should be accessed directly, not via any proxy set by setProxyServer().

The host may be any of:

The two properties HTTPClient.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").

Parameters:
host - a host name, domain name, IP-address or IP-subnet.
Throws:
ParseException - if the length of the netmask does not match the length of the IP-address

dontProxyFor

public static void dontProxyFor(java.lang.String[] hosts)
Convenience method to add a number of hosts at once. If any one host is null or cannot be parsed it is ignored.
Parameters:
hosts - The list of hosts to set
Since:
V0.3-2
See Also:
dontProxyFor(java.lang.String)

doProxyFor

public static boolean doProxyFor(java.lang.String host)
                          throws ParseException
Remove host from the list of hosts for which the proxy should not be used. This modifies the same list that dontProxyFor() uses, i.e. this is used to undo a dontProxyFor() setting. The syntax for host is specified in dontProxyFor().
Parameters:
host - a host name, domain name, IP-address or IP-subnet.
Returns:
true if the remove was sucessful, false otherwise
Throws:
ParseException - if the length of the netmask does not match the length of the IP-address
See Also:
dontProxyFor(java.lang.String)

setSocksServer

public static void setSocksServer(java.lang.String host)
Sets the SOCKS server to use. The server will only be used for new HTTPConnections created after this call and will not affect currrent instances of HTTPConnection. A null or empty string host parameter disables SOCKS.

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.

Parameters:
host - the host on which the proxy server resides. The port used is the default port 1080.
See Also:
setSocksServer(java.lang.String, int, int)

setSocksServer

public static void setSocksServer(java.lang.String host,
                                  int port)
Sets the SOCKS server to use. The server will only be used for new HTTPConnections created after this call and will not affect currrent instances of HTTPConnection. A null or empty string host parameter disables SOCKS.

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.

Parameters:
host - the host on which the proxy server resides.
port - the port the proxy server is listening on.
See Also:
setSocksServer(java.lang.String, int, int)

setSocksServer

public static void setSocksServer(java.lang.String host,
                                  int port,
                                  int version)
                           throws SocksException
Sets the SOCKS server to use. The server will only be used for new HTTPConnections created after this call and will not affect currrent instances of HTTPConnection. A null or empty string host parameter disables SOCKS.

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): HTTPClient.socksHost, HTTPClient.socksPort and HTTPClient.socksVersion. Whether HTTPClient.socksHost is set or not determines whether a SOCKS server is used; if HTTPClient.socksPort is not set it defaults to 1080; if HTTPClient.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.
Parameters:
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'.
Throws:
SocksException - If version is not '4' or '5'.

setupRequest

protected final HTTPResponse setupRequest(java.lang.String method,
                                          java.lang.String resource,
                                          NVPair[] headers,
                                          byte[] entity,
                                          HttpOutputStream stream)
                                   throws java.io.IOException,
                                          ModuleException
Sets up the request, creating the list of headers to send and creating instances of the modules. This may be invoked by subclasses which add further methods (such as those from DAV and IPP).
Parameters:
method - GET, POST, etc.
resource - the resource
headers - an array of headers to be used
entity - the entity (or null)
stream - the output stream (or null) - only one of stream and entity may be non-null
Returns:
the response.
Throws:
java.io.IOException - when an exception is returned from the socket.
ModuleException - if an exception is encountered in any module.

toString

public java.lang.String toString()
Generates a string of the form protocol://host.domain:port .
Returns:
the string

connect

public void connect()
             throws java.io.IOException
Connects the socket to the server, but does not send any data. This methods is particularly useful for sercurity aware applications using https. Once the socket is connected, ssl session information can be obtained.
Throws:
java.io.IOException
See Also:
getSSLSession()

addTrustPoint

public void addTrustPoint(byte[] certificate)
Adds the specified trust point to the list of trusted certificates for this connection's OracleSSLCredential. This method is only useful when OracleSSL (the default) is used as the underlying SSL provider.
Parameters:
certificate - the certificate to add to the trusted list

addTrustPoint

public void addTrustPoint(java.lang.String certificate)
Adds the specified trust point to the list of trusted certificates for this connection's OracleSSLCredential. This method is only useful when OracleSSL (the default) is used as the underlying SSL provider.
Parameters:
certificate - the certificate to add to the trusted list

addTrustPoint

public void addTrustPoint(java.security.cert.Certificate certificate)
                   throws java.security.cert.CertificateEncodingException
Adds the specified trust point to the list of trusted certificates for this connection's OracleSSLCredential. This method is only useful when OracleSSL (the default) is used as the underlying SSL provider.
Parameters:
certificate - the certificate to add to the trusted list
Throws:
java.security.cert.CertificateEncodingException

setDefaultSSLSocketFactory

public static void setDefaultSSLSocketFactory(javax.net.ssl.SSLSocketFactory factory)
Set the default SSLSocketFactory for use by HTTPClient. This socket factory will be used on any HTTPConnection on which the specific factory has not been set by setSSLSocketFactory or on which it has been set to null.

If no SSLSocketFactory is specified for an HTTPConnection (either by default or via a call to HTTPConnection.setSSLSocketFactory) then HTTPClient will attempt to use the Oracle implementation of SSL if it is available on the classpath. Otherwise it will use SSLSocketFactory.getDefault().

Parameters:
factory - the SSLSocketFactory to be used by default.

setSSLSocketFactory

public void setSSLSocketFactory(javax.net.ssl.SSLSocketFactory factory)
Sets the SSLSocketFactory that will be used for https connections
Parameters:
factory - the SSLSocketFactory used for https in this HTTPConnection.

getSSLCredential

public OracleSSLCredential getSSLCredential()
Returns the SSLCredential used by this socket

getSSLEnabledCipherSuites

public java.lang.String[] getSSLEnabledCipherSuites()
Returns the list of cipher suites that are enabled for the https connection

getSSLSession

public javax.net.ssl.SSLSession getSSLSession()
Returns the ssl session information for the ssl connection or null if no ssl connection has been established
See Also:
connect()

getSSLSocketFactory

public javax.net.ssl.SSLSocketFactory getSSLSocketFactory()
Returns the socket factory used to create SSL sockets

getSSLSocket

public java.net.Socket getSSLSocket(java.net.Socket proxy)
                             throws java.io.IOException
Obtains an new SSL Socket to be used for HTTPS connections If setSSLCredentials() or setSSLCipherSuites() were not called prior to this call, the property settings are checked
Throws:
java.io.IOException

setSSLCredential

public void setSSLCredential(OracleSSLCredential cred)
Sets the OracleSSLCredential object for use in SSL connections
Parameters:
cred - the credential object that will be used for the connection.

setSSLEnabledCipherSuites

public void setSSLEnabledCipherSuites(java.lang.String[] cipherSuites)
Controls which particular cipher suites are enabled for use on this connection. The cipher suites must have been listed by getSupportedCipherSuites() as being supported. Even if a suite has been enabled, it might never be used if no peer supports it, or the requisite certificates (and private keys) are not available.
Parameters:
cipherSuites - Names of all the cipher suites to enable.
Throws:
java.lang.IllegalArgumentException - when one of the ciphers named by the parameter is not supported.
See Also:
getSSLEnabledCipherSuites()

getQueryEncoding

public java.lang.String getQueryEncoding()
The current Java encoding used for the queries part of a URL Initially null.
Returns:
the current encoding

finalize

protected void finalize()
                 throws java.lang.Throwable
Added as a safety net, in-case HTTPConnection object is not referenced any more, make sure to release the sockets held by this.
Throws:
java.lang.Throwable

setQueryEncoding

public void setQueryEncoding(java.lang.String encoding)
Sets the Java encoding to be used for the query parts of URL's in requests If it is null the System default encoding is used. Strings being used in queries are first encoded using this encoding and then any "non-standard" characters are replace by a %HH escape.
Parameters:
encoding - the Java encoding

Oracle Application Server HTTPClient API Reference
10g Release 2 (10.1.2)

B14020-02


Copyright © 2004, 2005, Oracle. All rights reserved.