Java Reference
In-Depth Information
•
OutputStream getOutputStream()
returns an output stream that
writes to this open connection.
•
void setDoInput(boolean doinput)
specifies that this
URLCon-
nection
object supports (pass
true
to
doinput
) or doesn't support (pass
false
to
doinput
)input.Because
true
isthedefault,youwouldonlypass
true
tothismethodtodocumentyourintentiontoperforminput(asIdemon-
strate in
Chapter 11
)
.
•
void setDoOutput(boolean dooutput)
specifiesthatthis
URLCon-
nection
objectsupports(pass
true
to
dooutput
)ordoesn'tsupport(pass
false
to
dooutput
)output.Becausefalseisthedefault,youmustcallthis
method before you can perform output (as demonstrated in
Chapter 11
)
.
•
void setRequestProperty(String key, String value)
setsa
request property (e.g., HTTP's
accept
property). When a key already exists,
its value is overwritten with the specified value.
The following example shows you how to obtain a
URLConnection
object from
a
URL
object referenced by precreated variable
url
, set its
dooutput
property, and
obtain an output stream for writing to the resource:
URLConnection urlc = url.openConnection();
urlc.setDoOutput(true);
OutputStream os = urlc.getOutputStream();
URLConnection
is subclassed by
java.net.HttpURLConnection
and
java.net.JarURLConnection
. These classes declare constants and/or methods
thatarespecifictoworkingwiththeHTTPprotocolorinteracting withJAR-basedre-
sources.
Note
For brevity, I refer you to the JDK documentation on
URLConnection
,
tpURLConnection
examples for more information.
URLEncoder and URLDecoder
HyperTextMarkupLanguage(HTML)letsyouintroduceformsintowebpagesthatso-
licit information from page visitors. After filling out a form's fields, the visitor clicks
theform'sSubmitbutton(whichoftenhasadifferentlabel)andtheformcontent(field
names and values) is sent to some server program.
Before sending the form content to the server program, a web browser encodes this
data by replacing spaces and other URL-illegal characters, and sets the content's Mul-