IOS-Reading Notes-Web requests

Source: Internet
Author: User

Do you know the "3-time handshake"? Suddenly remembered the word

What is a 3-time handshake?
TCP three-time handshake/four wave details here is a detailed explanation of the 3-time handshake
The 3-time handshake is to reliably transmit data, TCP (what is TCP?). TCP is a reliable transport protocol) 3 handshakes between two hosts before data is sent. The data is sent after the 3-time handshake is complete.

Preliminary knowledge: The bit code is the TCP flag bit, there are 6 kinds of signs: SYN (synchronous set up) ACK (acknowledgement acknowledgment) PSH (push transfer) FIN (finish) RST (reset reset) URG ( Urgent emergency)
Sequence number (sequential) Acknowledge number (confirmation)
First handshake: Host a send bit code for syn=1, randomly generate SEQ number=1234567 packet to the server, Host B by Syn=1 know, a requirements to establish online;
Second handshake: Host B receives the request to confirm the online information, send an ACK to a number= (host A's seq+1), syn=1,ack=1, randomly generate seq=7654321 packets
Third handshake: Host a after receiving check ACK number is correct, that is, the first sent Seq Number+1, and the bit code ACK is 1, if correct, host A will send an ACK number= (Host B seq+1), ack= 1, Host B is received after confirming the SEQ value and ack=1 The connection was established successfully.
To complete the three handshake, host A and Host B start transmitting data.

Go to the Chase:
The preferred way to communicate in IOS is HTTP. The most convenient network API available for iOS is also for HTTP.
The above-mentioned "three handshake" is to establish a TCP connection, the HTTP request and response is done on the TCP connection, the TCP connection can be reused, that is, not every HTTP request to establish a TCP connection, as long as the TCP connection continues to open, HTTP requests can be used all the time.

Where are the differences between HTTP and HTTPS?
Safety. In fact, the HTTPS protocol is the SSL+HTTP protocol. The most important difference between HTTPS and HTTP is the session setup phase. After the TCP connection is established, SSL must be established between the client and the server before the HTTP request is sent (so what is SSL?). below) session. After the session is established, all data transmitted over the TCP connection is encrypted.

What is SSL?
SSL (Secure Socket Layer) is a protocol that guarantees secure communication between the server and the client. With the HTTPS protocol, all HTTP data is transmitted over the SSL protocol package. Suddenly came out another word TLS, in fact, TLS is the product of SSL standardization, in fact, we are now using TLS, but because of the history of the use of SSL as the usual or SSL-based. Introduction to SSL look at this article. An overview of the operating mechanism of the SSL/TLS protocol

IOS top-Level HTTP API

Nsurl, Nsurlrequest, nsurlconnection, Nsurlresponse objects

1 Nsurl

The Nsurl object provides easy access to the content that the URL points to, and Nsurl can point to a file resource or to a network resource.
Nsurl *url = [Nsurl urlwithstring:@ ""];
NSData *data = [NSData Datawithcontentsofurl:url];
That is, access to network resources and file resources can be obtained.

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    NSInputStream *inStream = [NSInputStream inputStreamWithFileAtPath:@"文件路径"];    setHTTPBodyStream:inStream];

Nsurl also provides a number of ways to read the values of the various parts of the URL, such as Url.port get the port of the URL.
The URL string needs to be percent-encoded before the Nsurl object is created. The RFC3986 documentation stipulates that only English letters (A-ZA-Z), Numbers (0-9),-_.~4 special characters, and all reserved characters are allowed in the URL. For more detailed look at the percent code

2 nsurlrequest

After you create the Nsurl object, you need to do the next step: Create the Nsurlrequest object. The Nsurlrequest object contains the information required to load the content of the URL. The URL loading system in iOS supports loading of HTTP, HTTPS, FTP, and file URL content
The URL loading system provides a way to extend a new protocol by creating a subclass of Nsurlprotocol and then providing the returned results to the URL loading system.

NSURL *url = [NSURL URLWithString:@"www.baidu.com"];    NSURLRequest *request = [NSURLRequest requestWithURL:url];

Use the default parameters to create the object. If it is HTTP or HTTPS then the request method is get and uses the default header provided by the operating system.

NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];

Setting the cache policy and the request time, Nsurlrequest also provides many ways to get the requested properties. But can't set, if want to set, then come with Nsmutablerequest.

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    [request setHTTPMethod:@"POST"];    [request setHTTPBody:[@"body" dataUsingEncoding:NSUTF8StringEncoding]];    [request setAllHTTPHeaderFields:@{@"":@""}];

Nsmutablerequest set various parameters.
Sending a bulk content can use Nsinputsteam, which provides the request body without loading the entire content into memory. Send a picture or video this is the best choice.

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    NSInputStream *inStream = [NSInputStream inputStreamWithFileAtPath:@"文件路径"];    setHTTPBodyStream:inStream];

3 nsurlconnection

The Nsurlconnection object is the center of the URL load activity. Used to connect to the server to get a response. Provides synchronous, asynchronous, queue-asynchronous operation modes. Synchronization is the simplest, but there are drawbacks such as clogging threads. The delegate agent is called in asynchronous mode to process the data.

4 Nsurlreponse

The Nsurlresponse object is returned after the URL load request is complete. The Nserror object and the Dsdata object, if a request occurs, or a server problem returns a Nserror object, the response body is included if a response returns a NSData object.
Nsurlreponse provides a lot of property information.
MIMEType the MIME type of the resulting data. This value originates from the server.
Expectedcontentlenth returns the size of the content, which may not match the actual.
Suggestedfilename provides the file name, either from the file name of the server-provided content, or from the URL and MIME type.
The URL of the URL that returns the content. Because of the orientation, the URL may be different from the URL that the request provides.
Textencodingname the text encoding name used for the original data source.
Hshttpurlresponse is a subclass of Nsurlresponse that contains properties that are specific to HTTP requests.
Response Head-Allheaderfields
HTTP status code-StatusCode

Article from the iOS Advanced Programming literacy

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS-Reading Notes-Web requests

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.