站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > JDK 5 Documentation v1.2.2, Java 2 SDK 英文文档

Java(TM) 2 Platform, Standard Edition, v1.2.2 API Specification: Class HttpURLConnection - JDK 5 Documentation v1.2.2, Java 2 SDK 英文文档

JavaTM 2 Platform
Standard Edition

java.net
Class HttpURLConnection

java.lang.Object
  |
  +--java.net.URLConnection
        |
        +--java.net.HttpURLConnection

public abstract class HttpURLConnection
extends URLConnection

A URLConnection with support for HTTP-specific features. See the spec for details.

Since:
JDK1.1

Field Summary
static int HTTP_ACCEPTED
           
static int HTTP_BAD_GATEWAY
           
static int HTTP_BAD_METHOD
           
static int HTTP_BAD_REQUEST
          4XX: client error
static int HTTP_CLIENT_TIMEOUT
           
static int HTTP_CONFLICT
           
static int HTTP_CREATED
           
static int HTTP_ENTITY_TOO_LARGE
           
static int HTTP_FORBIDDEN
           
static int HTTP_GATEWAY_TIMEOUT
           
static int HTTP_GONE
           
static int HTTP_INTERNAL_ERROR
           
static int HTTP_LENGTH_REQUIRED
           
static int HTTP_MOVED_PERM
           
static int HTTP_MOVED_TEMP
           
static int HTTP_MULT_CHOICE
          3XX: relocation/redirect
static int HTTP_NO_CONTENT
           
static int HTTP_NOT_ACCEPTABLE
           
static int HTTP_NOT_AUTHORITATIVE
           
static int HTTP_NOT_FOUND
           
static int HTTP_NOT_MODIFIED
           
static int HTTP_OK
          2XX: generally "OK"
static int HTTP_PARTIAL
           
static int HTTP_PAYMENT_REQUIRED
           
static int HTTP_PRECON_FAILED
           
static int HTTP_PROXY_AUTH
           
static int HTTP_REQ_TOO_LONG
           
static int HTTP_RESET
           
static int HTTP_SEE_OTHER
           
static int HTTP_SERVER_ERROR
          5XX: server error
static int HTTP_UNAUTHORIZED
           
static int HTTP_UNAVAILABLE
           
static int HTTP_UNSUPPORTED_TYPE
           
static int HTTP_USE_PROXY
           
static int HTTP_VERSION
           
protected  String method
           
protected  int responseCode
           
protected  String responseMessage
           
 
Fields inherited from class java.net.URLConnection
allowUserInteraction, connected, doInput, doOutput, ifModifiedSince, url, useCaches
 
Constructor Summary
protected HttpURLConnection(URL u)
          Constructor for the URLStreamHandler.
 
Method Summary
abstract  void disconnect()
          Close the connection to the server.
 InputStream getErrorStream()
          Returns the error stream if the connection failed but the server sent useful data nonetheless.
static boolean getFollowRedirects()
           
 Permission getPermission()
          Returns a permission object representing the permission necessary to make the connection represented by this object.
 String getRequestMethod()
          Get the request method.
 int getResponseCode()
          Gets HTTP response status.
 String getResponseMessage()
          Gets the HTTP response message, if any, returned along with the response code from a server.
static void setFollowRedirects(boolean set)
          Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this class.
 void setRequestMethod(String method)
          Set the method for the URL request, one of: GET POST HEAD OPTIONS PUT DELETE TRACE are legal, subject to protocol restrictions.
abstract  boolean usingProxy()
          Indicates if the connection is going through a proxy.
 
Methods inherited from class java.net.URLConnection
connect, getAllowUserInteraction, getContent, getContentEncoding, getContentLength, getContentType, getDate, getDefaultAllowUserInteraction, getDefaultRequestProperty, getDefaultUseCaches, getDoInput, getDoOutput, getExpiration, getFileNameMap, getHeaderField, getHeaderField, getHeaderFieldDate, getHeaderFieldInt, getHeaderFieldKey, getIfModifiedSince, getInputStream, getLastModified, getOutputStream, getRequestProperty, getURL, getUseCaches, guessContentTypeFromName, guessContentTypeFromStream, setAllowUserInteraction, setContentHandlerFactory, setDefaultAllowUserInteraction, setDefaultRequestProperty, setDefaultUseCaches, setDoInput, setDoOutput, setFileNameMap, setIfModifiedSince, setRequestProperty, setUseCaches, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

method

protected String method

responseCode

protected int responseCode

responseMessage

protected String responseMessage

HTTP_OK

public static final int HTTP_OK
2XX: generally "OK"

HTTP_CREATED

public static final int HTTP_CREATED

HTTP_ACCEPTED

public static final int HTTP_ACCEPTED

HTTP_NOT_AUTHORITATIVE

public static final int HTTP_NOT_AUTHORITATIVE

HTTP_NO_CONTENT

public static final int HTTP_NO_CONTENT

HTTP_RESET

public static final int HTTP_RESET

HTTP_PARTIAL

public static final int HTTP_PARTIAL

HTTP_MULT_CHOICE

public static final int HTTP_MULT_CHOICE
3XX: relocation/redirect

HTTP_MOVED_PERM

public static final int HTTP_MOVED_PERM

HTTP_MOVED_TEMP

public static final int HTTP_MOVED_TEMP

HTTP_SEE_OTHER

public static final int HTTP_SEE_OTHER

HTTP_NOT_MODIFIED

public static final int HTTP_NOT_MODIFIED

HTTP_USE_PROXY

public static final int HTTP_USE_PROXY

HTTP_BAD_REQUEST

public static final int HTTP_BAD_REQUEST
4XX: client error

HTTP_UNAUTHORIZED

public static final int HTTP_UNAUTHORIZED

HTTP_PAYMENT_REQUIRED

