Http Communication for Android-1. First knowledge of Http protocol

Source: Internet
Author: User

Http Communication for Android-1. First knowledge of Http protocol
Http Communication for Android-1. First knowledge of Http protocol

Android-Http Communication 1 first understand Http Protocol Introduction body what is Http? terminology differences between Http 10 and Http 11 Http request workflow Http request methods Http status code collection Http protocol features OSI Layer-7 TCP layer-4 Protocol Summary

Introduction:

Today is children's day. Let's say Happy holidays to all over-age children ~ (Small □small), pig also symbolically sent a red envelope to the children in the group... Well, the busy May s have finally passed. I have finished completing my work, writing my papers, taking graduation photos, replying, resigning, and all kinds of things, pig now has another job in a new company, and the third company, you will surely feel that pig is two-minded. After all, it has changed to so many companies after almost graduation, then you may ask me, "When did you jump to the fourth company, pig?", haha, it should be okay. What should I do? In fact, I am still very specific. After all, I just graduated, I don't need to support my family, support the elderly, and I don't have a girlfriend, haha ~ Therefore, when you are young, you can choose the soil suitable for your own growth, learn more skills, and pay nothing. To be honest, learning something is the key. When you really have the skills, at that time, I could talk about my salary. For example, my company's Daniel (but I have already switched to meizu this week). For the first time, I felt that bgex was really high and I feel inferior to myself, people write components, push, various code encapsulation, various design modes, and server, test, IOS... In a novel, when a beginner apprentice saw a master at the master level, he thought that I could really take over his project after a few days of inferiority? Or understand his code? Or can I write such code? In those days, the whole person was very impetuous, and all kinds of negative emotions could not even be seen in the Code. Later, I saw a design-oriented TED speech titled: "How to become a good designer: Luo zixiong", although this does not mean programming, but in fact many things are the same, divided into two stages: Entry & quasi-professional level, "You want to be a designer, but you want to be a good designer. However, if you don't have a life, you will be excellent. If you don't have a life, you will have a uidesign, graphic design, and web design .", How much experience do we need from entry level to quasi-professional level ?" In his book "heterogeneous", Gladwell pointed out: "The talents in the eyes of people are not extraordinary, but they have made continuous efforts, 10 thousand hours of tempering is essential for anyone to change from ordinary to extraordinary." 10 thousand hours, that is, you work 8 hours a day, 5 days a week, you need 5 years. You don't need a genius, you don't need an IQ, you don't need three heads and six arms, you don't need a long horn, you just need continuous, persistent efforts, there is a correct method, you can be in the design field, one Professional is the only one." After reading this, the whole person is very cheerful and calm down. Right, how long have people been doing it? How long have I been doing it? I have been working as an intern for five years. I am just a new intern. I don't have a lot of things. I don't have to worry about it. I plan every day and keep one step at a time. I have been stuck for five years, or no, I can become a quasi-professional master, right? So why should I feel inferior ~ Well, let's talk about things in chicken soup. drinking too much wine is useless. The video is still good. If you are interested, you can check it out. Okay, starting from today, pig will summarize some related things while learning about the company's projects and share them with you. The first chapter describes Http Communication in Android, first, we will describe some Http-related concepts, and then talk about two methods for sending Http requests on Android: HttpUrlConnection and HttpClient, and then encapsulate our HTTP requests using the fit framework, at last, we use RxJava to implement responsive programming and Asynchronous Network request calling. Of course, pig is also learning. If there is anything wrong with it, I would like to express my gratitude ~

Body: 1. What is Http? ① Glossary:
Hypertext transfer protocol (hypertext transfer protocol), a TCP/IP protocol Application Layer Protocol, Used to define WEB browser and WEB ServerThe process of data exchange between them. After the client connects to the web server, if you want to obtain a web Resource in the web server, you must comply with a certain communication format. the HTTP protocol is used to define the communication format between the client and the web server.
② The differences between Http 1.0 and Http 1.1:
1.0 protocol. After the client establishes a connection with the web server, only one web resource can be obtained! 1.1 protocol: allows the client to establish a connection with the web server and then obtain multiple web resources from one connection!

PS: most of them are now using the Http 1.1 protocol ~

③ Http request workflow:

Here we will first introduce two terms: SYN and ACK.

SYN (synchronous): the handshake signal ACK (Acknowledgement) used when TCP/IP is used to establish a connection: confirm that the sent data has been accepted

Next, let's talk about the three-way handshake of TCP/IP:

The client sends the syn Packet (syn = j) to the server, enters the SYN_SEND status, waits for the server to confirm that the server receives the syn packet, and confirms the customer's syn (ack = j + 1 ), at the same time, you can also send a SYN Packet (syn = k ),
That is, the SYN + ACK packet. When the server enters the SYN_RECV status, the client receives the SYN + ACK packet and wants the server to send the ACK (ack = k + 1) Confirmation packet. After the packet is sent, the client and the server
Enter the ESTABLISHED status, complete the three-way handshake, and then the two start to transmit data

If you think it is complicated, you may have an impression. The three handshakes are as follows:

Http operation process:

The user clicks the url (Hyperlink) in the browser. After the Web browser establishes a connection with the Web server, the client sends a request to the server in the following format:
Unified Resource Identifier (URL) + Protocol version number (generally 1.1) + MIME information (multiple message headers) + a blank row server receives a request and returns the corresponding information, the returned format is:
Protocol Version Number + status line (processing result) + multiple information headers + blank lines + entity content (such as returned HTML) The client receives the information returned by the server and displays it in a browser, then disconnect from the server. Of course, if an error occurs during a certain step, the error message will be returned to the client and displayed, such as the classic 404 error.

