gnu.java.net.protocol.http

Class HTTPConnection


public class HTTPConnection
extends Object

A connection to an HTTP server.

Field Summary

static int
HTTPS_PORT
The default HTTPS port.
static int
HTTP_PORT
The default HTTP port.
protected int
connectionTimeout
The connection timeout for connecting the underlying socket.
protected CookieManager
cookieManager
The cookie manager for this connection.
protected String
hostname
The host name of the server to connect to.
protected InputStream
in
The socket input stream.
protected int
majorVersion
The major version of HTTP supported by this client.
protected int
minorVersion
The minor version of HTTP supported by this client.
protected OutputStream
out
The socket output stream.
protected int
port
The port to connect to.
protected String
proxyHostname
The host name of the proxy to connect to.
protected int
proxyPort
The port on the proxy to connect to.
protected boolean
secure
Whether the connection should use transport level security (HTTPS).
protected Socket
socket
The socket this connection communicates on.
protected int
timeout
The read timeout for reads on the underlying socket.

Constructor Summary

HTTPConnection(String hostname)
Creates a new HTTP connection.
HTTPConnection(String hostname, boolean secure)
Creates a new HTTP or HTTPS connection.
HTTPConnection(String hostname, boolean secure, int connectionTimeout, int timeout)
Creates a new HTTP or HTTPS connection on the specified port.
HTTPConnection(String hostname, int port)
Creates a new HTTP connection on the specified port.
HTTPConnection(String hostname, int port, boolean secure)
Creates a new HTTP or HTTPS connection on the specified port.
HTTPConnection(String hostname, int port, boolean secure, int connectionTimeout, int timeout)
Creates a new HTTP or HTTPS connection on the specified port.

Method Summary

void
close()
Closes this connection.
protected void
closeConnection()
Closes the underlying socket, if any.
CookieManager
getCookieManager()
Returns the cookie manager in use for this connection.
String
getHostName()
Returns the name of the host to connect to.
protected InputStream
getInputStream()
protected OutputStream
getOutputStream()
int
getPort()
Returns the port on the host to connect to.
protected Socket
getSocket()
Retrieves the socket associated with this connection.
protected String
getURI()
Returns a URI representing the connection.
String
getVersion()
Returns the HTTP version string supported by this connection.
boolean
isSecure()
Indicates whether to use a secure connection or not.
boolean
isUsingProxy()
Indicates whether this connection is using an HTTP proxy.
Request
newRequest(String method, String path)
Creates a new request using this connection.
void
setCookieManager(CookieManager cookieManager)
Sets the cookie manager to use for this connection.
void
setProxy(String hostname, int port)
Directs this connection to use the specified proxy.
void
setVersion(int majorVersion, int minorVersion)
Sets the HTTP version supported by this connection.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

HTTPS_PORT

public static final int HTTPS_PORT
The default HTTPS port.
Field Value:
443

HTTP_PORT

public static final int HTTP_PORT
The default HTTP port.
Field Value:
80

connectionTimeout

protected final int connectionTimeout
The connection timeout for connecting the underlying socket.

cookieManager

protected CookieManager cookieManager
The cookie manager for this connection.

hostname

protected final String hostname
The host name of the server to connect to.

in

protected InputStream in
The socket input stream.

majorVersion

protected int majorVersion
The major version of HTTP supported by this client.

minorVersion

protected int minorVersion
The minor version of HTTP supported by this client.

out

protected OutputStream out
The socket output stream.

port

protected final int port
The port to connect to.

proxyHostname

protected String proxyHostname
The host name of the proxy to connect to.

proxyPort

protected int proxyPort
The port on the proxy to connect to.

secure

protected final boolean secure
Whether the connection should use transport level security (HTTPS).

socket

protected Socket socket
The socket this connection communicates on.

timeout

protected final int timeout
The read timeout for reads on the underlying socket.

Constructor Details

HTTPConnection

public HTTPConnection(String hostname)
Creates a new HTTP connection.
Parameters:
hostname - the name of the host to connect to

HTTPConnection

public HTTPConnection(String hostname,
                      boolean secure)
Creates a new HTTP or HTTPS connection.
Parameters:
hostname - the name of the host to connect to
secure - whether to use a secure connection

HTTPConnection

public HTTPConnection(String hostname,
                      boolean secure,
                      int connectionTimeout,
                      int timeout)
Creates a new HTTP or HTTPS connection on the specified port.
Parameters:
hostname - the name of the host to connect to
secure - whether to use a secure connection
connectionTimeout - the connection timeout
timeout - the socket read timeout

HTTPConnection

public HTTPConnection(String hostname,
                      int port)
Creates a new HTTP connection on the specified port.
Parameters:
hostname - the name of the host to connect to
port - the port on the host to connect to

HTTPConnection

public HTTPConnection(String hostname,
                      int port,
                      boolean secure)
Creates a new HTTP or HTTPS connection on the specified port.
Parameters:
hostname - the name of the host to connect to
port - the port on the host to connect to
secure - whether to use a secure connection

HTTPConnection

public HTTPConnection(String hostname,
                      int port,
                      boolean secure,
                      int connectionTimeout,
                      int timeout)
Creates a new HTTP or HTTPS connection on the specified port.
Parameters:
hostname - the name of the host to connect to
port - the port on the host to connect to
secure - whether to use a secure connection
connectionTimeout - the connection timeout
timeout - the socket read timeout
Throws:
IllegalArgumentException - if either connectionTimeout or timeout less than zero.

Method Details

close

public void close()
            throws IOException
Closes this connection.

closeConnection

protected void closeConnection()
            throws IOException
Closes the underlying socket, if any.

getCookieManager

public CookieManager getCookieManager()
Returns the cookie manager in use for this connection.

getHostName

public String getHostName()
Returns the name of the host to connect to.

getInputStream

protected InputStream getInputStream()
            throws IOException

getOutputStream

protected OutputStream getOutputStream()
            throws IOException

getPort

public int getPort()
Returns the port on the host to connect to.

getSocket

protected Socket getSocket()
            throws IOException
Retrieves the socket associated with this connection. This creates the socket if necessary.

getURI

protected String getURI()
Returns a URI representing the connection. This does not include any request path component.

getVersion

public String getVersion()
Returns the HTTP version string supported by this connection.

isSecure

public boolean isSecure()
Indicates whether to use a secure connection or not.

isUsingProxy

public boolean isUsingProxy()
Indicates whether this connection is using an HTTP proxy.

newRequest

public Request newRequest(String method,
                          String path)
Creates a new request using this connection.
Parameters:
method - the HTTP method to invoke
path - the URI-escaped RFC2396 abs_path with optional query part

setCookieManager

public void setCookieManager(CookieManager cookieManager)
Sets the cookie manager to use for this connection.
Parameters:
cookieManager - the cookie manager

setProxy

public void setProxy(String hostname,
                     int port)
Directs this connection to use the specified proxy.
Parameters:
hostname - the proxy host name
port - the port on the proxy to connect to

setVersion

public void setVersion(int majorVersion,
                       int minorVersion)
Sets the HTTP version supported by this connection.
Parameters:
majorVersion - the major version
minorVersion - the minor version

HTTPConnection.java -- Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.