Send XML data to the Internet

Source: Internet
Author: User
Code:
  1. Using the httpurlconnection object, we can send XML data to the network.
  2. Stringbuilder xml = new stringbuilder ();
  3. XML. append ("<? XML version =/"1.0/" encoding =/"UTF-8/"?> ");
  4. XML. append ("<M1 v = 10000> ");
  5. XML. append ("<u I = 1 d =/" n73/"> China </u> ");
  6. XML. append ("</M1> ");
  7. Byte [] xmlbyte = xml. tostring (). getbytes ("UTF-8 ");
  8. URL url = new URL ("http: // localhost: 8080/itcast/contanctmanage. do? Method = readxml ");
  9. Httpurlconnection conn = (httpurlconnection) URL. openconnection ();
  10. Conn. setconnecttimeout (6*1000 );
  11. Conn. setdooutput (true); // allow output
  12. Conn. setusecaches (false); // No cache is used.
  13. Conn. setrequestmethod ("Post ");
  14. Conn. setrequestproperty ("connection", "keep-alive"); // maintain a persistent connection
  15. Conn. setrequestproperty ("charset", "UTF-8 ");
  16. Conn. setrequestproperty ("Content-Length", String. valueof (xmlbyte. Length ));
  17. Conn. setrequestproperty ("Content-Type", "text/XML; charset = UTF-8 ");
  18. Dataoutputstream outstream = new dataoutputstream (conn. getoutputstream ());
  19. Outstream. Write (xmlbyte); // send XML data
  20. Outstream. Flush ();
  21. If (conn. getresponsecode ()! = 200) throw new runtimeexception ("request URL failed ");
  22. Inputstream is = conn. getinputstream (); // get the returned data
  23. String result = readasstring (is, "UTF-8 ");
  24. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.