public class HttpConnection extends Object
To use this do the following:
Modifier and Type | Class and Description |
---|---|
static class |
HttpConnection.ConnectionNotAllowedException
This is thrown by getRedirectedConnection() or by makeNewSocket() if the
HttpConnection determines that a connection to the new URL is not allowed.
|
static interface |
HttpConnection.SocketMaker |
Modifier and Type | Field and Description |
---|---|
String |
command
This is the command to be sent to the server.
|
protected Socket |
connectedSocket |
int |
contentLength
This is the length of the document read in, valid after a connection call.
|
static int |
DataReady |
String |
document
The document to fetch/submit.
|
boolean |
documentIsEncoded
If the document you supplied is already URL encoded, set this to true.
|
PropertyList |
documentProperties
This is the list of properties for the server and document.
|
String |
host
The host to connect to.
|
boolean |
keepAliveMode
Set this to true for keep alive mode requests.
|
protected Socket |
openSocket |
int |
port
The port to connect to.
|
PropertyList |
requestorProperties
These are the properties that will be sent to the WebServer.
|
String |
requestVersion
This is the protocol version sent to the server.
|
int |
responseCode
This is the response code from the server.
|
String |
scheme
The scheme being used - which defaults to "http"
|
static int |
SocketConnected |
TextCodec |
textCodec
This is the codec used when sending data to the server.
|
Constructor and Description |
---|
HttpConnection(HttpConnection redirectedFrom,
String redirectTo)
Create a new HttpConnection that has been redirected from a previous one, copying over the
original paramters.
|
HttpConnection(String url)
Create an HttpConnection with an http:// URL.
|
HttpConnection(String host,
int port,
String document)
Create a new HttpConnection to the specified host and port to fetch the specified document,
using the "http" protocol.
|
HttpConnection(URL url) |
Modifier and Type | Method and Description |
---|---|
void |
addRequestorProperty(String name,
String property)
Add a non-exclusive requestor property.
|
HttpConnection |
addSocketMaker(String forScheme,
HttpConnection.SocketMaker maker) |
Socket |
connect()
This makes the connection, blocking the current thread.
|
Socket |
connect(TextCodec serverCodec)
This makes the connection, blocking the current thread.
|
Handle |
connectAsync()
Connect asynchronously.
|
Handle |
connectAsync(TextCodec serverTextDecoder)
Connect asynchronously.
|
Socket |
connectSocketOnly()
Connect to the server and save the socket for later use as the "connectedSocket" field.
|
protected Socket |
createNewSocket(Handle h,
String scheme,
String host,
int port)
Make a connection to the host for the specified protocol.
|
void |
defaultRequestorProperty(String name,
String defaultValue)
Set the default value of a requestor property.
|
static int |
getDefaultPort(String scheme,
int defaultTo)
Get the default port for a specific scheme, e.g.
|
String |
getEncodedDocument() |
InputStream |
getInputStream()
Get an InputStream to read in the data.
|
InputStream |
getInputStream(Socket connectedSocket)
Get an InputStream to read in the data.
|
protected HttpConnection |
getNewConnection(String url)
By default this calls the constructor of the Class of this HttpConnection
but you may need to override this if you are extending HttpConnection
|
OutputStream |
getOutputStream() |
HttpConnection |
getRedirectedConnection(String redirectTo)
Deprecated.
use getRedirection(String url) instead.
|
HttpConnection |
getRedirection(String redirectTo)
Get a new HttpConnection whose parameters are copied from this HttpConnection
but which directs its request to a different host/document as directed by
a redirect response from an initial request.
|
String |
getRedirectTo()
Call this after a success connection.
|
PropertyList |
getRequestorProperties()
Returns the requestor properties.
|
boolean |
hasPostData()
This returns true if post data has been set for this connection.
|
ByteArray |
readData(Socket connection)
Read in the document body from the Socket.
|
Handle |
readInData()
Read in all the data from the Socket.
|
Handle |
readInData(Socket connection)
Read in all the data from the Socket.
|
Handle |
readInText(Socket connection,
TextCodec documentTextDecoder)
Read in all the data from the Socket, converting it to text using the specified
codec.
|
CharArray |
readText(Socket connection,
TextCodec documentTextDecoder)
Read in the document body from the Socket.
|
HttpConnection |
redirectionTo()
Call this after a successful connection.
|
HttpConnection |
redirectTo()
Deprecated.
use redirectionTo() instead.
|
void |
setAlreadyOpenSocket(Socket sock)
If a connection has already been made to the server, then you can call
this method and the HttpConnection protocol will be done over this Socket.
|
void |
setContentLength(int length)
This sets the "Content-Length" requestor property to be the specified length.
|
static void |
setDefaultPort(String scheme,
int port)
Set the default port for a specific scheme, e.g.
|
void |
setGetData(Object data) |
void |
setPostData(Object data)
Set the data to post out as either a Stream, InputStream,byte[],ByteArray or String.
|
void |
setPostDataLength(int length)
Deprecated.
use setContentLength() instead.
|
void |
setRequestorProperty(String name,
String property)
Set an exclusive requestor property.
|
protected void |
setupUrl(String url)
Parse the URL and setup the scheme, host, port and document.
|
String |
toURLString() |
public String scheme
public String host
public int port
public String document
public String command
public String requestVersion
public PropertyList requestorProperties
public PropertyList documentProperties
public int responseCode
public boolean documentIsEncoded
public boolean keepAliveMode
public int contentLength
public TextCodec textCodec
protected Socket openSocket
protected Socket connectedSocket
public static final int SocketConnected
public static final int DataReady
public HttpConnection(String host, int port, String document)
host
- The host to connect to.port
- The port to connect on.document
- the document to get.public HttpConnection(HttpConnection redirectedFrom, String redirectTo) throws HttpConnection.ConnectionNotAllowedException
redirectedFrom
- the HttpConnection being redirected from.redirectTo
- the new URL being directed to.HttpConnection.ConnectionNotAllowedException
- if this HttpConnection object determines that such
a connection should not be allowed.public HttpConnection(String url)
url
- The full url, starting with http://public HttpConnection(URL url)
public boolean hasPostData()
public static int getDefaultPort(String scheme, int defaultTo)
scheme
- the scheme used for the HTTP request.defaultTo
- the port number to default to if no default port was found.public static void setDefaultPort(String scheme, int port)
scheme
- the scheme used for the HTTP request.port
- the new default port for the scheme.protected HttpConnection getNewConnection(String url) throws HttpConnection.ConnectionNotAllowedException
url
- the url to connect to.HttpConnection.ConnectionNotAllowedException
- if this HttpConnection object determines that such
a connection should not be allowed.public HttpConnection getRedirectedConnection(String redirectTo)
Note that if this is a POST request and if the post-data source is an InputStream or Stream, then you will need to call setPostData() again to set up the post data for the new connection. Otherwise, the post data will be copied to this device. Call hasPostData() to determine if the post data was copied across successfully.
redirectTo
- the URL that the server instructed the client to redirect to.public HttpConnection getRedirection(String redirectTo) throws HttpConnection.ConnectionNotAllowedException
Note that if this is a POST request and if the post-data source is an InputStream or Stream, then you will need to call setPostData() again to set up the post data for the new connection. Otherwise, the post data will be copied to this device. Call hasPostData() to determine if the post data was copied across successfully.
redirectTo
- the URL that the server instructed the client to redirect to.HttpConnection.ConnectionNotAllowedException
- if this HttpConnection object determines that such
a connection should not be allowed.public PropertyList getRequestorProperties()
public void setPostData(Object data)
data
- the data to post either as a Stream, InputStream, byte[] or ByteArraypublic void setGetData(Object data)
public void setPostDataLength(int length)
length
- the number of bytes to be posted.public void setContentLength(int length)
length
- the number of bytes to be posted.public void setRequestorProperty(String name, String property)
name
- The name of the property.property
- The value of the property.public void addRequestorProperty(String name, String property)
name
- The name of the property.property
- The value of the property.public void defaultRequestorProperty(String name, String defaultValue)
name
- the name of the property.defaultValue
- the value to default to.public void setAlreadyOpenSocket(Socket sock)
sock
- The already connected socket.protected void setupUrl(String url)
url
- the url.public String toURLString()
public String getEncodedDocument()
public HttpConnection redirectTo()
public HttpConnection redirectionTo() throws HttpConnection.ConnectionNotAllowedException
public String getRedirectTo()
public Handle readInData(Socket connection)
connection
- The socket returned by a connect() call.public Handle readInData()
public InputStream getInputStream(Socket connectedSocket) throws IOException
IOException
public OutputStream getOutputStream() throws IOException
IOException
public InputStream getInputStream() throws IOException
IOException
public ByteArray readData(Socket connection) throws IOException
connection
- The socket returned by a connect() call.IOException
public Handle readInText(Socket connection, TextCodec documentTextDecoder)
connection
- The socket returned by a connect() call.documentTextDecoder
- The text codec to use to convert the bytes read in into text. If
this is null then the textCodec is used.public CharArray readText(Socket connection, TextCodec documentTextDecoder) throws IOException
connection
- The socket returned by a connect() call.documentTextDecoder
- The text codec to use to convert the bytes read in into text. If
this is null then the textCodec is used.IOException
public Handle connectAsync()
public Handle connectAsync(TextCodec serverTextDecoder)
serverTextDecoder
- The text decoder to convert the server and requestor properties data into text.public Socket connect() throws IOException
IOException
- if there was an error connecting or getting the data.public Socket connect(TextCodec serverCodec) throws IOException
serverCodec
- the TextCodec to use when communicating with the server.IOException
- if there was an error connecting or getting the data.public Socket connectSocketOnly() throws IOException, HttpConnection.ConnectionNotAllowedException
IOException
- if a connection could not be made.HttpConnection.ConnectionNotAllowedException
public HttpConnection addSocketMaker(String forScheme, HttpConnection.SocketMaker maker)
protected Socket createNewSocket(Handle h, String scheme, String host, int port) throws StoppedException, HttpConnection.ConnectionNotAllowedException, IOException
h
- an optional Handle to monitor and possible stop the operation.scheme
- the scheme (e.g. "http" or "https").host
- the host to connect to.port
- the port to connect to.StoppedException
- if stop() was called on the supplied handle and this method is able to detect
that fact and abort the connection.HttpConnection.ConnectionNotAllowedException
- if the HttpConnection determines that such a connection is not allowed.IOException
- for any other IOException