Get request: A GET request sends a request for data to the server to obtain information, which, like a database's select operation, simply queries the data, does not modify, add data, and does not affect the contents of the resource, that is, the request does not have side effects. The results are the same no matter how many times you do it.
1 /** 2 * Request to send a GET method to the specified URL3 * 4 */ 5 Public Staticstring get (string url) {6BufferedReader in =NULL; 7 Try { 8URL Realurl =Newurl (URL); 9 //opening and linking between URLsTenURLConnection connection =realurl.openconnection (); One //to set common request properties AConnection.setrequestproperty ("Accept", "*/*"); -Connection.setrequestproperty ("Connection", "keep-alive"); -Connection.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); theConnection.setconnecttimeout (5000); -Connection.setreadtimeout (5000); - //establish an actual connection - Connection.connect (); + //defines the response of the BufferedReader input stream to read the URL -in =NewBufferedReader (NewInputStreamReader (Connection.getinputstream ())); +StringBuffer SB =NewStringBuffer (); A String Line; at while(line = In.readline ())! =NULL) { - Sb.append (line); - } - returnsb.tostring (); -}Catch(Exception e) { -Log.error ("Exception occur when send HTTP GET request!", E); in } - //Use the finally block to close the input stream to finally { + Try { - if(In! =NULL) { the In.close (); * } $}Catch(Exception E2) {Panax Notoginseng E2.printstacktrace (); - } the } + return NULL; A}
Post request: Post is sending data to the server, but the request alters resources such as the type of data, and creates new content just like the insert operation of the database. Almost all of the current commit operations are requested by post.
1 /** 2 * Send HttpPost request3 * 4 * @paramstrURL5 * Service Address6 * @paramparams7 * 8 * @returnsuccess: Return JSON string <br/>9 */ Ten Public StaticString Jsonpost (String strurl, map<string, string>params) { One Try { AURL url =NewURL (strURL);//Create a connection -HttpURLConnection connection =(HttpURLConnection) url.openconnection (); -Connection.setdooutput (true); theConnection.setdoinput (true); -Connection.setusecaches (false); -Connection.setinstancefollowredirects (true); -Connection.setrequestmethod ("POST");//Set Request Mode +Connection.setrequestproperty ("Accept", "Application/json");//set the format for receiving data -Connection.setrequestproperty ("Content-type", "Application/json");//format the sending data + Connection.connect (); AOutputStreamWriter out =NewOutputStreamWriter (Connection.getoutputstream (), "UTF-8");//utf-8 Encoding at Out.append (jsonutil.object2jsonstring (params)); - Out.flush (); - Out.close (); - - intCode =Connection.getresponsecode (); -InputStream is =NULL; in if(Code = = 200) { -is =Connection.getinputstream (); to}Else { +is =Connection.geterrorstream (); - } the * //Read Response $ intLength = (int) Connection.getcontentlength ();//Get lengthPanax Notoginseng if(Length! =-1) { - byte[] data =New byte[length]; the byte[] temp =New byte[512]; + intReadlen = 0; A intDestpos = 0; the while((Readlen = Is.read (temp)) > 0) { +System.arraycopy (temp, 0, data, Destpos, Readlen); -Destpos + =Readlen; $ } $String result =NewString (data, "UTF-8");//utf-8 Encoding - returnresult; - } the -}Catch(IOException e) {WuyiLog.error ("Exception occur when send HTTP POST request!", E); the } - return"Error";//Custom Error Messages Wu}
Java implementation of Post and GET requests