Code:
- Using the httpurlconnection object, we can send XML data to the network.
- Stringbuilder xml = new stringbuilder ();
- XML. append ("<? XML version =/"1.0/" encoding =/"UTF-8/"?> ");
- XML. append ("<M1 v = 10000> ");
- XML. append ("<u I = 1 d =/" n73/"> China </u> ");
- XML. append ("</M1> ");
- Byte [] xmlbyte = xml. tostring (). getbytes ("UTF-8 ");
- URL url = new URL ("http: // localhost: 8080/itcast/contanctmanage. do? Method = readxml ");
- Httpurlconnection conn = (httpurlconnection) URL. openconnection ();
- Conn. setconnecttimeout (6*1000 );
- Conn. setdooutput (true); // allow output
- Conn. setusecaches (false); // No cache is used.
- Conn. setrequestmethod ("Post ");
- Conn. setrequestproperty ("connection", "keep-alive"); // maintain a persistent connection
- Conn. setrequestproperty ("charset", "UTF-8 ");
- Conn. setrequestproperty ("Content-Length", String. valueof (xmlbyte. Length ));
- Conn. setrequestproperty ("Content-Type", "text/XML; charset = UTF-8 ");
- Dataoutputstream outstream = new dataoutputstream (conn. getoutputstream ());
- Outstream. Write (xmlbyte); // send XML data
- Outstream. Flush ();
- If (conn. getresponsecode ()! = 200) throw new runtimeexception ("request URL failed ");
- Inputstream is = conn. getinputstream (); // get the returned data
- String result = readasstring (is, "UTF-8 ");
- Outstream. Close ();
216; MSO-Char-wrap: 1; MSO-kinsoku-overflow: 1'> 111%; visibility: hidden "> left: 44.35%; text-align: Left; font-family: wingdings; font-size: 70% "> L