Understanding of Android HTTP

Source: Internet
Author: User

The difference between HTTP 1.0 and http1.1
On a TCP connection
Only one TCP channel should be established after 1.1 each time
http1.0 each request requires a new TCP connection and the connection cannot be reused.
The http1.1 request can be sent on top of the last TCP connection requested, and the connection can be reused.
A bit is to reduce the overhead of repeating the TCP three handshake and improve efficiency.
Note: In the same TCP connection, the new request needs to wait until the last request receives a response before sending

URLConnection conn = Url.openconnection ();
To set common request properties
Conn.setrequestproperty ("Accept", "*/*");
Conn.setrequestproperty ("Connection", "keep-alive");
Conn.setrequestproperty ("User-agent", "mozilla/5.0 (Windows NT 6.1) applewebkit/537.36 (khtml, like Gecko) chrome/ 31.0.1650.63 safari/537.36 SE 2.X METASR 1.0 ");

HttpURLConnection and httpclient which is better

Select Use HttpClient before 2.3 because the long connection is closed when the httpurlconnection is turned off.
Sometimes the returned content length is-1;

After 2.3, choose to use HttpURLConnection, because the httpurlconnection small size, fast response, reduce power consumption

Httpresponsecache

Httpresponsecache is a cache framework that appears after 4.0, and now only supports Httpurlconnection,httpsurlconnection, Defaluthttpclient and androidhttpclient are not supported.

Code:

1  protected voidonCreate (Bundle savedinstancestate) {2        ...3 4        Try {5File Httpcachedir =NewFile (Context.getcachedir (), "http");6            LongHttpcachesize = 10 * 1024 * 1024;//Ten MiB7 Httpresponsecache.install (Httpcachedir, httpcachesize);8         Catch(IOException e) {9LOG.I (TAG, "HTTP Response cache Installation failed:" +e);Ten        } One    } A  -    protected voidOnStop () { -        ... the  -Httpresponsecache cache =httpresponsecache.getinstalled (); -        if(Cache! =NULL) { - Cache.flush (); +        } -}}

Understanding of Android HTTP

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.