FileDocCategorySizeDatePackage
HttpURLConnection.javaAPI DocAndroid 1.5 API18331Wed May 06 22:41:04 BST 2009java.net

HttpURLConnection

public abstract class HttpURLConnection extends URLConnection
This abstract subclass of {@code URLConnection} defines methods for managing HTTP connection according to the description given by RFC 2068.
see
ContentHandler
see
URL
see
URLConnection
see
URLStreamHandler
since
Android 1.0

Fields Summary
private String[]
methodTokens
protected String
method
The HTTP request method of this {@code HttpURLConnection}. The default value is {@code "GET"}.
protected int
responseCode
The status code of the response obtained from the HTTP request. The default value is {@code -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 boolean
    instanceFollowRedirects
    Flag to define whether the protocol will automatically follow redirects or not. The default value is {@code true}.
    private static boolean
    followRedirects
    protected int
    chunkLength
    If the HTTP chunked encoding is enabled this parameter defines the chunk-length. Default value is {@code -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 {@code -1} that means the fixed-length streaming mode is disabled.
    private static final int
    DEFAULT_CHUNK_LENGTH
    public static final int
    HTTP_ACCEPTED
    Numeric status code, 202: Accepted
    public static final int
    HTTP_BAD_GATEWAY
    Numeric status code, 502: Bad Gateway
    public static final int
    HTTP_BAD_METHOD
    Numeric status code, 405: Bad Method
    public static final int
    HTTP_BAD_REQUEST
    Numeric status code, 400: Bad Request
    public static final int
    HTTP_CLIENT_TIMEOUT
    Numeric status code, 408: Client Timeout
    public static final int
    HTTP_CONFLICT
    Numeric status code, 409: Conflict
    public static final int
    HTTP_CREATED
    Numeric status code, 201: Created
    public static final int
    HTTP_ENTITY_TOO_LARGE
    Numeric status code, 413: Entity too large
    public static final int
    HTTP_FORBIDDEN
    Numeric status code, 403: Forbidden
    public static final int
    HTTP_GATEWAY_TIMEOUT
    Numeric status code, 504: Gateway timeout
    public static final int
    HTTP_GONE
    Numeric status code, 410: Gone
    public static final int
    HTTP_INTERNAL_ERROR
    Numeric status code, 500: Internal error
    public static final int
    HTTP_LENGTH_REQUIRED
    Numeric status code, 411: Length required
    public static final int
    HTTP_MOVED_PERM
    Numeric status code, 301 Moved permanently
    public static final int
    HTTP_MOVED_TEMP
    Numeric status code, 302: Moved temporarily
    public static final int
    HTTP_MULT_CHOICE
    Numeric status code, 300: Multiple choices
    public static final int
    HTTP_NO_CONTENT
    Numeric status code, 204: No content
    public static final int
    HTTP_NOT_ACCEPTABLE
    Numeric status code, 406: Not acceptable
    public static final int
    HTTP_NOT_AUTHORITATIVE
    Numeric status code, 203: Not authoritative
    public static final int
    HTTP_NOT_FOUND
    Numeric status code, 404: Not found
    public static final int
    HTTP_NOT_IMPLEMENTED
    Numeric status code, 501: Not implemented
    public static final int
    HTTP_NOT_MODIFIED
    Numeric status code, 304: Not modified
    public static final int
    HTTP_OK
    Numeric status code, 200: OK
    public static final int
    HTTP_PARTIAL
    Numeric status code, 206: Partial
    public static final int
    HTTP_PAYMENT_REQUIRED
    Numeric status code, 402: Payment required
    public static final int
    HTTP_PRECON_FAILED
    Numeric status code, 412: Precondition failed
    public static final int
    HTTP_PROXY_AUTH
    Numeric status code, 407: Proxy authentication required
    public static final int
    HTTP_REQ_TOO_LONG
    Numeric status code, 414: Request too long
    public static final int
    HTTP_RESET
    Numeric status code, 205: Reset
    public static final int
    HTTP_SEE_OTHER
    Numeric status code, 303: See other
    public static final int
    HTTP_SERVER_ERROR
    Numeric status code, 500: Internal error
    public static final int
    HTTP_USE_PROXY
    Numeric status code, 305: Use proxy
    public static final int
    HTTP_UNAUTHORIZED
    Numeric status code, 401: Unauthorized
    public static final int
    HTTP_UNSUPPORTED_TYPE
    Numeric status code, 415: Unsupported type
    public static final int
    HTTP_UNAVAILABLE
    Numeric status code, 503: Unavailable
    public static final int
    HTTP_VERSION
    Numeric status code, 505: Version not supported
    Constructors Summary
    protected HttpURLConnection(URL url)
    Constructs a new {@code HttpURLConnection} instance pointing to the resource specified by the {@code url}.

    param
    url the URL of this connection.
    see
    URL
    see
    URLConnection
    since
    Android 1.0

    
                                                      
           
            super(url);
        
    Methods Summary
    public abstract voiddisconnect()
    Closes the connection to the HTTP server.

    see
    URLConnection#connect()
    see
    URLConnection#connected
    since
    Android 1.0

    public java.io.InputStreamgetErrorStream()
    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.

    return
    the error input stream returned by the server.
    since
    Android 1.0

            return null;
        
    public static booleangetFollowRedirects()
    Returns the value of {@code followRedirects} which indicates if this connection follows a different URL redirected by the server. It is enabled by default.

    return
    the value of the flag.
    see
    #setFollowRedirects
    since
    Android 1.0

            return followRedirects;
        
    public longgetHeaderFieldDate(java.lang.String field, long defaultValue)
    Returns the date value in milliseconds since {@code 01.01.1970, 00:00h} corresponding to the header field {@code field}. The {@code defaultValue} will be returned if no such field can be found in the response header.

    param
    field the header field name.
    param
    defaultValue the default value to use if the specified header field wont be found.
    return
    the header field represented in milliseconds since January 1, 1970 GMT.
    since
    Android 1.0

            return super.getHeaderFieldDate(field, defaultValue);
        
    public booleangetInstanceFollowRedirects()
    Returns whether this connection follows redirects.

    return
    {@code true} if this connection follows redirects, false otherwise.
    since
    Android 1.0

            return instanceFollowRedirects;
        
    public java.security.PermissiongetPermission()
    Returns the permission object (in this case {@code SocketPermission}) with the host and the port number as the target name and {@code "resolve, connect"} as the action list. If the port number of this URL instance is lower than {@code 0} the port will be set to {@code 80}.

    return
    the permission object required for this connection.
    throws
    IOException if an IO exception occurs during the creation of the permission object.
    since
    Android 1.0

            int port = url.getPort();
            if (port < 0) {
                port = 80;
            }
            return new SocketPermission(url.getHost() + ":" + port, //$NON-NLS-1$
                    "connect, resolve"); //$NON-NLS-1$
        
    public java.lang.StringgetRequestMethod()
    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.

    return
    the request method string.
    see
    #method
    see
    #setRequestMethod
    since
    Android 1.0

            return method;
        
    public intgetResponseCode()
    Returns the response code returned by the remote HTTP server.

    return
    the response code, -1 if no valid response code.
    throws
    IOException if there is an IO error during the retrieval.
    see
    #getResponseMessage
    since
    Android 1.0

            // Call getInputStream() first since getHeaderField() doesn't return
            // exceptions
            getInputStream();
            String response = getHeaderField(0);
            if (response == null) {
                return -1;
            }
            response = response.trim();
            int mark = response.indexOf(" ") + 1; //$NON-NLS-1$
            if (mark == 0) {
                return -1;
            }
            int last = mark + 3;
            if (last > response.length()) {
                last = response.length();
            }
            responseCode = Integer.parseInt(response.substring(mark, last));
            if (last + 1 <= response.length()) {
                responseMessage = response.substring(last + 1);
            }
            return responseCode;
        
    public java.lang.StringgetResponseMessage()
    Returns the response message returned by the remote HTTP server.

    return
    the response message. {@code null} if no such response exists.
    throws
    IOException if there is an error during the retrieval.
    see
    #getResponseCode()
    since
    Android 1.0

            if (responseMessage != null) {
                return responseMessage;
            }
            getResponseCode();
            return responseMessage;
        
    public voidsetChunkedStreamingMode(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.

    see
    #setFixedLengthStreamingMode
    param
    chunklen the length of a chunk.
    throws
    IllegalStateException if already connected or an other mode already set.
    since
    Android 1.0

            if (super.connected) {
                throw new IllegalStateException(Msg.getString("K0079")); //$NON-NLS-1$
            }
            if (0 <= fixedContentLength) {
                throw new IllegalStateException(Msg.getString("KA003")); //$NON-NLS-1$
            }
            if (0 >= chunklen) {
                chunkLength = DEFAULT_CHUNK_LENGTH;
            } else {
                chunkLength = chunklen;
            }
        
    public voidsetFixedLengthStreamingMode(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.

    see
    #setChunkedStreamingMode
    param
    contentLength the fixed length of the HTTP request body.
    throws
    IllegalStateException if already connected or an other mode already set.
    throws
    IllegalArgumentException if {@code contentLength} is less than zero.
    since
    Android 1.0

            if (super.connected) {
                throw new IllegalStateException(Msg.getString("K0079")); //$NON-NLS-1$
            }
            if (0 < chunkLength) {
                throw new IllegalStateException(Msg.getString("KA003")); //$NON-NLS-1$
            }
            if (0 > contentLength) {
                throw new IllegalArgumentException(Msg.getString("K0051")); //$NON-NLS-1$
            }
            this.fixedContentLength = contentLength;
        
    public static voidsetFollowRedirects(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.

    param
    auto the value to enable or disable this option.
    see
    SecurityManager#checkSetFactory()
    since
    Android 1.0

            SecurityManager security = System.getSecurityManager();
            if (security != null) {
                security.checkSetFactory();
            }
            followRedirects = auto;
        
    public voidsetInstanceFollowRedirects(boolean followRedirects)
    Sets whether this connection follows redirects.

    param
    followRedirects {@code true} if this connection will follows redirects, false otherwise.
    since
    Android 1.0

            instanceFollowRedirects = followRedirects;
        
    public voidsetRequestMethod(java.lang.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.

    param
    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.
    see
    #getRequestMethod()
    see
    #method
    since
    Android 1.0

            if (connected) {
                throw new ProtocolException(Msg.getString("K0037")); //$NON-NLS-1$
            }
            for (int i = 0; i < methodTokens.length; i++) {
                if (methodTokens[i].equals(method)) {
                    // if there is a supported method that matches the desired
                    // method, then set the current method and return
                    this.method = methodTokens[i];
                    return;
                }
            }
            // if none matches, then throw ProtocolException
            throw new ProtocolException();
        
    public abstract booleanusingProxy()
    Returns whether this connection uses a proxy server or not.

    return
    {@code true} if this connection passes a proxy server, false otherwise.
    since
    Android 1.0