Java Reference
In-Depth Information
if you want to retry authorization—then
getInputStream()
will throw a
ProtocolEx
ception
.
Figure 7-1. An authentication dialog
The static methods
getDefaultAllowUserInteraction()
and
setDefaultAllowUser
Interaction()
determine the default behavior for
URLConnection
objects that have not
set
allowUserInteraction
explicitly. Because the
allowUserInteraction
field is
static
(i.e., a class variable instead of an instance variable), setting it changes the default
behavior for all instances of the
URLConnection
class that are created after
setDefaul
tAllowUserInteraction()
is called.
For instance, the following code fragment checks to see whether user interaction is
allowed by default with
getDefaultAllowUserInteraction()
. If user interaction is not
allowed by default, the code uses
setDefaultAllowUserInteraction()
to make allow‐
ing user interaction the default behavior:
if
(!
URLConnection
.
getDefaultAllowUserInteraction
())
{
URLConnection
.
setDefaultAllowUserInteraction
(
true
);
}
protected boolean doInput
A
URLConnection
can be used for reading from a server, writing to a server, or both.
The protected boolean field
doInput
is
true
if the
URLConnection
can be used for read‐
ing,
false
if it cannot be. The default is
true
. To access this protected variable, use the
public
getDoInput()
and
setDoInput()
methods:
public
void
setDoInput
(
boolean
doInput
)
public
boolean
getDoInput
()
For example:
try
{
URL
u
=
new
URL
(
"http://www.oreilly.com"
);
URLConnection
uc
=
u
.
openConnection
();
if
(!
uc
.
getDoInput
())
{
uc
.
setDoInput
(
true
);
}