Java sends http get and post requests, and java sends getpost

Source: Internet
Author: User

Java sends http get and post requests, and java sends getpost

Directly Add code

1 package wzh. http; 2 3 import java. io. bufferedReader; 4 import java. io. IOException; 5 import java. io. inputStreamReader; 6 import java. io. printWriter; 7 import java.net. URL; 8 import java.net. URLConnection; 9 import java. util. list; 10 import java. util. map; 11 12 public class HttpRequest {13/** 14 * Send a GET Method Request to a specified URL 15*16 * @ param url 17 * send the request URL 18 * @ param 19 * request Parameters, the request parameter should be name1 = value 1 & name2 = value2 format. 20 * @ return URL indicates the remote resource response result 21 */22 public static String sendGet (String url, String param) {23 String result = ""; 24 BufferedReader in = null; 25 try {26 String urlNameString = url + "? "+ Param; 27 URL realUrl = new URL (urlNameString); 28 // open the connection with URL 29 URLConnection connection = realUrl. openConnection (); 30 // set the general request property 31 connection. setRequestProperty ("accept", "*/*"); 32 connection. setRequestProperty ("connection", "Keep-Alive"); 33 connection. setRequestProperty ("user-agent", 34 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); 35 // establish the actual connection 36 connection. connect (); 37 // obtain all response header fields 38 Map <String, List <String> map = connection. getHeaderFields (); 39 // traverse all response header fields 40 for (String key: map. keySet () {41 System. out. println (key + "--->" + map. get (key); 42} 43 // defines the BufferedReader input stream to read the URL response 44 in = new BufferedReader (new InputStreamReader (45 connection. getInputStream (); 46 String line; 47 while (line = in. readLine ())! = Null) {48 result + = line; 49} 50} catch (Exception e) {51 System. out. println ("an Exception occurred when sending a GET request! "+ E); 52 e. printStackTrace (); 53} 54 // use the finally block to close the input stream 55 finally {56 try {57 if (in! = Null) {58 in. close (); 59} 60} catch (Exception e2) {61 e2.printStackTrace (); 62} 63} 64 return result; 65} 66 67/** 68 * request for sending the POST method to the specified URL 69*70 * @ param url 71 * request URL 72 * @ param 73 * request parameter, the request parameters should be in the form of name1 = value1 & name2 = value2. 74 * @ return indicates the remote resource response result 75 */76 public static String sendPost (String url, String param) {77 PrintWriter out = null; 78 BufferedReader in = null; 79 String result = ""; 80 try {81 URL realUrl = new URL (url); 82 // open the connection with URL 83 URLConnection conn = realUrl. openConnection (); 84 // set the common request attribute 85 conn. setRequestProperty ("accept", "*/*"); 86 conn. setRequestProperty ("connection", "Keep-Alive"); 87 Conn. setRequestProperty ("user-agent", 88 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1 )"); 89 // The following two rows must be set for sending a POST request 90 conn. setDoOutput (true); 91 conn. setDoInput (true); 92 // get the output stream 93 out = new PrintWriter (conn. getOutputStream (); 94 // send request parameter 95 out. print (param); 96 // flush the buffer of the output stream 97 out. flush (); 98 // defines the BufferedReader input stream to read the URL's response 99 in = new BufferedReader (100 new InputS TreamReader (conn. getInputStream (); 101 String line; 102 while (line = in. readLine ())! = Null) {103 result + = line; 104} 105} catch (Exception e) {106 System. out. println ("an Exception occurred when sending a POST request! "+ E); 107 e. printStackTrace (); 108} 109 // use finally blocks to close the output stream and input stream 110 finally {111 try {112 if (out! = Null) {113 out. close (); 114} 115 if (in! = Null) {116 in. close (); 117} 118} 119 catch (IOException ex) {120 ex. printStackTrace (); 121} 122} 123 return result; 124} 125}

Call

1 public static void main (String [] args) {2 3 // send GET request 4 String s = HttpRequest. sendGet ("url", "params"); 5 System. out. println (s); 6 7 // send the POST request 8 String sr = HttpRequest. sendPost ("url", "params"); 9 System. out. println (sr); 10} // parameters passed by params at the url's own link address & connected

 

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.