Android-based HTTP Communication and Android-based HTTP Communication

Source: Internet
Author: User

Android-based HTTP Communication and Android-based HTTP Communication

I recently learned about HTTP Communication in MOOC Android and summarized it myself. I have read many blog posts and thank you for sharing them.

The content of this article includes:
1. HTTP Overview
2. Differences between HTTP/1.0 and HTTP/1.1
3. HTTP request header, response header, and status code
4. HttpUrlConnection in Android

1. Http Overview
Http (Hypertext transfer protocol) defines how a browser sends documents to the World Wide Web server, and how the server sends documents to the server. In terms of hierarchy, http is oriented to the application layer protocol and is the basis for reliable document exchange on the World Wide Web.
Http Workflow
When the user clicks a link (assuming the URL is a http://www.tsinghua.edu.cn/chn/yxsz/index.html), the event process occurs:
(1) The browser analyzes the URL of the page to which the connection points.
(2) the browser requests DNS to resolve the IP address of www.tsinghua.edu.cn.
(3) the browser parses the IP address of the server.
(4) Establish a TCP connection between the browser and the server.
(5) the browser sends the command to GET the file: GET/chn/yxsz/index.html.
(5) the server sends the file index.html to the browser.
(6) release the TCP connection.
(7) The browser displays the information used by index.html.
Http features
(1)Support Customer Service/Server (C/S) Mode
(2)Simple and fastWhen a customer requests a service from the server, only the Request Method and path are sent. Common Request methods include GET, POST, and HEAD. Each method requires that the connection is different from that of the server. Because the HTTP protocol is simple, the program size of the HTTP server is smaller and the communication speed is faster.
(3) HTTP isConnectionless. No connection means that HTTP only processes one request at a time. After the server completes processing the customer's request and receives the customer's response, the server disconnects the connection, which can save transmission time.
(4) HTTP isStateless. Stateless means that the HTTP protocol has no memory for the transaction. A lack of status indicates that subsequent processing requires the previous information, and it must be re-transmitted. This may increase the amount of information transmitted without connection. On the other hand, the server performs very fast without the previous information, and the server is more likely to support a large number of concurrent HTTP requests.
PS: Although HTTP is a connectionless protocol, HTTP uses the connection-oriented transport layer protocol TCP, which ensures reliable data transmission, HTTP does not need to consider how data is retransmitted when transmitted.
2. Differences between HTTP/1.0 and HTTP/1.1
The main disadvantage of HTTP/1.0 is that it usesNon-persistent connectionEvery request for a document requires twice the RTT overhead. In this case, if a home page has many linked objects (slices) and each link needs to establish a new TCP connection, each link download will result in a 2 × RTT overhead.
The HTTP/1.1 protocol solves this problem well and uses persistent connections. The web server still maintains this connection for a period of time after sending a response, the same client can send subsequent HTTP request messages and response messages to the server.
3. HTTP request header, response header, and status code
Request Header (the Request Header for accessing the simplified book can be viewed through the Firfox browser using the developer option to open the network (shortcut: ctrl + shift + Q )).

GET http://www.jianshu.com/
Host: www.jianshu.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv: 47.0) Gecko/20100101 Firefox/47.0
Accept: text/html, application/xhtml + xml, application/xml; q = 0.9,/; Q = 0.8
Accept-Language: zh-CN, zh; q = 0.8, en-US; q = 0.5, en; q = 0.3
Accept-Encoding: gzip, deflate
Referer: https://www.google.com.hk
Cookie: (omitted)
Connection: keep-alive
If-None-Match: W/"b4e2a47d84be2df34bb1d5b79be9c040"
Cache-Control: max-age = 0

