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 request for a GET method to a specified URL * * @param url * Send request url * @param param * request parameter, request parameter should be name1= The form of value1&name2=value2 . * @return URL Response Results for remote resources represented */ public static string sendget (string url, String param) { String result = ""; BufferedReader in = null; try { String urlNameString = url + "?" + param; url realurl = new url (urlnamestring); // connection between open and URL urlconnection connection = realurl.openconnection (); // set Common request Properties Connection.setrequestproperty ("Accept", "*/*"); &nbSp; 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 (); // Iterate through all the response header fields &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; for (String key : map.keyset ()) { system.out.println (key + "-- + map.get (key); } // definition BufferedReader the 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 the finally block to close the input stream finally { try { if (in != null) { in.close (); } } catch (EXCEPTION&NBSP;E2) { e2.printstacktrace (); } } return result; } /** * request to specify URL send a POST method * * @param url * send a request URL * @param param * request parameter, request parameter should be &NBThe form of sp;name1=value1&name2=value2 . * @return Response results for remote resources represented */ public static string sendpost (String url, string param) { PrintWriter out = null; BufferedReader in = null; string result = ""; try { url realurl = new url (URL); // connection between open and URL urlconnection conn = realurl.openconnection (); // set Common request Properties &NBsp; conn.setrequestproperty ("Accept", " */* "); conn.setrequestproperty (" Connection ", " keep-alive "); Conn.setrequestproperty ("User-agent", "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; SV1); // send a POST request must have the following two lines set conn.setdooutput (True); conn.setdoinput (True); // get the output stream corresponding to the URLConnection object &nBsp; out = new printwriter (Conn.getoutputstream ()); // Send Request Parameters out.print (param); // flush the buffered out.flush () of the output stream; // define BufferedReader input stream to read the response of 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 Unexpected! "+e"); e.printstacktrace (); } //using the finally block to close the output stream, input stream finally{ try{ if (out!=null) { &nBsp; 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); }
Java sends a GET, POST request for HTTP