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. Its value is returned by the
getInstanceFollowRedirects
method.
Its default value is based on the value of the static followRedirects
at HttpURLConnection construction time.
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,
getHeaderField(0)
returns the status
line, but
getHeaderFieldKey(0)
returns null.
the key for the
n
th
header field,
or
null
if the key does not exist.
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
getHeaderFieldKey
method to iterate through all
the headers in the message.
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.
Parameters:
set
- a
boolean
indicating whether or not
to follow HTTP redirects.
Throws:
SecurityException
- if a security manager exists and its
checkSetFactory
method doesn't
allow the operation.
setInstanceFollowRedirects
public void
setInstanceFollowRedirects
(boolean followRedirects)
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.
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).
Returns:
the HTTP Status-Code, or -1
Throws:
IOException
- if an error occurred connecting to the server.
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).
Returns:
the HTTP response message, or
null
Throws:
IOException
- if an error occurred connecting to the server.
Returns the value of the named field parsed as date.
The result is the number of milliseconds since January 1, 1970 GMT
represented by the named field.
This form of
getHeaderField
exists because some
connection types (e.g.,
http-ng
) have pre-parsed
headers. Classes for that connection type can override this method
and short-circuit the parsing.
the value of the field, parsed as a date. The value of the
Default
argument is returned if the field is
missing or malformed.
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.
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.
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.
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.
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.