The specific meanings are as follows:
GET defines the request method. There are eight methods (listed below ).
Host: Host and port in the initial URL.
User-Agent: browser type. This value is useful if the content returned by the Servlet is related to the browser type.
Accept: the MIME type acceptable to the browser.
Accept-Charset: the acceptable character set of the browser.
Accept-Language: the type of Language that the browser wants to use when the server can provide more than one Language version.
Accept-Encoding: The data Encoding method that the browser can decode, such as gzip. Servlet can return gzip-encoded HTML pages to a browser that supports gzip. In many cases, this can reduce the download time by 5 to 10 times.
Referer: contains a URL from which you can access the current requested page.
Cookie: This is one of the most important request header information. When an HTTP request is sent, it sends all the cookie values stored under the request domain name to the web server.
Connection: Indicates whether a persistent Connection is required. If the Servlet sees that the value here is "Keep-Alive", or the request uses HTTP 1.1 (HTTP 1.1 performs a persistent connection by default), it can take advantage of the advantages of persistent connections, when a page contains multiple elements (such as an Applet or image), the download time is significantly reduced. To achieve this, the Servlet needs to send a Content-Length header in the response. The simplest method is to write the Content into ByteArrayOutputStream first, then, calculate the size of the content before writing it.
Cache-Control: If-None-Match: If the content is not changed, code 304 is returned. The parameter is the Etag previously sent by the server. Compare it with the Etag responded by the server to determine whether to change it.
Cache-Control: Specifies the Cache mechanism for requests and responses.
8 Request Method explanation (from: http://itbilu.com/other/relate/EkwKysXIl.html)
GET
The requested resource is displayed. Generally, the GET method should be used only for Data Reading, rather than for non-idempotent operations that produce side effects. The GET method requests the specified page information and returns the response body. GET is considered unsafe because the GET method will be accessed by any web spider.
HEAD
The same method as the GET method is to send requests to the server for specified resources. However, the server is responding to the HEAD
The request does not return the content of the resource, that is, the response body. In this way, we can get the response header information of the server without transmitting all the content. The HEAD method is often used by clients to view server performance.
POST
The request submits data to the specified resource and the server is requested to process the data, such as form data submission and file upload. The request data is contained in the request body. The POST method is a non-idempotent method, because this request may create new resources or/and modify existing resources.
PUT
The request uploads the latest content to the specified resource location. The PUT method is a idempotent method. In this way, the client can transmit the latest data of the specified resource to the server to replace the content of the specified resource.
DELETE
The request is used to request the server to delete the Resource identified by the requested URI (Uniform Resource Identifier, Uniform Resource Identifier. After a DELETE request, the specified resource will be deleted, and the DELETE method is also idempotent.
.
CONNECT
This method is reserved for HTTP/1.1 and can be used to change the connection to a proxy server in the pipeline mode. It is usually used for communication between SSL encrypted server links and non-encrypted HTTP proxy servers.
OPTIONS
Requests are similar to headers, which are generally used by clients to view server performance. This method will request the server to return all HTTP request methods supported by the resource. This method will use''To replace the Resource Name and send an OPTIONS request to the server to test whether the server function is normal. When the XMLHttpRequest object of JavaScript is used for CORS cross-origin resource sharing, it uses the OPTIONS method to send a sniffing request to determine whether it has access permissions to the specified resource. Allow
*
TRACE
The request server returns the request information it receives. This method is mainly used for testing or diagnosing HTTP requests.
*
Response Header (also the response header on the homepage of the Request simplified book)

Cache-Control: max-age = 0, private, must-revalidate
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html; charset = UTF-8
Date: Sun, 19 Jun 2016 15:29:41 GMT
Etag: W/"e9a43aabd740855cd3fe0097faf6180d"
Server: nginx
Set-Cookie: (omitted)
Vary: Accept-Encoding
X-Request-Id: ce26a795-7e99-4959-a498-45f689471d7f
X-Runtime: 0.596683
X-content-type-options: nosniff
X-frame-options: DENY
X-xss-protection: 1; mode = block

Cache-Control specifies the Cache mechanism that requests and responses follow.
Connection: Indicates whether a persistent Connection is required.
The Encoding (Encode) method of the Content-Encoding document. The Content Type specified by the Content-Type header can be obtained only after decoding. Gzip compression can significantly reduce the download time of HTML documents.
Content-Type indicates the MIME Type of the subsequent document. Servlet is text/plain by default, but it must be explicitly specified as text/html. Because Content-Type is often set, HttpServletResponse provides a dedicated method setContentTyep.
The current GMT time of Date. You can use setDateHeader to set this header to avoid the trouble of converting the time format.
The current value of the entity tag of the Etag request variable.
Server name. Servlet generally does not set this value, but is set by the Web server.
Set-Cookie: Set the Cookie associated with the page.
Vary tells the downstream proxy whether to use cache response or request from the original server
For more details about request headers and response headers, see HTTP Header details.
*
Status Code
1XX: indicates the notification information, such as the request received or being processed.
2XX: indicates that the request is successful, such as receiving or knowing.
3XX: Indicates redirection. Further Processing is required if it is to be completed.
4XX: indicates the customer's error. If the request contains an incorrect syntax or cannot be completed.
5XX: indicates a server error. If the server fails, the request cannot be completed.

4. HttpUrlConnection in Android
The connection in Android is mainly completed through HttpUrlConnection.HttpUrlConnection usage, get and post passing parameters, multi-thread downloadThe usage of HttpUrlClient is as follows:
(1) Use format of HttpUrlConnection:
URL url = new URL ("http: // localhost: 8080/TestHttpURLConnectionPro/index. jsp"); // convert the address to a URL
URLConnection rulConnection = url. openConnection (); // The urlConnection object here is actually a subclass of the URLConnection class HttpURLConnection generated based on the URL request protocol (http, therefore, it is best to convert it to HttpURLConnection type object HttpURLConnection httpUrlConnection = (HttpURLConnection) rulConnection;
Set the connection parameters of HttpUrlClient:
// Set whether to output data to httpUrlConnection. Because this is a post request, the parameter should be placed. It must be set to true in the http body. The default value is false. httpUrlConnection. setDoOutput (true); // sets whether to read data from httpUrlConnection. The default value is true; httpUrlConnection. setDoInput (true); // The cache httpUrlConnection cannot be used for Post requests. setUseCaches (false); // set that the transmitted content type is a serializable java object. // (If this option is not set, when the serialized object is transferred, if the WEB service is not of this type by default, java may be thrown. io. EOFException) httpUrlConnection. setRequestProperty ("Content-type", "application/x-java-serialized-object"); // set the request method to "POST". The default value is GET httpUrlConnection. setRequestMethod ("POST"); // connection, from the above 2nd URLs. the current configuration of openConnection () must be completed before connect, httpUrlConnection. connect ();
For the specific usage of HttpUrlConnection in the code, we can see the same usage below.
(2) PASS Parameters in get and post Modes
Get Method
The key to passing parameters through get is the URl. In the code, we can see that some data is appended to the url. In fact, the get method transmits parameters by appending data to the url, therefore, this method is not safe.
Private void doGet () {try {url = url + "? Name = "+ URLEncoder. encode (name, "UTF-8") + "& age =" + age;} catch (UnsupportedEncodingException e) {e. printStackTrace ();} try {URL httpUrl = new URL (url); // create a URL object HttpURLConnection conn = (HttpURLConnection) httpUrl. openConnection (); // open a connection conn. setRequestMethod ("GET"); // set the request method to get conn. setReadTimeout (5000); // set the timeout value for reading data from the server to 5 seconds. BufferedReader reader = new BufferedReader (new InputStreamReader (Conn. getInputStream (); // obtain the data input stream String str transmitted by the server; StringBuffer sb = new StringBuffer (); // store the read data while (str = reader. readLine ())! = Null) {// read sb data. append (str);} System. out. println ("result:" + sb. toString ();} catch (MalformedURLException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}
POST method
The post parameter passing method is different from the get method. It writes the transmitted data to the Request body.
Private void doPost () {try {URL HttpUrl = new URL (url); HttpURLConnection conn = (HttpURLConnection) HttpUrl. openConnection (); conn. setRequestMethod ("POST"); conn. setReadTimeout (5000); OutputStream out = conn. getOutputStream (); // create the output stream object String content = "name =" + name + "& age =" + age; // pass the object out. write (content. getBytes (); // convert the transmitted object into a response stream and write it to the output stream. // The following describes the processing of the data returned by the server. BufferedReader reader = new BufferedReader (new InputStreamReader (conn. getInputStream (); StringBuffer sb = new StringBuffer (); String str; while (str = reader. readLine ())! = Null) {sb. append (str);} System. out. println (sb. toString ();} catch (MalformedURLException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}
(3) multi-thread download

public class DownLoad {private Executor threadPool = Executors.newFixedThreadPool(3);private Handler handler;public DownLoad(Handler handler){    this.handler = handler;}static class DownLoadRunnable implements Runnable {    private String url;    private String fileName;     private long start;    private long end;    private Handler handler;    public DownLoadRunnable(String url,String fileName,long start,long end,Handler handler){        this.url = url;        this.fileName = fileName;        this.start = start;        this.end = end;        this.handler = handler;    }    @Override    public void run() {        try {            URL httpUrl = new URL(url);            HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();            conn.setRequestMethod("GET");            conn.setRequestProperty("Range","bytes="+start+"-"+end);            conn.setReadTimeout(5000);                        RandomAccessFile access = new RandomAccessFile(new File(fileName),"rwd");            access.seek(start);            InputStream in = conn.getInputStream();             byte[] b = new byte[1024*4];            int len=0;            while((len=in.read(b))!=-1){                access.write(b,0,len);            }                        if (access!=null){                access.close();            }            if (in!=null){                in.close();            }                        Message msg = new Message();            msg.what = 1;            handler.sendMessage(msg);        } catch (IOException e) {            e.printStackTrace();        }    }}public void loadFile(String url) {    try {        URL httpUrl = new URL(url);        HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();        conn.setRequestMethod("GET");        conn.setReadTimeout(5000);        int count = conn.getContentLength();        int block = count / 3;         String fileName = getFileName(url);        File parent = Environment.getExternalStorageDirectory();        File download = new File(parent,fileName);                        for (int i=0;i<3;i++){            long start = i*block;            long end = (i+1)*block-1;            if (i==2){                end = count;            }                        DownLoadRunnable runnable = new DownLoadRunnable(url,download.getAbsolutePath(),start,end,handler);            threadPool.execute(runnable);        }    } catch (IOException e) {        e.printStackTrace();    }}public String getFileName(String url){    return url.substring(url.lastIndexOf("/")+1);}

}
Refer:
(1) HTTP Communication in MOOC course Android
(2) HTTP request methods: GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, and TRACE
(3) HTTP response header and Request Header
(4) HTTP Header details

Related Article

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.