iOS Development-http Protocol

Source: Internet
Author: User

HTTP protocol, the Hypertext Transfer Protocol (Hypertext Transfer Protocol).

HTTP is an application-layer protocol that consists of requests and responses and is a standard client server model. HTTP is a stateless protocol.

All transmissions in the Internet are made through TCP/IP. The HTTP protocol is no exception to the protocol used as the application layer in the TCP/IP model. The HTTP protocol is usually hosted on top of the TCP protocol, sometimes hosted on the TLS or SSL protocol layer, which is what we often call HTTPS.

The main features of the HTTP protocol can be summarized as follows:
1, support client/server mode. Support Basic authentication and security certification.
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 objects. The type being transmitted is marked by Content-type.
4, HTTP 0.9, and 1.0 use a non-persistent connection: Restricts each connection to only one request, the server finishes processing the customer's request, and receives the customer's response, which disconnects the connection. In this way, the transmission time can be saved. HTTP 1.1 uses persistent connections: You do not have to create a new connection for each Web object, and one connection can transfer multiple objects.
5, stateless: 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.

The HTTP protocol is a stateless and connection:keep-alive difference:
Stateless means that the protocol has no memory capacity for transactions, and the server does not know what the client state is. On the other hand, there is no connection between opening a Web page on a server and the pages you have previously opened on this server.
HTTP is a stateless, connection-oriented protocol, and stateless does not mean that HTTP cannot maintain TCP connections, nor does it use the UDP protocol (no connection) on behalf of HTTP.
From http/1.1 onwards, the default is to open the keep-alive, to maintain the connection characteristics, in short, when a Web page opens, the client and server for the transmission of HTTP data between the TCP connection will not be closed, if the client again access to the Web page on this server, will continue to use this established connection.
Keep-alive does not permanently keep the connection, it has a hold time that can be set in different server software (such as Apache).

For requests:

Request line, message header, request body.

For response:

Status line, message header, response body.

Resolving an HTTP stateless issue

7.1. Save status information via cookies

With cookies, the server can clearly know that request 2 and request 1 are from the same client.

7.2. Save status information through session

The session mechanism is a server-side mechanism that uses a hash-like structure (or perhaps a hash table) to hold information.

HTTPS protocol

1, the client initiates an HTTPS request connection and the server returns the public key;

2, the client generates its own key and public key, encrypts the client's key with the server's public key;

3, the client sends the encrypted key to the server;

4, the client encrypts its own information to communicate with its own public key.

Here are the HTTP request codes for your own development:

1-(void) Sendrequestwithpost: (NSString *) str Param: (nsdictionary *) Param success: (void(^) (ID)) Success2 3 {4 5Afhttpsessionmanager *manager =[Afhttpsessionmanager manager];6 7Manager.requestSerializer.timeoutInterval =Ten;8 9     if(!self.background) {//Display load tips, irrelevant httpTen  Onensstring* str =@"Loading in ..."; A  -         if(self.myloadingstrings) { -  thestr =self.myloadingstrings; -  -         } -  + [self presentloadingtips:str]; -  +     } A  at      -  -Manager.responseSerializer.acceptableContentTypes = [Nsset setwithobjects:@"Application/json",@"text/html",@"Text/json",@"Text/javascript",@"Text/plain", nil]; ACCEPT Type Settings -  -     if(log_able) { -  inLylog (@"%@ request with%@", str, [param description]); -  to     } +  -   the  *[Manager post:str Parameters:param Progress:nil success:^ (nsurlsessiondatatask * _nonnull task,ID_nullable Responseobject) { $ Panax Notoginseng         if(!self.background) { -  the [self dismisstips]; +  A         } the  +         if(log_able) { -  $Lylog (@"%@ \ Response:%@", str, [responseobject description]); $  -         } -  the success (responseobject); - Wuyinsdictionary* dict =Responseobject; the  -         if([Dict Iskindofclass:[nsdictionaryclass]] && [dict objectforkey:@"Msg_code"]) { Wu  -nsnumber* code = [Dict objectforkey:@"Msg_code"]; About  $             if(Code.integervalue = =20013) {//Session fail -  -                  -  A [self lymodalloginviewcontroller]; +  the             } -  $         } the  the} failure:^ (Nsurlsessiondatatask * _nullable task, Nserror *_nonnull Error) { the  the         if(!self.background) { -  in [self dismisstips]; the  the[Self presentmessagetips:@"operation failed"]; About  the         } the  theLylog (@"Error:%@", error); +  -Lylog (@"opertaion%@ Error", [task description]); the Bayi         if(Error.code = =kcfurlerrorbadserverresponse) {//Specific code processing logic the  the [self lymodalloginviewcontroller]; -  -         } the  the     }]; the  the } -  the  

Tool:Wireshark

Reference

http://blog.csdn.net/gueter/article/details/1524447

Http://www.cnblogs.com/EricaMIN1987_IT/p/3837436.html

iOS Development-http Protocol

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.