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 { /* ** Send a GET request 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 ); // Open the connection with the URL Urlconnection connection = Realurl. openconnection (); // Set common request attributes 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 (); // Retrieve 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 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 error occurred while sending the GET request! " + E); E. printstacktrace ();} // Use finally blocks to close input streams Finally { Try { If ( In ! = Null ){ In . Close ();}} Catch (Exception E2) {e2.printstacktrace ();}} Return Result ;} /* ** Send a POST request 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 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 ); // Open the connection with the URL Urlconnection conn = Realurl. openconnection (); // Set common request attributes 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 following two rows must be set to send a POST request: Conn. setdooutput ( True ); Conn. setdoinput ( True ); // Obtain the output stream corresponding to the urlconnection object Out = New Printwriter (conn. getoutputstream ()); // Send request parameters Out . Print (PARAM ); // Flush buffer of the output stream Out . Flush (); // Define bufferedreader input stream to read 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 error occurred while sending the POST request! " +E); E. printstacktrace ();} // Use finally blocks to close output streams and input streams 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 );}