Java sends HTTP requests

Source: Internet
Author: User

java sends HTTP requests
  1. Package com.stock.show.util;


  2. Import Java.io.BufferedReader;

  3. Import java.io.IOException;

  4. Import Java.io.InputStreamReader;

  5. Import Java.io.PrintWriter;

  6. Import Java.net.URL;

  7. Import java.net.URLConnection;

  8. Import java.util.List;

  9. Import Java.util.Map;


  10. public class Httprequestutil {


  11. /**

  12. * Request to send a GET method to the specified URL

  13. *

  14. * @param URL

  15. * Send the requested URL

  16. * @param param

  17. * Request Parameters

  18. * @return The response result of the remote resource represented by the URL

  19. */

  20. public static string Sendget (string url, map<string,string> param) {

  21. String result = "";

  22. BufferedReader in = null;

  23. try {

  24. String urlnamestring = URL;


  25. Add parameter

  26. if (Param!=null&m.size () >0) {

  27. urlnamestring = urlnamestring+ "?";

  28. For (String Key:param.keySet ()) {

  29. urlnamestring = urlnamestring + key + "=" + param.get (key);

  30. }

  31. }


  32. URL realurl = new URL (urlnamestring);

  33. Opening and linking between URLs

  34. URLConnection connection = Realurl.openconnection ();

  35. To set common request properties

  36. Connection.setrequestproperty ("Accept", "*/*");

  37. Connection.setrequestproperty ("Connection", "keep-alive");

  38. Connection.setrequestproperty ("User-agent",

  39. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");

  40. Establish an actual connection

  41. Connection.connect ();

  42. Get all response header fields

  43. map<string, list<string>> map = Connection.getheaderfields ();

  44. Iterate through all the response header fields

  45. For (String Key:map.keySet ()) {

  46. SYSTEM.OUT.PRINTLN (key + "--->" + map.get (key));

  47. }

  48. Defines the response of the BufferedReader input stream to read the URL

  49. in = new BufferedReader (New InputStreamReader (

  50. Connection.getinputstream ()));

  51. String Line;

  52. while (line = In.readline ()) = null) {

  53. result + = line;

  54. }

  55. } catch (Exception e) {

  56. SYSTEM.OUT.PRINTLN ("Send GET request exception!") "+ e);

  57. E.printstacktrace ();

  58. }

  59. Use the finally block to close the input stream

  60. finally {

  61. try {

  62. if (in = null) {

  63. In.close ();

  64. }

  65. } catch (Exception E2) {

  66. E2.printstacktrace ();

  67. }

  68. }

  69. return result;

  70. }


  71. /**

  72. * Request to send the Post method to the specified URL

  73. *

  74. * @param URL

  75. * Send the requested URL

  76. * @param param

  77. * Request Parameters

  78. * @return The response result of the remote resource represented

  79. */

  80. public static string Sendpost (string url, map<string,string> param) {

  81. PrintWriter out = null;

  82. BufferedReader in = null;

  83. String result = "";

  84. try {

  85. URL realurl = new URL (URL);

  86. Opening and linking between URLs

  87. URLConnection conn = Realurl.openconnection ();

  88. To set common request properties

  89. Conn.setrequestproperty ("Accept", "*/*");

  90. Conn.setrequestproperty ("Connection", "keep-alive");

  91. Conn.setrequestproperty ("User-agent",

  92. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");

  93. To send a POST request, you must set the following two lines

  94. Conn.setdooutput (TRUE);

  95. Conn.setdoinput (TRUE);

  96. Gets the output stream corresponding to the URLConnection object

  97. out = new PrintWriter (Conn.getoutputstream ());


  98. Processing parameters

  99. String paramstring = "";

  100. if (Param!=null&m.size () >0) {

  101. For (String Key:param.keySet ()) {

  102. paramstring = paramstring + key + "=" + param.get (key);

  103. }

  104. }

  105. Send Request parameters

  106. Out.print (paramstring);

  107. Buffer for flush output stream

  108. Out.flush ();

  109. Defines the response of the BufferedReader input stream to read the URL

  110. in = new BufferedReader (

  111. New InputStreamReader (Conn.getinputstream ()));

  112. String Line;

  113. while (line = In.readline ()) = null) {

  114. result + = line;

  115. }

  116. } catch (Exception e) {

  117. SYSTEM.OUT.PRINTLN ("Send POST request exception!") "+e);

  118. E.printstacktrace ();

  119. }

  120. Use the finally block to close the output stream, input stream

  121. finally{

  122. try{

  123. if (out!=null) {

  124. Out.close ();

  125. }

  126. if (in!=null) {

  127. In.close ();

  128. }

  129. }

  130. catch (IOException ex) {

  131. Ex.printstacktrace ();

  132. }

  133. }

  134. return result;

  135. }


  136. public static void Main (string[] args) {

  137. Send a GET request

  138. String s=httprequestutil.sendget ("http://hq.sinajs.cn/list=sh601006", null);

  139. System.out.println (s);



  140. }


  141. }


Java sends HTTP requests

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.