| |
|
| java.lang.Object java.net.URLConnection java.net.HttpURLConnection
All known Subclasses: sun.net.www.protocol.http.HttpURLConnection,
HttpURLConnection | abstract public class HttpURLConnection extends URLConnection (Code) | | A URLConnection with support for HTTP-specific features. See
the spec for
details.
Each HttpURLConnection instance is used to make a single request
but the underlying network connection to the HTTP server may be
transparently shared by other instances. Calling the close() methods
on the InputStream or OutputStream of an HttpURLConnection
after a request may free network resources associated with this
instance but has no effect on any shared persistent connection.
Calling the disconnect() method may close the underlying socket
if a persistent connection is otherwise idle at that time.
See Also: java.net.HttpURLConnection.disconnect since: JDK1.1 |
Field Summary | |
final public static int | HTTP_ACCEPTED HTTP Status-Code 202: Accepted. | final public static int | HTTP_BAD_GATEWAY HTTP Status-Code 502: Bad Gateway. | final public static int | HTTP_BAD_METHOD HTTP Status-Code 405: Method Not Allowed. | final public static int | HTTP_BAD_REQUEST HTTP Status-Code 400: Bad Request. | final public static int | HTTP_CLIENT_TIMEOUT HTTP Status-Code 408: Request Time-Out. | final public static int | HTTP_CONFLICT HTTP Status-Code 409: Conflict. | final public static int | HTTP_CREATED HTTP Status-Code 201: Created. | final public static int | HTTP_ENTITY_TOO_LARGE HTTP Status-Code 413: Request Entity Too Large. | final public static int | HTTP_FORBIDDEN HTTP Status-Code 403: Forbidden. | final public static int | HTTP_GATEWAY_TIMEOUT HTTP Status-Code 504: Gateway Timeout. | final public static int | HTTP_GONE HTTP Status-Code 410: Gone. | final public static int | HTTP_INTERNAL_ERROR HTTP Status-Code 500: Internal Server Error. | final public static int | HTTP_LENGTH_REQUIRED HTTP Status-Code 411: Length Required. | final public static int | HTTP_MOVED_PERM HTTP Status-Code 301: Moved Permanently. | final public static int | HTTP_MOVED_TEMP HTTP Status-Code 302: Temporary Redirect. | final public static int | HTTP_MULT_CHOICE HTTP Status-Code 300: Multiple Choices. | final public static int | HTTP_NOT_ACCEPTABLE HTTP Status-Code 406: Not Acceptable. | final public static int | HTTP_NOT_AUTHORITATIVE HTTP Status-Code 203: Non-Authoritative Information. | final public static int | HTTP_NOT_FOUND HTTP Status-Code 404: Not Found. | final public static int | HTTP_NOT_IMPLEMENTED HTTP Status-Code 501: Not Implemented. | final public static int | HTTP_NOT_MODIFIED HTTP Status-Code 304: Not Modified. | final public static int | HTTP_NO_CONTENT HTTP Status-Code 204: No Content. | final public static int | HTTP_OK HTTP Status-Code 200: OK. | final public static int | HTTP_PARTIAL HTTP Status-Code 206: Partial Content. | final public static int | HTTP_PAYMENT_REQUIRED HTTP Status-Code 402: Payment Required. | final public static int | HTTP_PRECON_FAILED HTTP Status-Code 412: Precondition Failed. | final public static int | HTTP_PROXY_AUTH HTTP Status-Code 407: Proxy Authentication Required. | final public static int | HTTP_REQ_TOO_LONG HTTP Status-Code 414: Request-URI Too Large. | final public static int | HTTP_RESET HTTP Status-Code 205: Reset Content. | final public static int | HTTP_SEE_OTHER HTTP Status-Code 303: See Other. | final public static int | HTTP_UNAUTHORIZED HTTP Status-Code 401: Unauthorized. | final public static int | HTTP_UNAVAILABLE HTTP Status-Code 503: Service Unavailable. | final public static int | HTTP_UNSUPPORTED_TYPE HTTP Status-Code 415: Unsupported Media Type. | final public static int | HTTP_USE_PROXY HTTP Status-Code 305: Use Proxy. | final public static int | HTTP_VERSION HTTP Status-Code 505: HTTP Version Not Supported. | protected boolean | instanceFollowRedirects If true , the protocol will automatically follow redirects.
If false , the protocol will not automatically follow
redirects.
This field is set by the setInstanceFollowRedirects
method. | protected String | method The HTTP method (GET,POST,PUT,etc.). | protected int | responseCode An int representing the three digit HTTP Status-Code. | protected String | responseMessage The HTTP response message. |
Method Summary | |
abstract public void | disconnect() Indicates that other requests to the server
are unlikely in the near future. | public InputStream | getErrorStream() Returns the error stream if the connection failed
but the server sent useful data nonetheless. | public static boolean | getFollowRedirects() Returns a boolean indicating
whether or not HTTP redirects (3xx) should
be automatically followed. | public String | getHeaderField(int n) Returns the value for the n th header field. | public long | getHeaderFieldDate(String name, long Default) | public String | getHeaderFieldKey(int n) Returns the key for the n th header field.
Some implementations may treat the 0 th
header field as special, i.e. | public boolean | getInstanceFollowRedirects() Returns the value of this HttpURLConnection 's
instanceFollowRedirects field. | public Permission | getPermission() | public String | getRequestMethod() Get the request method. | public int | getResponseCode() Gets the status code from an HTTP response message.
For example, in the case of the following status lines:
HTTP/1.0 200 OK
HTTP/1.0 401 Unauthorized
It will return 200 and 401 respectively.
Returns -1 if no code can be discerned
from the response (i.e., the response is not valid HTTP).
throws: IOException - if an error occurred connecting to the server. | public String | getResponseMessage() Gets the HTTP response message, if any, returned along with the
response code from a server. | public static void | setFollowRedirects(boolean set) Sets whether HTTP redirects (requests with response code 3xx) should
be automatically followed by this class. | public void | setInstanceFollowRedirects(boolean followRedirects) Sets whether HTTP redirects (requests with response code 3xx) should
be automatically followed by this HttpURLConnection
instance. | public 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 public boolean | usingProxy() Indicates if the connection is going through a proxy. |
HTTP_ACCEPTED | final public static int HTTP_ACCEPTED(Code) | | HTTP Status-Code 202: Accepted.
|
HTTP_BAD_GATEWAY | final public static int HTTP_BAD_GATEWAY(Code) | | HTTP Status-Code 502: Bad Gateway.
|
HTTP_BAD_METHOD | final public static int HTTP_BAD_METHOD(Code) | | HTTP Status-Code 405: Method Not Allowed.
|
HTTP_BAD_REQUEST | final public static int HTTP_BAD_REQUEST(Code) | | HTTP Status-Code 400: Bad Request.
|
HTTP_CLIENT_TIMEOUT | final public static int HTTP_CLIENT_TIMEOUT(Code) | | HTTP Status-Code 408: Request Time-Out.
|
HTTP_CONFLICT | final public static int HTTP_CONFLICT(Code) | | HTTP Status-Code 409: Conflict.
|
HTTP_CREATED | final public static int HTTP_CREATED(Code) | | HTTP Status-Code 201: Created.
|
HTTP_ENTITY_TOO_LARGE | final public static int HTTP_ENTITY_TOO_LARGE(Code) | | HTTP Status-Code 413: Request Entity Too Large.
|
HTTP_FORBIDDEN | final public static int HTTP_FORBIDDEN(Code) | | HTTP Status-Code 403: Forbidden.
|
HTTP_GATEWAY_TIMEOUT | final public static int HTTP_GATEWAY_TIMEOUT(Code) | | HTTP Status-Code 504: Gateway Timeout.
|
HTTP_GONE | final public static int HTTP_GONE(Code) | | HTTP Status-Code 410: Gone.
|
HTTP_INTERNAL_ERROR | final public static int HTTP_INTERNAL_ERROR(Code) | | HTTP Status-Code 500: Internal Server Error.
|
HTTP_LENGTH_REQUIRED | final public static int HTTP_LENGTH_REQUIRED(Code) | | HTTP Status-Code 411: Length Required.
|
HTTP_MOVED_PERM | final public static int HTTP_MOVED_PERM(Code) | | HTTP Status-Code 301: Moved Permanently.
|
HTTP_MOVED_TEMP | final public static int HTTP_MOVED_TEMP(Code) | | HTTP Status-Code 302: Temporary Redirect.
|
HTTP_MULT_CHOICE | final public static int HTTP_MULT_CHOICE(Code) | | HTTP Status-Code 300: Multiple Choices.
|
HTTP_NOT_ACCEPTABLE | final public static int HTTP_NOT_ACCEPTABLE(Code) | | HTTP Status-Code 406: Not Acceptable.
|
HTTP_NOT_AUTHORITATIVE | final public static int HTTP_NOT_AUTHORITATIVE(Code) | | HTTP Status-Code 203: Non-Authoritative Information.
|
HTTP_NOT_FOUND | final public static int HTTP_NOT_FOUND(Code) | | HTTP Status-Code 404: Not Found.
|
HTTP_NOT_IMPLEMENTED | final public static int HTTP_NOT_IMPLEMENTED(Code) | | HTTP Status-Code 501: Not Implemented.
|
HTTP_NOT_MODIFIED | final public static int HTTP_NOT_MODIFIED(Code) | | HTTP Status-Code 304: Not Modified.
|
HTTP_NO_CONTENT | final public static int HTTP_NO_CONTENT(Code) | | HTTP Status-Code 204: No Content.
|
HTTP_OK | final public static int HTTP_OK(Code) | | HTTP Status-Code 200: OK.
|
HTTP_PARTIAL | final public static int HTTP_PARTIAL(Code) | | HTTP Status-Code 206: Partial Content.
|
HTTP_PAYMENT_REQUIRED | final public static int HTTP_PAYMENT_REQUIRED(Code) | | HTTP Status-Code 402: Payment Required.
|
HTTP_PRECON_FAILED | final public static int HTTP_PRECON_FAILED(Code) | | HTTP Status-Code 412: Precondition Failed.
|
HTTP_PROXY_AUTH | final public static int HTTP_PROXY_AUTH(Code) | | HTTP Status-Code 407: Proxy Authentication Required.
|
HTTP_REQ_TOO_LONG | final public static int HTTP_REQ_TOO_LONG(Code) | | HTTP Status-Code 414: Request-URI Too Large.
|
HTTP_RESET | final public static int HTTP_RESET(Code) | | HTTP Status-Code 205: Reset Content.
|
HTTP_SEE_OTHER | final public static int HTTP_SEE_OTHER(Code) | | HTTP Status-Code 303: See Other.
|
HTTP_UNAUTHORIZED | final public static int HTTP_UNAUTHORIZED(Code) | | HTTP Status-Code 401: Unauthorized.
|
HTTP_UNAVAILABLE | final public static int HTTP_UNAVAILABLE(Code) | | HTTP Status-Code 503: Service Unavailable.
|
HTTP_UNSUPPORTED_TYPE | final public static int HTTP_UNSUPPORTED_TYPE(Code) | | HTTP Status-Code 415: Unsupported Media Type.
|
HTTP_USE_PROXY | final public static int HTTP_USE_PROXY(Code) | | HTTP Status-Code 305: Use Proxy.
|
HTTP_VERSION | final public static int HTTP_VERSION(Code) | | HTTP Status-Code 505: HTTP Version Not Supported.
|
method | protected String method(Code) | | The HTTP method (GET,POST,PUT,etc.).
|
responseCode | protected int responseCode(Code) | | An int representing the three digit HTTP Status-Code.
- 1xx: Informational
- 2xx: Success
- 3xx: Redirection
- 4xx: Client Error
- 5xx: Server Error
|
responseMessage | protected String responseMessage(Code) | | The HTTP response message.
|
HttpURLConnection | protected HttpURLConnection(URL u)(Code) | | Constructor for the HttpURLConnection.
Parameters: u - the URL |
disconnect | abstract public void disconnect()(Code) | | Indicates that other requests to the server
are unlikely in the near future. Calling disconnect()
should not imply that this HttpURLConnection
instance can be reused for other requests.
|
getErrorStream | public InputStream getErrorStream()(Code) | | 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
the connection was not connected, or if the server did not have
an error while connecting or if the server had an error but
no error data was sent, this method will return null. This is
the default.
an error stream if any, null if there have been noerrors, the connection is not connected or the server sent nouseful data. |
getFollowRedirects | public static boolean getFollowRedirects()(Code) | | Returns a boolean indicating
whether or not HTTP redirects (3xx) should
be automatically followed.
true if HTTP redirects shouldbe automatically followed, false if not. See Also: HttpURLConnection.setFollowRedirects(boolean) |
getHeaderField | public String getHeaderField(int n)(Code) | | Returns the value for the n th header field.
Some implementations may treat the 0 th
header field as special, i.e. as the status line returned by the HTTP
server.
This method can be used in conjunction with the
HttpURLConnection.getHeaderFieldKey getHeaderFieldKey method to iterate through all
the headers in the message.
Parameters: n - an index, where n>=0. the value of the n th header field,or null if the value does not exist. See Also: java.net.HttpURLConnection.getHeaderFieldKey(int) |
getHeaderFieldDate | public long getHeaderFieldDate(String name, long Default)(Code) | | |
getHeaderFieldKey | public String getHeaderFieldKey(int n)(Code) | | Returns the key for the n th header field.
Some implementations may treat the 0 th
header field as special, i.e. as the status line returned by the HTTP
server. In this case,
HttpURLConnection.getHeaderField(int) getHeaderField(0) returns the status
line, but getHeaderFieldKey(0) returns null.
Parameters: n - an index, where n >=0. the key for the n th header field,or null if the key does not exist. |
getResponseCode | public int getResponseCode() throws IOException(Code) | | Gets the status code from an HTTP response message.
For example, in the case of the following status lines:
HTTP/1.0 200 OK
HTTP/1.0 401 Unauthorized
It will return 200 and 401 respectively.
Returns -1 if no code can be discerned
from the response (i.e., the response is not valid HTTP).
throws: IOException - if an error occurred connecting to the server. the HTTP Status-Code, or -1 |
getResponseMessage | public String getResponseMessage() throws IOException(Code) | | 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. the HTTP response message, or null |
setFollowRedirects | public static void setFollowRedirects(boolean set)(Code) | | 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.
Parameters: set - a boolean indicating whether or notto follow HTTP redirects. exception: SecurityException - if a security manager exists and its checkSetFactory method doesn't allow the operation. See Also: SecurityManager.checkSetFactory See Also: HttpURLConnection.getFollowRedirects() |
setInstanceFollowRedirects | public void setInstanceFollowRedirects(boolean followRedirects)(Code) | | Sets whether HTTP redirects (requests with response code 3xx) should
be automatically followed by this HttpURLConnection
instance.
The default value comes from followRedirects, which defaults to
true.
Parameters: followRedirects - a boolean indicating whether or not to follow HTTP redirects. See Also: java.net.HttpURLConnection.instanceFollowRedirects See Also: HttpURLConnection.getInstanceFollowRedirects |
setRequestMethod | public void setRequestMethod(String method) throws ProtocolException(Code) | | 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.
Parameters: method - the HTTP method exception: ProtocolException - if the method cannot be reset or ifthe requested method isn't valid for HTTP. See Also: HttpURLConnection.getRequestMethod() |
usingProxy | abstract public boolean usingProxy()(Code) | | Indicates if the connection is going through a proxy.
a boolean indicating if the connection isusing a proxy. |
|
|
|