Android HTTP communication--1. Initial knowledge of HTTP protocol

Source: Internet
Author: User
Tags ack

Android HTTP communication--1. Initial knowledge of HTTP protocol

    • Android HTTP Communication 1 Initial knowledge of HTTP protocol
      • Introduction
      • Body
        • What the hell is http?
          • Noun analysis
          • The difference between HTTP 10 and HTTP 11
          • Workflow for HTTP requests
          • Several ways to request HTTP
          • HTTP Status Code Collection
          • Features of the HTTP protocol
          • OSI layer Seven protocol TCP four layer protocol
      • Summarize

Introduction:

Today is children's Day, first here to you over-the-age children say that the holiday happy ha ~ (╯-╰), the pig also symbolically to the group of children sent a red envelope ... Well, the busy May finally passed, to do the completion of the establishment, writing papers, film graduation, defense, resignation, all kinds of things, but also busy, OK, Piglet is now in a new company to work, the third company, you will certainly feel that the pig half-hearted is it, after all, after all, almost graduated to change so many companies, Then may ask me, "Piglet when you jump fourth company", Haha, should not ha, how to say, in fact, I am still very single-minded, after all, I just graduated, I do not need to feed the family, support the elderly, and I do not have a girlfriend, haha ~ So while young, choose suitable for their growing soil, learn more skills , wages actually nothing, really, can learn something is the key, and so really have the ability, that time can talk about wages, such as I This company's Daniel (but this week has been job-hopping to Meizu), the first time to feel the breath of Daniel, B lattice really high, shame, others write components, push, various code encapsulation, Various design patterns, and will serve the end, test, IOS ... A little look at the novel, a novice apprentice to see a guru level of the master when that feeling, once, piglet inferiority for a few days, have been thinking, I really can take over his project? Or read his code? Or can I write such a code? Those days, the whole people are very impetuous, all kinds of negative emotions, even the code can not see, and then inadvertently saw a talk about the design of the Ted speech, the title is: "How to become a good designer: Rozichon", although this is not to say programming, but in fact, many things are interlinked, divided into two stages: introductory & Quasi-Professional, "You want to be a designer, but more than that, you want to be a good designer." But no life down is excellent, no life down is a good, no life down will be UI design, will graphic design, will web design. "How much experience do we need from the beginner to the quasi-Professional level?" Gladwell in the Book of "Heterogeneous": "The Genius in the eyes of people is not extraordinary, but to pay a continuous effort, 10,000 hours of temper is anyone from ordinary to extraordinary requirements." "10,000 hours, which means you work 8 hours a day, 5 days a week, you need 5 years." You need no genius, no IQ, no three heads, no need for a long head, you only need to continue, insist on the efforts, have the right method, you can in the design field, a professional independently. "After reading, the whole person on the wide let cheerful, the heart also calm down, right ah, people do how long, I do how long?" People do five years, I am just a graduate intern, not very normal ah, a lot of things, anxious not to, every day planning, step by step, accumulate, adhere to 5 years, or not, I can become a quasi-professional Daniel is it, so whatWill Self-esteem it ~ Ah, the soul of chicken soup is so much, drink too much wine has no effect, the video is good, interested can see HA, well, starting today, the pig will be in the study of the company project at the same time summarize some of the relevant things, and then share to everyone, The first chapter is on the HTTP communication in Android, first describe some of the HTTP related concepts, and then talk about the two ways to send HTTP requests on Android: HttpURLConnection and HttpClient, Then use the retrofit framework to encapsulate our HTTP requests, and finally use Rxjava to implement responsive programming, network asynchronous request calls, of course, Piglet is also in the study, if there is something wrong, welcome to point out, extremely grateful ~

Body: What the hell is 1.Http? ① noun Analysis:
  • Hypertext Transfer Protocol (Hypertext Transfer Protocol), an application-layer protocol for the TCP/IP protocol that defines the process of exchanging data between a Web browser and a Web server . After the client connects to the Web server, if you want to obtain a Web resource in the Web server, you need to follow a certain communication format, the HTTP protocol is used to define the format of the client and Web server communication.
the difference 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 that allows clients to connect to a Web server and get multiple Web resources on a single connection!

PS: Now most of the HTTP 1.1 protocol is used ~

Workflow for ③http requests:

Here we first introduce two nouns, syn and ACK

  • SYN (synchronous): The handshake signal used when TCP/IP establishes a connection
  • ACK (Acknowledgement): confirms the character and confirms that the sent data has been accepted correctly

Next, the concept of TCP/IP three-time handshake:

  1. The client sends the SYN packet (syn = j) to the server, enters the Syn_send state, and waits for the server to confirm
  2. The server receives the SYN packet, confirms the customer's SYN (ack = j + 1), and also sends a SYN packet (SYN=K) on its own,
    That is, the SYN + ACK packet, the server enters the SYN_RECV state
  3. The client receives a SYN + ACK packet, wants the server to send a confirmation packet ack (ACK = k + 1), after sending, the client and the service side
    Enter the established state, complete three handshake, and then both start transmitting data

If you feel complicated, look at the picture you have a general impression, three times the handshake is as follows:

Flow of HTTP operations:

  1. The user clicks the URL (hyperlink) on the browser, and the Web browser establishes a connection to the Web server
  2. After the connection is established, the client sends a request to the server in the format of the request:
    Uniform Resource Identifier (URL) + protocol version number (typically 1.1) +mime information (multiple message headers) + a blank line
  3. After the service side is requested, the return information is given in the following format:
    Protocol version number + status line (processing result) + Multiple information header + blank line + entity content (for example, returned HTML)
  4. The client receives the service-side return information, displays it through the browser, and disconnects from the server, of course, if an error occurs halfway through a step, the error message is returned to the client and displayed, for example: Classic 404 error

