Java Reference
In-Depth Information
// read from the connection...
}
catch
(
IOException
ex
)
{
System
.
err
.
println
(
ex
);
}
protected boolean doOutput
Programs can use a
URLConnection
to send output back to the server. For example, a
program that needs to send data to the server using the POST method could do so by
getting an output stream from a
URLConnection
. The protected boolean field
doOutput
is
true
if the
URLConnection
can be used for writing,
false
if it cannot be; it is
false
by default. To access this protected variable, use the
getDoOutput()
and
setDoOut
put()
methods:
public
void
setDoOutput
(
boolean
dooutput
)
public
boolean
getDoOutput
()
For example:
try
{
URL
u
=
new
URL
(
"http://www.oreilly.com"
);
URLConnection
uc
=
u
.
openConnection
();
if
(!
uc
.
getDoOutput
())
{
uc
.
setDoOutput
(
true
);
}
// write to the connection...
}
catch
(
IOException
ex
)
{
System
.
err
.
println
(
ex
);
}
When you set
doOutput
to true for an
http
URL, the request method is changed from
GET to POST. We'll explore this in more detail later in
“Writing Data to a Server” on
page 218
.
protected boolean ifModifiedSince
Many clients, especially web browsers and proxies, keep caches of previously retrieved
documents. If the user asks for the same document again, it can be retrieved from the
cache. However, it may have changed on the server since it was last retrieved. The only
way to tell is to ask the server. Clients can include an If-Modified-Since in the client
request HTTP header. This header includes a date and time. If the document has
changed since that time, the server should send it. Otherwise, it should not. Typically,
this time is the last time the client fetched the document. For example, this client request
says the document should be returned only if it has changed since 7:22:07 A.M., October
31, 2014, Greenwich Mean Time:
GET / HTTP/1.1
Host: login.ibiblio.org:56452
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2