Get request: Gets request sends the request to the server requesting data, thus obtains the information, the request is like the database's select operation, only uses to query the data, does not modify, adds the data, does not affect the resource content, namely this request does not have the side effect. No matter how many times you do this, the results are the same.
Post request: Post is to send data to the server side, but the request will change the type of data and other resources, like the database insert operation, will create new content. Almost all of the present submissions are made with post requests.
Javacode:
/** * Request to send a GET method to the specified URL */public static string get (String URL) {BufferedReader in = null;
try {URL realurl = new URL (URL);
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) ");
Connection.setconnecttimeout (5000);
Connection.setreadtimeout (5000);
Establish the actual connection connection.connect ();
Defines the response of the BufferedReader input stream to read the URL in = new BufferedReader (New InputStreamReader (Connection.getinputstream ()));
StringBuffer sb = new StringBuffer ();
String Line;
while (line = In.readline ())!= null) {sb.append (line);
return sb.tostring ();
catch (Exception e) {log.error ("Exception occur when send HTTP GET request!", e); ///Use finally block to close the input stream finally {try {if (in!= null) {in.Close ();
} catch (Exception E2) {e2.printstacktrace ();
} return null; }
/** * Send httppost Request * * @param strurl * Service address * @param params * * @return Success: return JSON string <br/> */public static string Jsonpost (String strurl, map<string, string> params) {try {URL url = new URL (strur
L)//Create connection HttpURLConnection connection = (httpurlconnection) url.openconnection ();
Connection.setdooutput (TRUE);
Connection.setdoinput (TRUE);
Connection.setusecaches (FALSE);
Connection.setinstancefollowredirects (TRUE); Connection.setrequestmethod ("POST"); Set the request mode Connection.setrequestproperty ("Accept", "Application/json"); Sets the format for receiving data Connection.setrequestproperty ("Content-type", "Application/json");
Sets the format for sending data connection.connect (); OutputStreamWriter out = new OutputStreamWriter (Connection.getoutputstream (), "UTF-8");
Utf-8 Encoding Out.append (jsonutil.object2jsonstring (params));
Out.flush ();
Out.close ();
int code = Connection.getresponsecode ();
InputStream is = null; if (code = =) {is = Connection.getinputstream ();
else {is = Connection.geterrorstream (); //Read response int length = (int) connection.getcontentlength ()//To get length if (length!=-1) {byte[] data = new BYT
E[length];
byte[] temp = new byte[512];
int readlen = 0;
int destpos = 0;
while ((Readlen = Is.read (temp)) > 0) {system.arraycopy (temp, 0, data, Destpos, Readlen);
Destpos + = Readlen; string result = new String (data, "UTF-8");
Utf-8 code return result;
The catch (IOException e) {log.error ("Exception occur when send HTTP POST request!", e); return "error"; Custom error Messages}