Java sends http get and post requests, and java sends getpost
Reposted blog: http://www.cnblogs.com/zhuawang/archive/2012/12/08/2809380.html
Http request class
Package wzh. http; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStreamReader; import java. io. printWriter; import java.net. URL; import java.net. URLConnection; import java. util. list; import java. util. map; public class HttpRequest {/*** request for sending a GET method to a specified URL ** @ param url * The request URL * @ param * request parameter, the request parameters should be in the form of name1 = value1 & name2 = value2. * @ Return URL indicates the response result of the Remote resource */public static String sendGet (String url, String param) {String result = ""; BufferedReader in = null; try {String urlNameString = url + "? "+ Param; URL realUrl = new URL (urlNameString); // enable the URL connection URLConnection connection = realUrl. openConnection (); // set the common request attribute connection. setRequestProperty ("accept", "*/*"); connection. setRequestProperty ("connection", "Keep-Alive"); connection. setRequestProperty ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); // establish the actual connection. connect (); // obtain the Map of all response header fields <String, Lis T <String> map = connection. getHeaderFields (); // traverses all response header fields for (String key: map. keySet () {System. out. println (key + "--->" + map. get (key);} // defines the BufferedReader input stream to read the URL response in = new BufferedReader (new InputStreamReader (connection. getInputStream (); String line; while (line = in. readLine ())! = Null) {result + = line;} catch (Exception e) {System. out. println ("an Exception occurred when sending a GET request! "+ E); e. printStackTrace () ;}// use the finally block to close the input stream finally {try {if (in! = Null) {in. close () ;}} catch (Exception e2) {e2.printStackTrace () ;}} return result ;} /*** send a POST method request to a specified URL ** @ param url * send the request URL * @ param * request parameter, the request parameters should be in the form of name1 = value1 & name2 = value2. * @ Return indicates the response result of the Remote resource */public static String sendPost (String url, String param) {PrintWriter out = null; BufferedReader in = null; String result = ""; try {URL realUrl = new URL (url); // enable the URL Connection URLConnection conn = realUrl. openConnection (); // set the common request attribute conn. setRequestProperty ("accept", "*/*"); conn. setRequestProperty ("connection", "Keep-Alive"); conn. setRequestProperty ("user-agent", "Moz Illa/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); // The following two rows of conn must be set to send a POST request. setDoOutput (true); conn. setDoInput (true); // get the output stream of the URLConnection object out = new PrintWriter (conn. getOutputStream (); // sends the request parameter out. print (param); // flush the Buffer out of the output stream. flush (); // defines the BufferedReader input stream to read the URL response in = new BufferedReader (new InputStreamReader (conn. getInputStream (); String line; while (line = in. readLine ())! = Null) {result + = line;} catch (Exception e) {System. out. println ("an Exception occurred when sending the POST request! "+ E); e. printStackTrace () ;}// use finally blocks to close the output stream and input stream finally {try {if (out! = Null) {out. close () ;}if (in! = Null) {in. close () ;}catch (IOException ex) {ex. printStackTrace () ;}} return result ;}}
Call method:
Public static void main (String [] args) {// send the GET request String s = HttpRequest. sendGet ("http: // localhost: 6144/Home/RequestString", "key = 123 & v = 456"); System. out. println (s); // send the POST request String sr = HttpRequest. sendPost ("http: // localhost: 6144/Home/RequestPostString", "key = 123 & v = 456"); System. out. println (sr );}