PackageCom.ld.nhmz.util;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.URL;Importjava.net.URLConnection;Importjava.util.List;ImportJava.util.Map; Public classHttpRequest {/*** A request to send a GET method to a specified URL *@paramURL * URL to send request *@paramparam * Request parameters, request parameters should be in the form of name1=value1&name2=value2. * @returnThe response result of the remote resource represented by the URL*/ Public Staticstring sendget (string url, string param) {string result= ""; BufferedReader in=NULL; Try{String urlnamestring= URL + "?" +param; URL Realurl=NewURL (urlnamestring); //opening and linking between URLsURLConnection connection =realurl.openconnection (); //to set common request propertiesConnection.setrequestproperty ("Accept", "*/*"); Connection.setrequestproperty ("Connection", "keep-alive"); Connection.setrequestproperty ("User-agent", "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); //establish an actual connectionConnection.connect (); //Get all response header fieldsmap<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)); } //defines the response of the BufferedReader input stream to read the URLin =NewBufferedReader (NewInputStreamReader (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 the finally block to close the input stream finally { Try { if(In! =NULL) {in.close (); } } Catch(Exception E2) {e2.printstacktrace (); } } returnresult; } /*** Request to send the Post method to the specified URL * *@paramURL * URL to send request *@paramparam * Request parameters, request parameters should be in the form of name1=value1&name2=value2. * @returnThe response result of the remote resource represented*/ Public Staticstring sendpost (string url, string param) {PrintWriter out=NULL; BufferedReader in=NULL; String result= ""; Try{URL Realurl=Newurl (URL); //opening and linking between URLsURLConnection conn =realurl.openconnection (); //to set common request propertiesConn.setrequestproperty ("Accept", "*/*"); Conn.setrequestproperty ("Connection", "keep-alive"); Conn.setrequestproperty ("User-agent", "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); //to send a POST request, you must set the following two linesConn.setdooutput (true); Conn.setdoinput (true); //gets the output stream corresponding to the URLConnection objectout =NewPrintWriter (Conn.getoutputstream ()); //Send Request Parametersout.print (param); //buffer for flush output streamOut.flush (); //defines the response of the BufferedReader input stream to read the URLInputStream xx =Conn.getinputstream (); Inch=NewBufferedReader (NewInputStreamReader (xx)); String Line; while(line = In.readline ())! =NULL) {result+=Line ; } } Catch(Exception e) {System.out.println ("Send POST Request exception!" " +e); E.printstacktrace (); } //Use the 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 (); } } returnresult; } /*** A request to send a GET method to a specified URL *@paramURL * URL to send request *@paramparam * Request parameters, request parameters should be in the form of name1=value1&name2=value2. * @returnThe response result of the remote resource represented by the URL*/ Public Staticstring sendrestget (string url) {string result= ""; BufferedReader in=NULL; Try{String urlnamestring=URL; URL Realurl=NewURL (urlnamestring); //opening and linking between URLsURLConnection connection =realurl.openconnection (); //to set common request propertiesConnection.setrequestproperty ("Accept", "*/*"); Connection.setrequestproperty ("Connection", "keep-alive"); Connection.setrequestproperty ("User-agent", "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); //establish an actual connectionConnection.connect (); //Get all response header fieldsmap<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)); } //defines the response of the BufferedReader input stream to read the URLInputStream xx =Connection.getinputstream (); Inch=NewBufferedReader (NewInputStreamReader (xx)); String Line; while(line = In.readline ())! =NULL) {result+=Line ; } } Catch(Exception e) {System.out.println ("Send GET request exception!" " +e); E.printstacktrace (); } //Use the finally block to close the input stream finally { Try { if(In! =NULL) {in.close (); } } Catch(Exception E2) {e2.printstacktrace (); } } returnresult; }}
Java sends HTTP requests