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

Source: Internet
Author: User
<span id="Label3"></p>Android HTTP communication--1. Introduction to the first knowledge HTTP Protocol: <blockquote> <p><p> Today is Children's day, first here to give you over-the-age children say 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, to write a thesis, to take a picture of graduation, to reply, to Resign. All sorts of things, too busy, ok. The pig is now working in a new company. The third Company. You will think that piglet half-hearted is, after all, almost the same graduation in exchange for so many companies, and 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 have no need to feed my family. Support the old man, and I don't have a girlfriend. haha ~ so take advantage of the young, choose the soil suitable for their growth. Learn more skills, wages in fact nothing, really, can learn something is the key, and so really have the Ability. At that time can talk about wages, such as my company's Daniel (only this week has been a job to the meizu). The first time I feel Daniel's breath, B-lattice really high. shame, they write their own components. push, various Code Encapsulation. Various design patterns, and will serve the end, Test. Ios... A little read in Fiction. A newly-started little apprentice saw a guru-level master when that feeling, Before. 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 were very impetuous. A variety 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, a lot of things are interlinked. It is 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 be graphic DESIGN. Web Design. ”。 How much experience do we need from the beginner to the quasi-professional level? "the genius in the eyes of the people," Gladwell in the book "alien". is not extraordinary, but to pay a continuous effort, 10,000 hours of temper is no matter what people from mediocrity into extraordinary requirements. </p></p> <p><p>"10,000 Hours. That means you work 8 hours a day, 5 days a week, and you need 5 Years. You need no genius, no iq, no three heads, no need for a long head, you just 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 just a graduate of the intern, not very normal ah, very many things, anxious not to Come. There are plans every day. Step by step, the Cumulative. Persist for 5 Years. or Not. I can also become a quasi-professional Daniel is it, so why low self-esteem it ~ ah, the soul of chicken soup is so much to Say. Drink too much wine has no effect, the video is good, there is interest to see ha, well, starting from today, the pig will be in the study of the company project at the same time summed up some of the relevant things, and then share to everyone, the first chapter explains the HTTP communication in android, the first description of the HTTP related concepts, Then we 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 implement responsive programming with Rxjava to make asynchronous request calls to the Network. of course, The pig is also learning among Them. Suppose there is something wrong in the Place. Welcome to Point out, Extremely grateful ~</p></p> </blockquote>Body:<strong><strong>What the hell is 1.Http?</strong></strong><strong><strong>① noun analysis:</strong></strong> <blockquote> <blockquote> <ul> <li>Hypertext Transfer Protocol (hypertext transfer protocol), an <strong>Application-layer protocol</strong> for the TCP/IP protocol that defines the process of exchanging data between a <strong>Web browser and a webserver</strong> .<p></p><p>After the client connects to webserver, to obtain a Web resource in webserver, a certain communication format is required, and the HTTP protocol is used to define the format of the client and webserver Communication.</p></li> </ul> </blockquote> </blockquote><strong><strong>the difference between ②http 1.0 and Http 1.1:</strong></strong> <blockquote> <blockquote> <ul> <li>1.0 Protocol. After the client has established a connection with webserver, it can only get a web resource!</li> <li>1.1 protocol, After the client has established a connection with webserver, get multiple Web resources on one connection!</li> </ul> </blockquote> </blockquote><p><p>PS: today most of the HTTP 1.1 protocol is used ~</p></p><strong><strong>Workflow for ③http requests:</strong></strong><p><p>Here we first introduce two nouns, syn and ACK</p></p> <blockquote> <blockquote> <ul> <li>SYN (synchronous): The handshake signal used when TCP/IP establishes a connection</li> <li>ACK (acknowledgement): Confirmation Character. Confirm that the data sent has been accepted without Error.</li> </ul> </blockquote> </blockquote><p><p>next, The concept of TCP/IP three-time handshake:</p></p> <blockquote> <blockquote> <ol> <li>The client sends the SYN packet (syn = J) to the server, enters the Syn_send state, and waits for the server to confirm</li> <li>The server receives the SYN packet, confirms the Customer's syn (ack = j + 1), and at the same time sends a SYN packet (syn=k) on its own,<br>That is, the SYN + ACK packet, the server enters the SYN_RECV state</li> <li>The client receives a SYN + ACK packet, wants the server to send a confirmation packet ack (ack = k + 1), after sending, the client and server<br>Enter the established State. Three handshake, then both start transmitting data</li> </ol> </blockquote> </blockquote><p><p>Suppose you think complex words, look at the picture you have a general impression, three times handshake such as the following:</p></p><p><p></p></p><p><p>Flow of HTTP Operations:</p></p> <blockquote> <blockquote> <ol> <li>The user clicks the URL (hyperlink) on the Browser. Web browser establishes a connection with webserver</li> <li>After the connection is Established. The client sends the request to the server in the form of the Request:<br>Uniform Resource Identifier (URL) + protocol version (typically 1.1) +mime information (multiple message Headers) + a blank line</li> <li>After the server has been Requested. Given the corresponding return information, the return format is:<br>Protocol version + status line (processing Result) + Multiple information header + blank line + entity content (for example, returned HTML)</li> <li>The client receives information from the server, displays it through the browser and disconnects from the server, assuming, of course, that the error message is returned to the client and displayed, for example, the classic 404 error, If a step error occurs halfway.</li> </ol> </blockquote> </blockquote><p><p>ok, No pic You say a jb, there is a picture of the truth, the following 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: input account password, Send login requests, related information head. As for the relevant information head, we will explain the next section of the specific ~:</p></p><p><p>What the HTTP request contains:</p></p><p><p></p></p><p><p>What the HTTP response contains:</p></p><p><p></p></p>Several ways to ④http requests:<p><p>We see above we send HTTP request way is post, it and get in our usual development use more, the following we list out all the request way:</p></p> <blockquote> <blockquote> <ul> <li>Get: request for the resource identified by Request-uri</li> <li>Post: append new data to the resource identified by Request-uri</li> <li>HEAD request Gets the response information header for the resource identified by Request-uri</li> <li>PUT: request that the server store a resource and use Request-uri as its identity</li> <li>Delete: Request Server to delete the resource identified by Request-uri</li> <li>TRACE: Request Server Loopback received request information, mainly for testing or diagnostics</li> <li>CONNECT: Keep Future use</li> <li>OPTIONS: request performance for query Server. or query for resource-related options</li> </ul> </blockquote> </blockquote><p><p>well, besides get and post, I didn't use it, so here's just the difference between get and post:</p></p> <blockquote> <blockquote> <ul> <li>Get: bring the data to the server in the form of the requested Url. Multiple data is separated by &, but the data capacity usually cannot exceed 2K, for example: http://xxx?<p></p><p>Username=...&pawd= ... So this is Get.</p></li> <li>POST: This is able to send data to the server in the requested entity content, with no limit on the number of transfers</li> <li>In addition to say, these two things are sending data, just the sending mechanism is not the same, do not believe that the internet said "get the server data, post to the server to send data"!!<br>In addition, get security is very low. Post security is high, but the operation efficiency is better than the post method, the general query when we use Get. Use post when data is changed or Deleted.</li> </ul> </blockquote> </blockquote>⑤http Status Code collection: <blockquote> <blockquote> <ol> <li>100~199: successful acceptance of the request, the client needs to submit the next request talent completed the entire process</li> <li>200:ok,client Request succeeded</li> <li>300~399: the request resource has been moved to the new address (302,307,304)</li> <li>401: the request is not Authorized. The Change status code needs to be used with the Www-authenticate header field</li> <li>403:forbidden. Server received the request but refused to provide service</li> <li>404:not Found, The request resource does not exist, this is needless to say.</li> <li>500:internal Server Error. Unexpected error occurred with server</li> <li>503:server Unavailable. Server is currently unable to process client Requests. May return to normal after a certain period of time</li> </ol> </blockquote> </blockquote>Features of the ⑥http protocol<p><p>All right. These things know to be able to, the pig will not slowly buckle, directly copy paste ha ~</p></p> <blockquote> <blockquote> <ol> <li>Support Customer/server Mode.</li> <li>Simple high speed: When a customer requests a service from the Server. Only the request method and path need to be Transmitted. The request method is often used with, and POST. Each method specifies that the customer has a different type of contact with the Server.<p></p><p>Because the HTTP protocol is simple, the Httpserver program is small, so the communication speed is very fast.</p></li> <li>Flexible: HTTP agrees to transfer arbitrary types of data Objects. The type being transmitted is marked by Content-type.<p></p><p></p></li> <li>No connection: the meaning of no connection is to limit each connection to just one Request. After the server finishes processing the Customer's request and receives the Customer's Response. The connection is Disconnected. In this way, the transmission time can be saved.<br>5. Stateless: The HTTP protocol is a stateless Protocol.<p></p><p>Stateless means that the protocol has no memory capacity for transactional processing.</p><p>A lack of state means that it must be re-transmitted if it is supposed to handle the information that is required earlier. This may result in an increase in the amount of data transferred per Connection. There is one more aspect. When the server does not need the previous information, it responds more quickly.</p></li> </ol> </blockquote> </blockquote>⑦osi Seven layer protocol &tcp four layer protocol;<p><p>well, We know that, and maybe if we need to know more, our HTTP request is out of the seventh tier: the application layer,<br>You know you can, 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.<br>And four to seven layer is mainly responsible for interoperability, memorize good, interview may ask, is it. haha ~</p></p><p><p>OSI Seven-layer Protocol diagram &tcp Four-layer model diagram:</p></p><p><p></p></p><p><p>In addition, the pig to check the relevant information, found a higher b-grid summary, interested in the ability to study slowly:<br></p></p><p><p>Paste the original link, want to look into the following:</p></p><p><p>The OSI seven layer model is explained in detail</p></p>Summarize:<p><p>well, because of the time is more hasty, after all, only can write after work, may be a bit chaotic, looking for understanding, there is time to smooth a smooth thinking. Part of the content of the Web "Android http communication" video content, interested in being able to take a look at the class. Well. finally, summarize the following in this section:</p></p> <blockquote> <blockquote> <ol> <li>This paper introduces some concepts of the next HTTP protocol,</li> <li>The difference between HTTP 1.0 and HTTP 1.1 is simply SAID.</li> <li>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</li> <li>OSI Seven-layer Protocol diagram &tcp Four-layer model diagram</li> </ol> </blockquote> </blockquote><p><p>This section is a few conceptual things, just Understand. In the next section, we'll look at different response Headers. Our service side controls some of the behavior of the browser by setting different response headers, for example, the page from Orientation. Timed Refresh. Prompt file download and so on, please look forward to ~ well. Take a bath and Sleep. Go to work tomorrow Hum (ˉ(∞)ˉ) ~</p></p><p><p>PS: just a friend passing by, ask me what to buckle all night? Write code, I say write bo. And then he asked, "is that going to make money?" I said No. Then he said, "there's an egg in it." I don't know how to explain to him, Everyone's Opinion. Different Values. I like to write bo, although I am not daniel, I write out of the things B is not high. May be some basic stuff. Even so, I am willing to eat after work after the meal, bury the code word one night, do not accumulate kuibu why thousands of Miles. Continued. 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 to facilitate the latter. Why not? Right!</p></p><p><p>With you all the Siege Lions to cheer up!</p></p> <p><p>Android HTTP communication--1. initial knowledge of HTTP protocol</p></p></span>
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.