public abstract class

HttpURLConnection

extends URLConnection
java.lang.Object
   ↳ java.net.URLConnection
     ↳ java.net.HttpURLConnection
Known Direct Subclasses

Class Overview

This abstract subclass of URLConnection defines methods for managing HTTP connection according to the description given by RFC 2068.

Summary

Constants
int HTTP_ACCEPTED Numeric status code, 202: Accepted
int HTTP_BAD_GATEWAY Numeric status code, 502: Bad Gateway
int HTTP_BAD_METHOD Numeric status code, 405: Bad Method
int HTTP_BAD_REQUEST Numeric status code, 400: Bad Request
int HTTP_CLIENT_TIMEOUT Numeric status code, 408: Client Timeout
int HTTP_CONFLICT Numeric status code, 409: Conflict
int HTTP_CREATED Numeric status code, 201: Created
int HTTP_ENTITY_TOO_LARGE Numeric status code, 413: Entity too large
int HTTP_FORBIDDEN Numeric status code, 403: Forbidden
int HTTP_GATEWAY_TIMEOUT Numeric status code, 504: Gateway timeout
int HTTP_GONE Numeric status code, 410: Gone
int HTTP_INTERNAL_ERROR Numeric status code, 500: Internal error
int HTTP_LENGTH_REQUIRED Numeric status code, 411: Length required
int HTTP_MOVED_PERM Numeric status code, 301 Moved permanently
int HTTP_MOVED_TEMP Numeric status code, 302: Moved temporarily
int HTTP_MULT_CHOICE Numeric status code, 300: Multiple choices
int HTTP_NOT_ACCEPTABLE Numeric status code, 406: Not acceptable
int HTTP_NOT_AUTHORITATIVE Numeric status code, 203: Not authoritative
int HTTP_NOT_FOUND Numeric status code, 404: Not found
int HTTP_NOT_IMPLEMENTED Numeric status code, 501: Not implemented
int HTTP_NOT_MODIFIED Numeric status code, 304: Not modified
int HTTP_NO_CONTENT Numeric status code, 204: No content
int HTTP_OK Numeric status code, 200: OK
int HTTP_PARTIAL Numeric status code, 206: Partial
int HTTP_PAYMENT_REQUIRED Numeric status code, 402: Payment required
int HTTP_PRECON_FAILED Numeric status code, 412: Precondition failed
int HTTP_PROXY_AUTH Numeric status code, 407: Proxy authentication required
int HTTP_REQ_TOO_LONG Numeric status code, 414: Request too long
int HTTP_RESET Numeric status code, 205: Reset
int HTTP_SEE_OTHER Numeric status code, 303: See other
int HTTP_SERVER_ERROR This constant is deprecated. Use HTTP_INTERNAL_ERROR
int HTTP_UNAUTHORIZED Numeric status code, 401: Unauthorized
int HTTP_UNAVAILABLE Numeric status code, 503: Unavailable
int HTTP_UNSUPPORTED_TYPE Numeric status code, 415: Unsupported type
int HTTP_USE_PROXY Numeric status code, 305: Use proxy
int HTTP_VERSION Numeric status code, 505: Version not supported
Fields
protected int chunkLength If the HTTP chunked encoding is enabled this parameter defines the chunk-length.
protected int fixedContentLength If using HTTP fixed-length streaming mode this parameter defines the fixed length of content.
protected boolean instanceFollowRedirects Flag to define whether the protocol will automatically follow redirects or not.
protected String method The HTTP request method of this HttpURLConnection.
protected int responseCode The status code of the response obtained from the HTTP request.
protected String responseMessage The HTTP response message which corresponds to the response code.
[Expand]
Inherited Fields
From class java.net.URLConnection
Protected Constructors
HttpURLConnection(URL url)
Constructs a new HttpURLConnection instance pointing to the resource specified by the url.
Public Methods
abstract void disconnect()
Closes the connection to the HTTP server.
InputStream getErrorStream()
Returns an input stream from the server in the case of an error such as the requested file has not been found on the remote server.
static boolean getFollowRedirects()
Returns the value of followRedirects which indicates if this connection follows a different URL redirected by the server.
long getHeaderFieldDate(String field, long defaultValue)
Returns the date value in milliseconds since 01.01.1970, 00:00h corresponding to the header field field.
boolean getInstanceFollowRedirects()
Returns whether this connection follows redirects.
Permission getPermission()
Returns the permission object (in this case SocketPermission) with the host and the port number as the target name and "resolve, connect" as the action list.
String getRequestMethod()
Returns the request method which will be used to make the request to the remote HTTP server.
int getResponseCode()
Returns the response code returned by the remote HTTP server.
String getResponseMessage()
Returns the response message returned by the remote HTTP server.
void setChunkedStreamingMode(int chunklen)
If the length of a HTTP request body is NOT known ahead, enable chunked transfer encoding to enable streaming with buffering.
void setFixedLengthStreamingMode(int contentLength)
If the length of a HTTP request body is known ahead, sets fixed length to enable streaming without buffering.
static void setFollowRedirects(boolean auto)
Sets the flag of whether this connection will follow redirects returned by the remote server.
void setInstanceFollowRedirects(boolean followRedirects)
Sets whether this connection follows redirects.
void setRequestMethod(String method)
Sets the request command which will be sent to the remote HTTP server.
abstract boolean usingProxy()
Returns whether this connection uses a proxy server or not.
[Expand]
Inherited Methods
From class java.net.URLConnection
From class java.lang.Object