public static final int HTTP_PAYMENT_REQUIRED

HTTP_FORBIDDEN

public static final int HTTP_FORBIDDEN

HTTP_NOT_FOUND

public static final int HTTP_NOT_FOUND

HTTP_BAD_METHOD

public static final int HTTP_BAD_METHOD

HTTP_NOT_ACCEPTABLE

public static final int HTTP_NOT_ACCEPTABLE

HTTP_PROXY_AUTH

public static final int HTTP_PROXY_AUTH

HTTP_CLIENT_TIMEOUT

public static final int HTTP_CLIENT_TIMEOUT

HTTP_CONFLICT

public static final int HTTP_CONFLICT

HTTP_GONE

public static final int HTTP_GONE

HTTP_LENGTH_REQUIRED

public static final int HTTP_LENGTH_REQUIRED

HTTP_PRECON_FAILED

public static final int HTTP_PRECON_FAILED

HTTP_ENTITY_TOO_LARGE

public static final int HTTP_ENTITY_TOO_LARGE

HTTP_REQ_TOO_LONG

public static final int HTTP_REQ_TOO_LONG

HTTP_UNSUPPORTED_TYPE

public static final int HTTP_UNSUPPORTED_TYPE

HTTP_SERVER_ERROR

public static final int HTTP_SERVER_ERROR
5XX: server error

HTTP_INTERNAL_ERROR

public static final int HTTP_INTERNAL_ERROR

HTTP_BAD_GATEWAY

public static final int HTTP_BAD_GATEWAY

HTTP_UNAVAILABLE

public static final int HTTP_UNAVAILABLE

HTTP_GATEWAY_TIMEOUT

public static final int HTTP_GATEWAY_TIMEOUT

HTTP_VERSION

public static final int HTTP_VERSION
Constructor Detail

HttpURLConnection

protected HttpURLConnection(URL u)
Constructor for the URLStreamHandler.
Method Detail

setFollowRedirects

public static void setFollowRedirects(boolean set)
Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this class. True by default. Applets cannot change this variable.

If there is a security manager, this method first calls the security manager's checkSetFactory method to ensure the operation is allowed. This could result in a SecurityException.

Throws:
SecurityException - if a security manager exists and its checkSetFactory method doesn't allow the operation.
See Also:
SecurityManager.checkSetFactory()

getFollowRedirects

public static boolean getFollowRedirects()

setRequestMethod

public void setRequestMethod(String method)
                      throws ProtocolException
Set the method for the URL request, one of:
  • GET
  • POST
  • HEAD
  • OPTIONS
  • PUT
  • DELETE
  • TRACE
are legal, subject to protocol restrictions. The default method is GET.
Throws:
ProtocolException - if the method cannot be reset or if the requested method isn't valid for HTTP.

getRequestMethod

public String getRequestMethod()
Get the request method.

getResponseCode

public int getResponseCode()
                    throws IOException
Gets HTTP response status. From responses like:
 HTTP/1.0 200 OK
 HTTP/1.0 401 Unauthorized
 
Extracts the ints 200 and 401 respectively. Returns -1 if none can be discerned from the response (i.e., the response is not valid HTTP).
Throws:
IOException - if an error occurred connecting to the server.

getResponseMessage

public String getResponseMessage()
                          throws IOException
Gets the HTTP response message, if any, returned along with the response code from a server. From responses like:
 HTTP/1.0 200 OK
 HTTP/1.0 404 Not Found
 
Extracts the Strings "OK" and "Not Found" respectively. Returns null if none could be discerned from the responses (the result was not valid HTTP).
Throws:
IOException - if an error occurred connecting to the server.

disconnect

public abstract void disconnect()
Close the connection to the server.

usingProxy

public abstract boolean usingProxy()
Indicates if the connection is going through a proxy.

getPermission

public Permission getPermission()
                         throws IOException
Description copied from class: URLConnection
Returns a permission object representing the permission necessary to make the connection represented by this object. This method returns null if no permission is required to make the connection. By default, this method returns java.security.AllPermission. Subclasses should override this method and return the permission that best represents the permission required to make a a connection to the URL. For example, a URLConnection representing a file: URL would return a java.io.FilePermission object.

The permission returned may dependent upon the state of the connection. For example, the permission before connecting may be different from that after connecting. For example, an HTTP sever, say foo.com, may redirect the connection to a different host, say bar.com. Before connecting the permission returned by the connection will represent the permission needed to connect to foo.com, while the permission returned after connecting will be to bar.com.

Permissions are generally used for two purposes: to protect caches of objects obtained through URLConnections, and to check the right of a recipient to learn about a particular URL. In the first case, the permission should be obtained after the object has been obtained. For example, in an HTTP connection, this will represent the permission to connect to the host from which the data was ultimately fetched. In the second case, the permission should be obtained and tested before connecting.

Overrides:
getPermission in class URLConnection
Tags copied from class: URLConnection
Returns:
the permission object representing the permission necessary to make the connection represented by this URLConnection.
Throws:
IOException - if the computation of the permission requires network or file I/O and an exception occurs while computing it.

getErrorStream

public InputStream getErrorStream()
Returns the error stream if the connection failed but the server sent useful data nonetheless. The typical example is when an HTTP server responds with a 404, which will cause a FileNotFoundException to be thrown in connect, but the server sent an HTML help page with suggestions as to what to do.

This method will not cause a connection to be initiated. If there the connection was not connected, or if the server did not have an error while connecting or if the server did have an error but there no error data was sent, this method will return null. This is the default.

Returns:
an error stream if any, null if there have been no errors, the connection is not connected or the server sent no useful data.

JavaTM 2 Platform
Standard Edition

Submit a bug or feature
Java, Java 2D, and JDBC are a trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1999 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.