This abstract subclass of {@code URLConnection} defines methods for managing
HTTP connection according to the description given by RFC 2068.
| Field Summary |
|---|
| 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}. |
| 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. |
| 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 |
| Constructor: |
protected HttpURLConnection(URL url) {
super(url);
}
Constructs a new {@code HttpURLConnection} instance pointing to the
resource specified by the {@code url}. |
| Method from java.net.HttpURLConnection Summary: |
|---|
|
disconnect, getErrorStream, getFollowRedirects, getHeaderFieldDate, getInstanceFollowRedirects, getPermission, getRequestMethod, getResponseCode, getResponseMessage, setChunkedStreamingMode, setFixedLengthStreamingMode, setFollowRedirects, setInstanceFollowRedirects, setRequestMethod, usingProxy |
| Methods from java.net.URLConnection: |
|---|
|
addRequestProperty, connect, getAllowUserInteraction, getConnectTimeout, getContent, getContent, getContentEncoding, getContentLength, getContentType, getDate, getDefaultAllowUserInteraction, getDefaultRequestProperty, getDefaultUseCaches, getDoInput, getDoOutput, getExpiration, getFileNameMap, getHeaderField, getHeaderField, getHeaderFieldDate, getHeaderFieldInt, getHeaderFieldKey, getHeaderFields, getIfModifiedSince, getInputStream, getLastModified, getOutputStream, getPermission, getReadTimeout, getRequestProperties, getRequestProperty, getURL, getUseCaches, guessContentTypeFromName, guessContentTypeFromStream, setAllowUserInteraction, setConnectTimeout, setContentHandlerFactory, setDefaultAllowUserInteraction, setDefaultRequestProperty, setDefaultUseCaches, setDoInput, setDoOutput, setFileNameMap, setIfModifiedSince, setReadTimeout, setRequestProperty, setUseCaches, toString |
| Methods from java.lang.Object: |
|---|
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from java.net.HttpURLConnection Detail: |
abstract public void disconnect()
Closes the connection to the HTTP server. |
public InputStream getErrorStream() {
return null;
}
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. |
public static boolean getFollowRedirects() {
return followRedirects;
}
Returns the value of {@code followRedirects} which indicates if this
connection follows a different URL redirected by the server. It is
enabled by default. |
public long getHeaderFieldDate(String field,
long defaultValue) {
return super.getHeaderFieldDate(field, 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. |
public boolean getInstanceFollowRedirects() {
return instanceFollowRedirects;
}
Returns whether this connection follows redirects. |
public Permission getPermission() throws IOException {
int port = url.getPort();
if (port < 0) {
port = 80;
}
return new SocketPermission(url.getHost() + ":" + port, //$NON-NLS-1$
"connect, resolve"); //$NON-NLS-1$
}
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}. |
public String getRequestMethod() {
return method;
}
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. |
public int getResponseCode() throws IOException {
// 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;
}
Returns the response code returned by the remote HTTP server. |
public String getResponseMessage() throws IOException {
if (responseMessage != null) {
return responseMessage;
}
getResponseCode();
return responseMessage;
}
Returns the response message returned by the remote HTTP server. |
public void setChunkedStreamingMode(int chunklen) {
if (super.connected) {
throw new IllegalStateException(Messages.getString("luni.5F")); //$NON-NLS-1$
}
if (0 < = fixedContentLength) {
throw new IllegalStateException(Messages.getString("luni.60")); //$NON-NLS-1$
}
if (0 >= chunklen) {
chunkLength = DEFAULT_CHUNK_LENGTH;
} else {
chunkLength = 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. |
public void setFixedLengthStreamingMode(int contentLength) {
if (super.connected) {
throw new IllegalStateException(Messages.getString("luni.5F")); //$NON-NLS-1$
}
if (0 < chunkLength) {
throw new IllegalStateException(Messages.getString("luni.60")); //$NON-NLS-1$
}
if (0 > contentLength) {
throw new IllegalArgumentException(Messages.getString("luni.61")); //$NON-NLS-1$
}
this.fixedContentLength = 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. |
public static void setFollowRedirects(boolean auto) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkSetFactory();
}
followRedirects = 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. |
public void setInstanceFollowRedirects(boolean followRedirects) {
instanceFollowRedirects = followRedirects;
}
Sets whether this connection follows redirects. |
public void setRequestMethod(String method) throws ProtocolException {
if (connected) {
throw new ProtocolException(Messages.getString("luni.5E")); //$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();
}
Sets the request command which will be sent to the remote HTTP server.
This method can only be called before the connection is made. |
abstract public boolean usingProxy()
Returns whether this connection uses a proxy server or not. |