Java Reference
In-Depth Information
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null)
sb.append(line);
System.out.println(sb.toString());
}
else
System.err.println("cannot
update
book:
"+ht-
tpurlc.getResponseCode());
System.out.println();
}
}
LibraryClient
ispartitionedintoa
main()
methodandfour
do
-prefixedmeth-
odsforperforming
DELETE
,
GET
,
POST
,and
PUT
operations.
main()
invokeseach
“
do
” method to make a request and output a response.
A“
do
”methodfirstinstantiatesthe
URL
class;
doDelete()
and
doGet()
attach
query strings to their URI arguments when these methods are called with nonnull
isbn
arguments. The method then invokes
URL
's
URLConnection openCon-
nection()
method to return a communications link between the application and
URL
instance as an instance of a concrete subclass of the abstract
java.net.URLConnection
class. This concrete subclass is
HttpConnection
because of the
http://
prefix in the argument passed to
URL
's constructor.
HttpURLConnection
's
void setRequestMethod(String method)
is
then called to specify the HTTP verb, which must appear in uppercase with no
whitespace. Depending on the “
do
” method, either
void setDoInput(boolean
doinput)
is called with a
true
argument, or
void setDoInput(boolean
doinput)
and
void setDoOutput(boolean dooutput)
are called with
true
arguments,tosignifythataninputstreamorinputandoutputstreamsarerequired
to communicate with the web service.
Each of
doPost()
and
doPut()
is required to set the
Content-Type
request
headerto
text/xml
,whichitaccomplishesbypassingthisheaderandMIMEtypeto
the
void setRequestProperty(String key, String value)
method.
Forgetting to set the content type to
text/xml
causes the JAX-WS infrastructure to
respond with an internal error response code (500).