Constants

public static final int HTTP_ACCEPTED

Numeric status code, 202: Accepted

Constant Value: 202 (0x000000ca)

public static final int HTTP_BAD_GATEWAY

Numeric status code, 502: Bad Gateway

Constant Value: 502 (0x000001f6)

public static final int HTTP_BAD_METHOD

Numeric status code, 405: Bad Method

Constant Value: 405 (0x00000195)

public static final int HTTP_BAD_REQUEST

Numeric status code, 400: Bad Request

Constant Value: 400 (0x00000190)

public static final int HTTP_CLIENT_TIMEOUT

Numeric status code, 408: Client Timeout

Constant Value: 408 (0x00000198)

public static final int HTTP_CONFLICT

Numeric status code, 409: Conflict

Constant Value: 409 (0x00000199)

public static final int HTTP_CREATED

Numeric status code, 201: Created

Constant Value: 201 (0x000000c9)

public static final int HTTP_ENTITY_TOO_LARGE

Numeric status code, 413: Entity too large

Constant Value: 413 (0x0000019d)

public static final int HTTP_FORBIDDEN

Numeric status code, 403: Forbidden

Constant Value: 403 (0x00000193)

public static final int HTTP_GATEWAY_TIMEOUT

Numeric status code, 504: Gateway timeout

Constant Value: 504 (0x000001f8)

public static final int HTTP_GONE

Numeric status code, 410: Gone

Constant Value: 410 (0x0000019a)

public static final int HTTP_INTERNAL_ERROR

Numeric status code, 500: Internal error

Constant Value: 500 (0x000001f4)

public static final int HTTP_LENGTH_REQUIRED

Numeric status code, 411: Length required

Constant Value: 411 (0x0000019b)

public static final int HTTP_MOVED_PERM

Numeric status code, 301 Moved permanently

Constant Value: 301 (0x0000012d)

public static final int HTTP_MOVED_TEMP

Numeric status code, 302: Moved temporarily

Constant Value: 302 (0x0000012e)

public static final int HTTP_MULT_CHOICE

Numeric status code, 300: Multiple choices

Constant Value: 300 (0x0000012c)

public static final int HTTP_NOT_ACCEPTABLE

Numeric status code, 406: Not acceptable

Constant Value: 406 (0x00000196)

public static final int HTTP_NOT_AUTHORITATIVE

Numeric status code, 203: Not authoritative

Constant Value: 203 (0x000000cb)

public static final int HTTP_NOT_FOUND

Numeric status code, 404: Not found

Constant Value: 404 (0x00000194)

public static final int HTTP_NOT_IMPLEMENTED

Numeric status code, 501: Not implemented

Constant Value: 501 (0x000001f5)

public static final int HTTP_NOT_MODIFIED

Numeric status code, 304: Not modified

Constant Value: 304 (0x00000130)

public static final int HTTP_NO_CONTENT

Numeric status code, 204: No content

Constant Value: 204 (0x000000cc)

public static final int HTTP_OK

Numeric status code, 200: OK

Constant Value: 200 (0x000000c8)

public static final int HTTP_PARTIAL

Numeric status code, 206: Partial

Constant Value: 206 (0x000000ce)

public static final int HTTP_PAYMENT_REQUIRED

Numeric status code, 402: Payment required

Constant Value: 402 (0x00000192)

public static final int HTTP_PRECON_FAILED

Numeric status code, 412: Precondition failed

Constant Value: 412 (0x0000019c)

public static final int HTTP_PROXY_AUTH

Numeric status code, 407: Proxy authentication required

Constant Value: 407 (0x00000197)

public static final int HTTP_REQ_TOO_LONG

Numeric status code, 414: Request too long

Constant Value: 414 (0x0000019e)

public static final int HTTP_RESET

Numeric status code, 205: Reset

Constant Value: 205 (0x000000cd)

public static final int HTTP_SEE_OTHER

Numeric status code, 303: See other

Constant Value: 303 (0x0000012f)

public static final int HTTP_SERVER_ERROR

This constant is deprecated.Use HTTP_INTERNAL_ERROR

Numeric status code, 500: Internal error

Constant Value: 500 (0x000001f4)

public static final int HTTP_UNAUTHORIZED

Numeric status code, 401: Unauthorized

Constant Value: 401 (0x00000191)

public static final int HTTP_UNAVAILABLE

Numeric status code, 503: Unavailable

