HTTP Request Classes
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 to send a GET method to the specified URL * * @param URL * Send the requested URL * @param param *
Request parameter, the request parameter should be the form of name1=value1&name2=value2.
* @return The response result of the remote resource represented by the URL/public static string Sendget (string URL, string param) {String results = "";
BufferedReader in = null;
try {String urlnamestring = URL + '? ' + param;
URL realurl = new URL (urlnamestring);
The connection between open and URL urlconnection connection = Realurl.openconnection ();
Sets 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 connection.connect ();
Gets all response header fields Map<string, list<string>> Map = Connection.getheaderfields ();
Iterate through all the response header fields for (String Key:map.keySet ()) {System.out.println (key +---> + map.get (key)); }//define BufferedReader input stream to read URL response in = new BufferedReader (New InputStreamReader Connection.getinpu
Tstream ()));
String Line;
while (line = In.readline ())!= null) {result + = line; The 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.close ();
} catch (Exception E2) {e2.printstacktrace ();
} return result; /** * Request * Send the Post method to the specified URL * * @param URL * Send the requested URL * @param param * Request parameters, request parameters should be name1
The form of =value1&name2=value2. * 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 ();
Sets the common 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 of the URLConnection object = new PrintWriter (Conn.getoutputstream ());
Send request parameter out.print (param);
Flush output Stream Buffer Out.flush ();
Defines the response of the BufferedReader input stream to read the URL in = new BufferedReader (New InputStreamReader (Conn.getinputstream ()));
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 turn off 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);
}
The above Java send HTTP GET, POST request implementation code is small series to share all the content of everyone, hope to give you a reference, but also hope that we support cloud habitat community.