Okay, No pic you say a jb. If there is a picture of the truth, we will use HttpWatch to capture the package (of course, you can also use Chrome f12 directly ), the website tested here is the educational administration system of pig school. after entering the account and password, the system sends a login request and the relevant information header. As for the relevant information header, we will explain it in detail next section ~ :

HTTP request content:

HTTP Response content:

④ Http request methods:

We can see that the above method of sending an http request is POST, and it and GET are used much in our usual development. Next we will list all the request methods:

Get: Request to Get the resource POST identified by Request-URI: append a new data HEAD to the resource identified by Request-URI to Request the response information header of the resource identified by Request-URI. PUT: The Request server stores a resource, use Request-URI as its identifier DELETE: Request the server to DELETE the resource TRACE identified by Request-URI: Request the server to send back the received Request information, mainly used to test or diagnose CONNECT: retain future OPTIONS: query the server performance or query resource-related OPTIONS.

Well, except for GET and POST, I have never used it. So here we only distinguish GET from POST:

GET: After the requested URL address? The data is carried to the server. Multiple Data are separated by &, but the data capacity cannot exceed 2 K. For example: http: // xxx? Username =... & Pawd =... This is get post: this can be used to send data to the server in the object content of the request. There is no limit on the transmission quantity. In addition, both of these two tools send data, but the sending mechanism is different, do not trust the "GET server data, POST Server Data "!!
In addition, the GET security is very low, and the Post security is high, but the execution efficiency is better than the Post method. Generally, we use GET when querying, and POST when adding, deleting, and modifying data !!
⑤ Http status code collection:
100 ~ 199: the request is successfully accepted. The client must submit the next request to complete the entire process. 200: OK. The client request is 300 ~ 399: the requested resource has been moved to the new address (302,307,304) 401: the request is not authorized. The status change code must be used with the WWW-Authenticate header domain 403: Forbidden. The server receives the request, however, if the requested resource does Not exist and the service is rejected 404: Not Found, the Error 500: Internal Server Error does Not occur. The Server has an unexpected Error 503: Server Unavailable, the server cannot process client requests at present, and may return to normal after a period of time
⑥ Features of Http

All right, you can just copy and paste these items ~

Supports the customer/Server mode. Simple and fast: when a customer requests a service from the server, only the Request Method and path are required. Common Request methods include GET, HEAD, and POST. Each method specifies the type of contact between the customer and the server. Because the HTTP protocol is simple, the program size of the HTTP server is small, so the communication speed is fast. Flexible: HTTP allows transmission of any type of data objects. The Type being transferred is marked by Content-Type. No connection: No connection means that only one request is allowed for each connection. After the server processes the customer's request and receives the customer's response, the connection is disconnected. This method can save transmission time.
5. Stateless: HTTP is stateless. Stateless means that the Protocol has no memory for transaction processing. The lack of status means that if subsequent processing requires the previous information, it must be re-transmitted, which may increase the amount of data transmitted by each connection. On the other hand, when the server does not need previous information, its response is faster.
7. OSI Layer-7 protocol & TCP layer-4 protocol;

Well, we can understand this. If you need it later, we can further understand that our HTTP requests come from the Seventh Layer: application layer,
Everyone knows. In addition, it is better to write down these seven layers. By the way, one to three layers are used to create physical connections between two network devices,
Layer 4 to Layer 7 is mainly responsible for interoperability. It is good to be familiar with it. You may ask for an interview, right? haha ~

OSI Layer-7 protocol diagram & TCP layer-4 model diagram:

In addition, when I checked the relevant information, I found a higher summary map of B cells. If you are interested, you can study it slowly:

Paste the original article link. If you want to answer the question, you can see:

Detailed description of OSI Layer-7 Model

Summary:

Well, because the time is too short, after all, it can only be written when I get off work. It may be a bit messy. I hope I can understand it. If I have time, I will try again, for some of the content, refer to the content of the "Android HTTP Communication" video of MOOC. If you are interested, you can go to MOOC. Well, let's summarize the following content:

This article introduces some concepts of the HTTP protocol, briefly introduces the process of distinguishing HTTP 1.0 from HTTP 1.1, including SYN and ACK, and three-way handshake of TCP/IP, HTTP operation process, several HTTP request methods, HTTP status code collection, and HTTP protocol features OSI Layer-7 protocol diagram & TCP layer-4 model Diagram

This section provides some conceptual things. For more information, see. In the next section, we will study different response headers. Our server will control browser behaviors by setting different response headers, for example, page orientation, timed refresh, and file download prompt. Please wait ~ Okay, take a bath and go to bed. Continue to work tomorrow ~

PS: My roommate passed by and asked me what I was deducting for one night? Do I write code? I want to write blogs. Then, he asked me if I could make money? I said no. Then he said, there was an egg... I don't know how to explain it to him. Everyone's views and values are different. I like to write blogs. Even though I am not a great guy, I have nothing to do with B, it may be basic things. Even so, I am still willing to bury myself in the code for one night after I finish my work. What a thousand miles, sustained, and persistent efforts will not be accumulated, coupled with the correct method, pig also became a great cow on that day. In addition, it is easy to write out the method to help later users. Why not? Right! I would like to share with all the siege lions ~ Fuel

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.