Java Reference
In-Depth Information
public
static
boolean
getDefaultAllowUserInteraction
()
public
static
FileNameMap
getFileNameMap
()
public
static
void
setFileNameMap
(
FileNameMap
map
)
Unlike the instance methods, these methods can be invoked at any time. The new de‐
faults will apply only to
URLConnection
objects constructed after the new default values
are set.
protected URL url
The
url
field specifies the URL that this
URLConnection
connects to. The constructor
sets it when the
URLConnection
is created and it should not change thereafter. You can
retrieve the value by calling the
getURL()
method.
Example 7-12
opens a
URLConnec
tion
to
http://www.oreilly.com/
, gets the
URL
of that connection, and prints it.
Example 7-12. Print the URL of a URLConnection to http://www.oreilly.com/
import
java.io.*
;
import
java.net.*
;
public
class
URLPrinter
{
public
static
void
main
(
String
[]
args
)
{
try
{
URL
u
=
new
URL
(
"http://www.oreilly.com/"
);
URLConnection
uc
=
u
.
openConnection
();
System
.
out
.
println
(
uc
.
getURL
());
}
catch
(
IOException
ex
)
{
System
.
err
.
println
(
ex
);
}
}
}
Here's the result, which should be no great surprise. The URL that is printed is the one
used to create the
URLConnection
.
%
java URLPrinter
http://www.oreilly.com/
protected boolean connected
The boolean field
connected
is
true
if the connection is open and
false
if it's closed.
Because the connection has not yet been opened when a new
URLConnection
object is
created, its initial value is
false
. This variable can be accessed only by instances of
java.net.URLConnection
and its subclasses.
There are no methods that directly read or change the value of
connected
. However,
any method that causes the
URLConnection
to connect should set this variable to true,
including
connect()
,
getInputStream()
, and
getOutputStream()
. Any method that