Constant Value: 503 (0x000001f7)

public static final int HTTP_UNSUPPORTED_TYPE

Numeric status code, 415: Unsupported type

Constant Value: 415 (0x0000019f)

public static final int HTTP_USE_PROXY

Numeric status code, 305: Use proxy

Constant Value: 305 (0x00000131)

public static final int HTTP_VERSION

Numeric status code, 505: Version not supported

Constant Value: 505 (0x000001f9)

Fields

protected int chunkLength

If the HTTP chunked encoding is enabled this parameter defines the chunk-length. Default value is -1 that means the chunked encoding mode is disabled.

protected int fixedContentLength

If using HTTP fixed-length streaming mode this parameter defines the fixed length of content. Default value is -1 that means the fixed-length streaming mode is disabled.

protected boolean instanceFollowRedirects

Flag to define whether the protocol will automatically follow redirects or not. The default value is true.

protected String method

The HTTP request method of this HttpURLConnection. The default value is "GET".

protected int responseCode

The status code of the response obtained from the HTTP request. The default value is -1.

  • 1xx: Informational
  • 2xx: Success
  • 3xx: Relocation/Redirection
  • 4xx: Client Error
  • 5xx: Server Error
  • protected String responseMessage

    The HTTP response message which corresponds to the response code.

    Protected Constructors

    protected HttpURLConnection (URL url)

    Constructs a new HttpURLConnection instance pointing to the resource specified by the url.

    Parameters
    url the URL of this connection.
    See Also

    Public Methods

    public abstract void disconnect ()

    Closes the connection to the HTTP server.

    public InputStream getErrorStream ()

    Returns an input stream from the server in the case of an error such as the requested file has not been found on the remote server. This stream can be used to read the data the server will send back.

    Returns
    • the error input stream returned by the server.

    public static boolean getFollowRedirects ()

    Returns the value of followRedirects which indicates if this connection follows a different URL redirected by the server. It is enabled by default.

    Returns
    • the value of the flag.

    public long getHeaderFieldDate (String field, long defaultValue)

    Returns the date value in milliseconds since 01.01.1970, 00:00h corresponding to the header field field. The defaultValue will be returned if no such field can be found in the response header.

    Parameters
    field the header field name.
    defaultValue the default value to use if the specified header field wont be found.
    Returns
    • the header field represented in milliseconds since January 1, 1970 GMT.

    public boolean getInstanceFollowRedirects ()

    Returns whether this connection follows redirects.

    Returns
    • true if this connection follows redirects, false otherwise.

    public Permission getPermission ()

    Returns the permission object (in this case SocketPermission) with the host and the port number as the target name and "resolve, connect" as the action list. If the port number of this URL instance is lower than 0 the port will be set to 80.

    Returns
    • the permission object required for this connection.
    Throws
    IOException if an IO exception occurs during the creation of the permission object.

    public String getRequestMethod ()

    Returns the request method which will be used to make the request to the remote HTTP server. All possible methods of this HTTP implementation is listed in the class definition.

    Returns
    • the request method string.

    public int getResponseCode ()

    Returns the response code returned by the remote HTTP server.

    Returns
    • the response code, -1 if no valid response code.
    Throws
    IOException if there is an IO error during the retrieval.

    public String getResponseMessage ()

    Returns the response message returned by the remote HTTP server.

    Returns
    • the response message. null if no such response exists.
    Throws
    IOException if there is an error during the retrieval.

    public void setChunkedStreamingMode (int chunklen)

    If the length of a HTTP request body is NOT known ahead, enable chunked transfer encoding to enable streaming with buffering. Notice that not all http servers support this mode. Sets after connection will cause an exception.

    Parameters
    chunklen the length of a chunk.
    Throws
    IllegalStateException if already connected or an other mode already set.

    public void setFixedLengthStreamingMode (int contentLength)

    If the length of a HTTP request body is known ahead, sets fixed length to enable streaming without buffering. Sets after connection will cause an exception.

    Parameters
    contentLength the fixed length of the HTTP request body.
    Throws
    IllegalStateException if already connected or an other mode already set.
    IllegalArgumentException if contentLength is less than zero.

    public static void setFollowRedirects (boolean auto)

    Sets the flag of whether this connection will follow redirects returned by the remote server. This method can only be called with the permission from the security manager.

    Parameters
    auto the value to enable or disable this option.

    public void setInstanceFollowRedirects (boolean followRedirects)

    Sets whether this connection follows redirects.

    Parameters
    followRedirects true if this connection will follows redirects, false otherwise.

    public void setRequestMethod (String method)

    Sets the request command which will be sent to the remote HTTP server. This method can only be called before the connection is made.

    Parameters
    method the string representing the method to be used.
    Throws
    ProtocolException if this is called after connected, or the method is not supported by this HTTP implementation.

    public abstract boolean usingProxy ()

    Returns whether this connection uses a proxy server or not.

    Returns
    • true if this connection passes a proxy server, false otherwise.