OK, No pic You say a JB, there is a picture of the truth, below we use HttpWatch to grab the bag (of course, you can also directly use Chrome F12), here the test site is the school's educational system: After entering the account password, send login request, related information head, As for the relevant information head, we will explain in detail in the next section ~:

What the HTTP request contains:

What the HTTP response includes:

Several ways to ④http requests:

We see above we send HTTP request way is post, it and get in our normal development use more, below we list all request way:

  • Get: Request for the resource identified by Request-uri
  • Post: Append new data to the resource identified by Request-uri
  • HEAD request Gets the response information header for the resource identified by Request-uri
  • PUT: The request server stores a resource and uses Request-uri as its identity
  • Delete: The request server deletes the resource identified by the Request-uri
  • TRACE: Requests the server to echo received request information, primarily for testing or diagnostics
  • CONNECT: Keep Future use
  • Options: Request performance of the query server or query for resource-related options

Well, besides get and post other, the author has not used, so, here only the difference between get and post:

  • Get: After the requested URL address in the form of the data given to the server, a plurality of data separated by &, but the data capacity is usually not more than 2K, such as: http://xxx?username=...&pawd= ... This is get.
  • POST: This can send data to the server in the requested entity content, with no limit on the number of transfers
  • Another thing to say, these two things are sending data, but the sending mechanism is not the same, do not believe that the internet said "Get Get server data, post to the server to send data"!!
    In addition, get security is very low, post security is higher, but the execution efficiency is better than the Post method, the general query when we use GET, data additions and deletions with post!!
⑤http Status Code collection:
  1. 100~199: Successful acceptance of the request, the client must submit the next request to complete the process
  2. 200:ok, client request succeeded
  3. 300~399: The request resource has been moved to the new address (302,307,304)
  4. 401: Request not authorized, change status code to be used with Www-authenticate header domain
  5. 403:forbidden, the server receives the request but refuses to provide the service
  6. 404:not Found, the request resource does not exist, this is needless to say.
  7. 500:internal server error, unexpected errors occurred on the servers
  8. 503:server unavailable, the server is currently unable to process client requests and may return to normal after some time
Features of the ⑥http protocol

Well, these things to know, the pig will not slowly buckle, directly copy and paste ha ~

  1. Supports client/server mode.
  2. Simple and fast: When a customer requests a service from the server, it simply transmits the request method and path. The request method commonly has, POST. Each method specifies a different type of contact between the customer and the server. Because the HTTP protocol is simple, the HTTP server's program size is small, so the communication speed is fast.
  3. Flexible: HTTP allows the transfer of any type of data object. The type being transmitted is marked by Content-type.
  4. No connection: The meaning of no connection is to limit the processing of only one request per connection. When the server finishes processing the customer's request and receives the customer's answer, the connection is disconnected. In this way, the transmission time can be saved.
    5. Stateless: The HTTP protocol is a stateless protocol. Stateless means that the protocol has no memory capacity for transactional processing. A lack of state means that if the previous information is required for subsequent processing, it must be re-routed, which may cause the amount of data to be transferred per connection to increase. On the other hand, it responds faster when the server does not need the previous information.
⑦osi Seven layer protocol &tcp four layer protocol;

Well, we know that we can, follow the need to further understand, our HTTP request is out of the seventh layer: the Application layer,
You know it, and it's better to write down these seven layers, yes, one to three layers is used to create a physical connection between two network devices,
And four to seven layer is mainly responsible for interoperability, memorize good, interview may ask, yes, haha ~

OSI seven-layer protocol diagram &tcp four-layer model diagram:

In addition, the little pig to check the relevant information, found a higher B-grid summary, interested can slowly study:

Paste the original link, want to get to know the following:

OSI seven-layer model detailed

Summarize:

Well, because the time is more hasty, after all, can only work when writing, may be a little messy, look for understanding, there will be time to re-draw a smooth thinking, part of the content of the network's "Android HTTP Communication" video content, interested can go to MU class to see, well, finally summed up the east of this section:

  1. This paper introduces some concepts of the next HTTP protocol,
  2. The difference between HTTP 1.0 and HTTP 1.1 is easy to say.
  3. HTTP request flow, including the concept of SYN and ACK, TCP/IP three-time handshake, HTTP operation Flow, HTTP request several ways, HTTP status Code collection, and HTTP protocol features
  4. OSI seven-layer protocol diagram &tcp four-layer model diagram

This section is a few conceptual things, to understand the good, the next section, we have to study the different response headers, our service side by setting different response headers to control some of the browser behavior, such as page from the orientation, timed refresh, prompt file download, etc., please look forward to-good, bath to sleep, tomorrow continue to work hum (ˉt ∞) ˉt ) Jacks ~

PS: Just a friend passing by, ask me what to buckle all night? Write the code, I said to write Bo, then he asked this can make money? I said no, then he said, there's an egg in it. I do not know how to explain to him, everyone's opinion, the values are different, I like to write Bo, although I am not Daniel, I write out of the things B is not high, may be some of the basis of things, however, I am willing to eat after work after dinner, Bury code word one night, do not accumulate kuibu why thousands of miles, continuous Adhere to the efforts, coupled with the correct method, the pig also has become the day of Daniel, in addition, to find out the method of writing, but also convenient later, why not? Right! With you all the siege Lions to cheer up!

Android HTTP communication--1. Initial knowledge of HTTP protocol

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.