Java sends a GET, POST request for HTTP
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 {/** * a request to send a GET method to a specified URL * * @param URL * Send the requested URL * @param pa RAM * Request parameters, request parameters should be in the form of name1=value1&name2=value2. * The response result of the remote resource represented by the @return URL */public static string Sendget (string URL, string param) {string result = ""; BufferedReader in = null; try {String urlnamestring = URL + "?" + param; URL realurl = new URL (urlnamestring); The connection between open and URL urlconnection connection = Realurl.openconnection (); Set the generic 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 connection.connect (); Get all response header fields Map<string, list<string>> Map = Connection.getheaderfields (); Traverse all response header fields for (String Key:map.keySet ()) {System.out.println (key + "--->" + map.get (key )); }//define BufferedReader input stream to read the response of the URL in = new BufferedReader (New InputStreamReader ( Connection.getinputstream ())); String Line; while (line = In.readline ()) = null) {result + = line; }} catch (Exception e) {System.out.println ("Send GET request exception!) "+ e); E.printstacktrace (); }//Use finally block to close the input stream finally {try {if (in! = null) {In.clos E (); }} catch (Exception E2) {e2.printstacktrace (); }} return result; /** * Request to send a POST method to the specified URL * * @param URL * Send the requested URL * @param param * Request parameters, request parameters should be in the form of name1=value1&name2=value2. * The response result of the remote resource represented by the @return */public static string Sendpost (string url, string param) {printwriter out = null; BufferedReader in = null; String result = ""; try {URL realurl = new URL (URL); The connection between open and URL URLConnection conn = realurl.openconnection (); Set the generic request attribute Conn.setrequestproperty ("accept", "*/*"); Conn.setrequestproperty ("Connection", "keep-alive"); Conn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); The Send POST request must be set to the following two lines conn.setdooutput (true); Conn.setdoinput (TRUE); Gets the output stream corresponding to the URLConnection object out = new PrintWriter (Conn.getoutputstream ()); Send request parameter out.print (param); Flush output Stream Buffer Out.flush (); Defines the BufferedReader input stream to read the response of the URL in = new BufferedReader (New InputStreamReader (conn.getinput Stream ())); String Line; while (line = In.readline ()) = null) {result + = line; }} catch (Exception e) {System.out.println ("send POST request exception! "+e); E.printstacktrace (); }//Use finally block to close the output stream, 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 GET request String s=httprequest.sendget ("http://localhost:6144/Home/ RequestString "," key=123&v=456 "); System.out.println (s); Send POST request String sr=httprequest.sendpost ("http://localhost:6144/Home/RequestPostString", "key=123&v=456 "); System.out.println (SR); }
Transfer from http://www.cnblogs.com/zhuawang/archive/2012/12/08/2809380.html
Java sends a GET, POST request for HTTP