HTTP Send HTTP Message

Source: Internet
Author: User
Tags response code

  1. Package com.lkt.jdk;
  2. Import Java.io.BufferedInputStream;
  3. Import Java.io.BufferedReader;
  4. Import Java.io.DataOutputStream;
  5. Import java.io.IOException;
  6. Import Java.io.InputStream;
  7. Import Java.io.InputStreamReader;
  8. Import java.net.HttpURLConnection;
  9. Import Java.net.URL;
  10. Public class Gethttp {
  11. public static String getwebcontent ()throws ioexception{
  12. //Create URLs, protocols, IP, ports, visited pages
  13. URL url=new URL ("HTTP","192.168.22.209",7001,"/request_user_login");
  14. //Create HttpURLConnection Object
  15. HttpURLConnection conn= (httpurlconnection) url.openconnection ();
  16. //Set request mode post
  17. Conn.setrequestmethod ("POST");
  18. Conn.setrequestproperty ("Content-type", "Application/xml");
  19. Conn.setrequestproperty ("Connection", "keep-alive");
  20. //Requested data
  21. String xml="<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>" +
  22. "
  23. "<item name=\" admin\ "password=\" 670b14728ad9902aecba32e22fa4f6bd\ "userip=\" \ "port=\" \ "BusinessSysCode=\" \ "/>" +
  24. "</HTTP_XML>";
  25. //Data length
  26. Conn.setrequestproperty ("Content-length", xml.length () +"");
  27. //Set input and output streams
  28. Conn.setdooutput (true);
  29. Conn.setdoinput (true);
  30. //output parameters to the connection
  31. //Use this method, I do not have a problem in the local execution, the error occurred under the WebLogic
  32. //java.net.protocolexception:did not meet stated content length of Outputstream:you wrote 0 bytes and I was Expecti  Ng you to write exactly 241 bytes!!!
  33. //I'll use the second method.
  34. / * Method One
  35. Conn.getoutputstream (). Write (Xml.getbytes ("UTF-8"));
  36. Conn.getoutputstream (). Flush ();
  37. Conn.getoutputstream (). Close ();
  38. */
  39. /* 
  40. * Method Two * *
  41. DataOutputStream dos=New DataOutputStream (Conn.getoutputstream ());
  42. Dos.writebytes (XML);
  43. Dos.flush ();
  44. Dos.close ();
  45. //Get HTTP response code
  46. int Code=conn.getresponsecode ();
  47. InputStream Is=conn.getinputstream ();
  48. //Get return message
  49. BufferedReader bis=New BufferedReader (new InputStreamReader (IS));
  50. StringBuffer sb=New StringBuffer ();
  51. String temp="";
  52. While ((Temp=bis.readline ()) =null) {
  53. Sb.append (temp+"\ n");
  54. }
  55. return null;
  56. }
  57. public static void Main (string[] args)throws Exception {
  58. Getwebcontent ();
  59. }
  60. }

HTTP Send HTTP Message

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.