GeezerVison is showing docs.oracle.com — URLConnection (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.net
Class URLConnection
-
-
Field Summary
Fields
| Modifier and Type |
Field |
Description |
protected boolean |
allowUserInteraction |
If true, this URL is being examined in
a context in which it makes sense to allow user interactions such
as popping up an authentication dialog.
|
protected boolean |
connected |
If false, this connection object has not created a
communications link to the specified URL.
|
protected boolean |
doInput |
This variable is set by the setDoInput method.
|
protected boolean |
doOutput |
This variable is set by the setDoOutput method.
|
protected long |
ifModifiedSince |
Some protocols support skipping the fetching of the object unless
the object has been modified more recently than a certain time.
|
protected URL |
url |
The URL represents the remote object on the World Wide Web to
which this connection is opened.
|
protected boolean |
useCaches |
If true, the protocol is allowed to use caching
whenever it can.
|
-
Constructor Summary
Constructors
| Modifier |
Constructor |
Description |
protected |
URLConnection(URL url) |
Constructs a URL connection to the specified URL.
|
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods
| Modifier and Type |
Method |
Description |
void |
addRequestProperty(String key,
String value) |
Adds a general request property specified by a
key-value pair.
|
abstract void |
connect() |
Opens a communications link to the resource referenced by this
URL, if such a connection has not already been established.
|
boolean |
getAllowUserInteraction() |
Returns the value of the allowUserInteraction field for
this object.
|
int |
getConnectTimeout() |
Returns setting for connect timeout.
|
Object |
getContent() |
Retrieves the contents of this URL connection.
|
Object |
getContent(Class[] classes) |
Retrieves the contents of this URL connection.
|
String |
getContentEncoding() |
Returns the value of the content-encoding header field.
|
int |
getContentLength() |
Returns the value of the content-length header field.
|
long |
getContentLengthLong() |
Returns the value of the content-length header field as a
long.
|
String |
getContentType() |
Returns the value of the content-type header field.
|
long |
getDate() |
Returns the value of the date header field.
|
static boolean |
getDefaultAllowUserInteraction() |
Returns the default value of the allowUserInteraction
field.
|
static String |
getDefaultRequestProperty(String key) |
Deprecated.
The instance specific getRequestProperty method
should be used after an appropriate instance of URLConnection
is obtained.
|
boolean |
getDefaultUseCaches() |
Returns the default value of a URLConnection's
useCaches flag.
|
boolean |
getDoInput() |
Returns the value of this URLConnection's
doInput flag.
|
boolean |
getDoOutput() |
Returns the value of this URLConnection's
doOutput flag.
|
long |
getExpiration() |
Returns the value of the expires header field.
|
static FileNameMap |
getFileNameMap() |
Loads filename map (a mimetable) from a data file.
|
String |
getHeaderField(int n) |
Returns the value for the nth header field.
|
String |
getHeaderField(String name) |
Returns the value of the named header field.
|
long |
getHeaderFieldDate(String name,
long Default) |
Returns the value of the named field parsed as date.
|
int |
getHeaderFieldInt(String name,
int Default) |
Returns the value of the named field parsed as a number.
|
String |
getHeaderFieldKey(int n) |
Returns the key for the nth header field.
|
long |
getHeaderFieldLong(String name,
long Default) |
Returns the value of the named field parsed as a number.
|
Map<String,List<String>> |
getHeaderFields() |
Returns an unmodifiable Map of the header fields.
|
long |
getIfModifiedSince() |
Returns the value of this object's ifModifiedSince field.
|
InputStream |
getInputStream() |
Returns an input stream that reads from this open connection.
|
long |
getLastModified() |
Returns the value of the last-modified header field.
|
OutputStream |
getOutputStream() |
Returns an output stream that writes to this connection.
|
Permission |
getPermission() |
Returns a permission object representing the permission
necessary to make the connection represented by this
object.
|
int |
getReadTimeout() |
Returns setting for read timeout.
|
Map<String,List<String>> |
getRequestProperties() |
Returns an unmodifiable Map of general request
properties for this connection.
|
String |
getRequestProperty(String key) |
Returns the value of the named general request property for this
connection.
|
URL |
getURL() |
Returns the value of this URLConnection's URL
field.
|
boolean |
getUseCaches() |
Returns the value of this URLConnection's
useCaches field.
|
static String |
guessContentTypeFromName(String fname) |
Tries to determine the content type of an object, based
on the specified "file" component of a URL.
|
static String |
guessContentTypeFromStream(InputStream is) |
Tries to determine the type of an input stream based on the
characters at the beginning of the input stream.
|
void |
setAllowUserInteraction(boolean allowuserinteraction) |
Set the value of the allowUserInteraction field of
this URLConnection.
|
void |
setConnectTimeout(int timeout) |
Sets a specified timeout value, in milliseconds, to be used
when opening a communications link to the resource referenced
by this URLConnection.
|
static void |
setContentHandlerFactory(ContentHandlerFactory fac) |
Sets the ContentHandlerFactory of an
application.
|
static void |
setDefaultAllowUserInteraction(boolean defaultallowuserinteraction) |
Sets the default value of the
allowUserInteraction field for all future
URLConnection objects to the specified value.
|
static void |
setDefaultRequestProperty(String key,
String value) |
Deprecated.
The instance specific setRequestProperty method
should be used after an appropriate instance of URLConnection
is obtained. Invoking this method will have no effect.
|
void |
setDefaultUseCaches(boolean defaultusecaches) |
Sets the default value of the useCaches field to the
specified value.
|
void |
setDoInput(boolean doinput) |
Sets the value of the doInput field for this
URLConnection to the specified value.
|
void |
setDoOutput(boolean dooutput) |
Sets the value of the doOutput field for this
URLConnection to the specified value.
|
static void |
setFileNameMap(FileNameMap map) |
Sets the FileNameMap.
|
void |
setIfModifiedSince(long ifmodifiedsince) |
Sets the value of the ifModifiedSince field of
this URLConnection to the specified value.
|
void |
setReadTimeout(int timeout) |
Sets the read timeout to a specified timeout, in
milliseconds.
|
void |
setRequestProperty(String key,
String value) |
Sets the general request property.
|
void |
setUseCaches(boolean usecaches) |
Sets the value of the useCaches field of this
URLConnection to the specified value.
|
String |
toString() |
Returns a String representation of this URL connection.
|
-
-
Field Detail
-
url
protected URL url
The URL represents the remote object on the World Wide Web to
which this connection is opened.
The value of this field can be accessed by the
getURL method.
The default value of this variable is the value of the URL
argument in the URLConnection constructor.
- See Also:
getURL(),
url
-
connected
protected boolean connected
If false, this connection object has not created a
communications link to the specified URL. If true,
the communications link has been established.
-
Method Detail
-
getFileNameMap
public static FileNameMap getFileNameMap()
Loads filename map (a mimetable) from a data file. It will
first try to load the user-specific table, defined
by "content.types.user.table" property. If that fails,
it tries to load the default built-in table.
- Returns:
- the FileNameMap
- Since:
- 1.2
- See Also:
setFileNameMap(java.net.FileNameMap)
-
getReadTimeout
public int getReadTimeout()
Returns setting for read timeout. 0 return implies that the
option is disabled (i.e., timeout of infinity).
- Returns:
- an
int that indicates the read timeout
value in milliseconds
- Since:
- 1.5
- See Also:
setReadTimeout(int),
InputStream.read()
-
getLastModified
public long getLastModified()
Returns the value of the last-modified header field.
The result is the number of milliseconds since January 1, 1970 GMT.
- Returns:
- the date the resource referenced by this
URLConnection was last modified, or 0 if not known.
- See Also:
getHeaderField(java.lang.String)
-
getHeaderFields
public Map<String,List<String>> getHeaderFields()
Returns an unmodifiable Map of the header fields.
The Map keys are Strings that represent the
response-header field names. Each Map value is an
unmodifiable List of Strings that represents
the corresponding field values.
- Returns:
- a Map of header fields
- Since:
- 1.4
-
getInputStream
public InputStream getInputStream()
throws IOException
Returns an input stream that reads from this open connection.
A SocketTimeoutException can be thrown when reading from the
returned input stream if the read timeout expires before data
is available for read.
- Returns:
- an input stream that reads from this open connection.
- Throws:
IOException - if an I/O error occurs while
creating the input stream.
UnknownServiceException - if the protocol does not support
input.
- See Also:
setReadTimeout(int),
getReadTimeout()
-
setDefaultAllowUserInteraction
public static void setDefaultAllowUserInteraction(boolean defaultallowuserinteraction)
Sets the default value of the
allowUserInteraction field for all future
URLConnection objects to the specified value.
- Parameters:
defaultallowuserinteraction - the new value.
- See Also:
getDefaultAllowUserInteraction()
-
addRequestProperty
public void addRequestProperty(String key,
String value)
Adds a general request property specified by a
key-value pair. This method will not overwrite
existing values associated with the same key.
- Parameters:
key - the keyword by which the request is known
(e.g., "Accept").
value - the value associated with it.
- Throws:
IllegalStateException - if already connected
NullPointerException - if key is null
- Since:
- 1.4
- See Also:
getRequestProperties()
-
getRequestProperties
public Map<String,List<String>> getRequestProperties()
Returns an unmodifiable Map of general request
properties for this connection. The Map keys
are Strings that represent the request-header
field names. Each Map value is a unmodifiable List
of Strings that represents the corresponding
field values.
- Returns:
- a Map of the general request properties for this connection.
- Throws:
IllegalStateException - if already connected
- Since:
- 1.4
-
setDefaultRequestProperty
@Deprecated
public static void setDefaultRequestProperty(String key,
String value)
Deprecated. The instance specific setRequestProperty method
should be used after an appropriate instance of URLConnection
is obtained. Invoking this method will have no effect.
Sets the default value of a general request property. When a
URLConnection is created, it is initialized with
these properties.
- Parameters:
key - the keyword by which the request is known
(e.g., "Accept").
value - the value associated with the key.
- See Also:
setRequestProperty(java.lang.String,java.lang.String),
getDefaultRequestProperty(java.lang.String)
-
getDefaultRequestProperty
@Deprecated
public static String getDefaultRequestProperty(String key)
Deprecated. The instance specific getRequestProperty method
should be used after an appropriate instance of URLConnection
is obtained.
Returns the value of the default request property. Default request
properties are set for every connection.
- Parameters:
key - the keyword by which the request is known (e.g., "Accept").
- Returns:
- the value of the default request property
for the specified key.
- See Also:
getRequestProperty(java.lang.String),
setDefaultRequestProperty(java.lang.String, java.lang.String)
-
guessContentTypeFromName
public static String guessContentTypeFromName(String fname)
Tries to determine the content type of an object, based
on the specified "file" component of a URL.
This is a convenience method that can be used by
subclasses that override the getContentType method.
- Parameters:
fname - a filename.
- Returns:
- a guess as to what the content type of the object is,
based upon its file name.
- See Also:
getContentType()
Java™ Platform Standard Ed. 8
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|