The essential difference between Android get and post

Source: Internet
Author: User
Tags comparison table html header

1. Get is the data that is fetched from the server and post is the data sent to the server.


2. Get is the URL where the parameter data queue is added to the Action property of the submission form, and the value corresponds to the field one by one in the form, which is visible in the URL. Post is the HTTP post mechanism that places the fields within the form with their contents in the HTML header, along with the URL address referred to by the Action property. The user does not see the process.


3. Get transmits a small amount of data, cannot be greater than 2KB. Post transmits a large amount of data, which is generally not restricted by default. In theory, however, the maximum amount of IIS4 is 100KB in 80KB,IIS5.


4. Get security is very low, post security is high. But the execution efficiency is better than the Post method.

Recommendation:


1, get the security of the method is less than the post, including confidential information, it is recommended to use the Post data submission method;


2, when doing data query, it is recommended to use Get method, and in the data to add, modify or delete, the proposed post method;

Request Headers Value

POST:
(request-line):P ost/wdinfo.php http/1.1
host:qurl.f.360.cn
accept:*/*
Cache-control:no-cache
Content-type:application/octet-stream
content-length:534
Return: Response Header value
(Status-line) http/1.1 OK
Cache-control Private
Date Mon, Mar 12:10:18 GMT
Expires Mon, Mar 12:10:18 GMT
Content-type text/html
Server bws/1.0
Connection keep-alive
Content-length 17
GET:
(Request-line) GET/?s=20150316200919 http/1.1
Accept image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*
User-agent Microsoft URL Control-6.00.8169
Host www.baidu.com
Connection keep-alive
Cache-control No-cache
Cookie Baidupsid=0eee39afcf8553059f84dd4bd6ed3e55; baiduid=c9c6c28b4741a72cb3850803ac4b651e:fg=1; bduss= Hby0nwv0tvbgfacgf3z2lnzw5uq1zyzepxd21xn05vndbbvdh2r0z5edj0uzfwqvfbqufbjcqaaaaaaaaaaaeaaaa4b8qcrejawknaaaaaaaaaaaaaaaaaaaa Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahyoblv2kazvwu; h_ps_pssid=8343_1439_12881_10902_10211_12575_12691_12694_12722_12735_12743_12781_11420_8498; bdsvrtm=221; bd_home=1; bd_upn=112351; h_ps_bbanner=5; H_ps_645ec=6439y0fb0gtiscg9fss%2fhifv3rt%2bvoo%2b34iq6usd5ylur%2fmaypol4wmwzhysrxxezwoq

Return: Response Header value
(Status-line) http/1.1 OK
Date Mon, Mar 12:09:30 GMT
Content-type text/html
Connection keep-alive
Vary accept-encoding
Cache-control Private
Expires Mon, Mar 12:09:30 GMT
Server bws/1.1
Bdpagetype 2
Bdqid 0xe7465d0d00000f35
Bduserid 482608952
Set-cookie bdsvrtm=193; path=/
Set-cookie bd_home=1; path=/
Set-cookie h_ps_pssid=8343_1439_12881_10902_10211_12575_12691_12694_12722_12735_12743_12781_11420_8498; path=/; Domain=.baidu.com
Content-length 148892

First, *************************************************************************httppost way:

1th step: Create a HttpPost object
HttpPost HttpPost = new HttpPost (URL);
Setting the HTTP POST request parameter must be with the Namevaluepair object
list<namevaluepair> params = new arraylist<namevaluepair> ();
Params.add (New Basicnamevaluepair ("BookName", Etbookname
. GetText (). toString ()));
Set HTTP POST request parameters
Httppost.setentity (New urlencodedformentity (params, HTTP. Utf_8));
2nd step: Use the Execute method to send an HTTP POST request and return the HttpResponse object
HttpResponse = new Defaulthttpclient (). Execute (httppost);
if (Httpresponse.getstatusline (). Getstatuscode () = = 200)
{
3rd Step: Use the GetEntity method to get the return result
String result = entityutils.tostring (HttpResponse
. GetEntity ());
Remove the "\ r" character from the returned result, or a small square will appear after the result string
Tvqueryresult.settext (Result.replaceall ("\ R", ""));
}

HttpGet Way:
String url = "Http://10.197.214.222:8080/querybooks/QueryServlet";
Add request parameters to a URL
URL + = "? Bookname=" + Etbookname.gettext (). toString ();
1th step: Create a HttpGet object
HttpGet httpget = new HttpGet (URL);
2nd step: Use the Execute method to send an HTTP GET request and return the HttpResponse object
HttpResponse = new Defaulthttpclient (). Execute (httpget);
To determine the request response status code, the status code of 200 indicates that the server has successfully responded to the client's request
if (Httpresponse.getstatusline (). Getstatuscode () = = 200) {
3rd Step: Use the GetEntity method to get the return result
String result = entityutils.tostring (HttpResponse
. GetEntity ());
Remove the "\ r" character from the returned result, or a small square will appear after the result string
Tvqueryresult.settext (Result.replaceall ("\ R", ""));
}

Second, HttpURLConnection way:

@Override
 public void Onfileitemclick (String filename)
 {
  string uploadurl = " Http://192.168.17.104:8080/upload/UploadServlet ";
  string end = "\ r \ n";
  string twohyphens = "--";
  string boundary = "******";
  try
  {
   url url = new URL (uploadurl);
   httpurlconnection httpurlconnection = (httpurlconnection) URL
     . OpenConnection ();
   httpurlconnection.setdoinput (TRUE);
   httpurlconnection.setdooutput (TRUE);
   httpurlconnection.setusecaches (false);

To set the HTTP request method, the method name must be capitalized, for example: Post,get
Httpurlconnection.setrequestmethod ("POST");
Httpurlconnection.setrequestproperty ("Connection", "keep-alive");
Httpurlconnection.setrequestproperty ("Charset", "UTF-8");
Httpurlconnection.setrequestproperty ("Content-type",
"multipart/form-data;boundary=" + boundary);

DataOutputStream dos = new DataOutputStream (
Httpurlconnection.getoutputstream ());
Dos.writebytes (twohyphens + boundary + end);
Dos.writebytes ("Content-disposition:form-data; Name=\ "File\"; Filename=\ ""
+ filename.substring (Filename.lastindexof ("/") + 1)
+ "\""
+ end);
Dos.writebytes (end);

FileInputStream fis = new FileInputStream (filename);
byte[] buffer = new byte[8192]; 8k
int count = 0;
while ((count = fis.read (buffer))! =-1)
{
Dos.write (buffer, 0, count);

}
Fis.close ();

Dos.writebytes (end);
Dos.writebytes (twohyphens + boundary + twohyphens + end);
Dos.flush ();

InputStream is = Httpurlconnection.getinputstream ();
InputStreamReader ISR = new InputStreamReader (IS, "utf-8");
BufferedReader br = new BufferedReader (ISR);
String result = Br.readline ();

Toast.maketext (this, result, Toast.length_long). Show ();
Dos.close ();
Is.close ();

}
catch (Exception e)
{
Settitle (E.getmessage ());
}

}


Third, the service side return code:

Response.setcontenttype ("Text/html;charset=utf-8");
String querystr = "";
if ("Post". Equals (Request.getmethod (). toLowerCase ()))
Querystr = "POST request; query string:" + new String (Request.getparameter ("BookName"). GetBytes (
"Iso-8859-1"), "Utf-8");
else if ("Get". Equals (Request.getmethod (). toLowerCase ()))
QUERYSTR = "Get request; Query string:" + request.getparameter ("BookName");

String s =QUERYSTR
+ "[Java Web Development Fast learning; Java Development Guide Idea (4th edition); Java EE Development Treasure; C # Development book] ";
PrintWriter out = Response.getwriter ();
Out.println (s);

Content-type Comparison table:

Http://tool.oschina.net/commons

The essential difference between Android get and post

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.