IOS UI Advanced Network Programming (HTTP protocol)

Source: Internet
Author: User

The concept of the HTTP protocol

HTTP protocol, Hyper text Transfer Protocol (Hypertext Transfer Protocol) is a transport protocol used to transmit hypertext to a local browser from a World Wide Web server, HTTP is an application-layer protocol, consisting of requests and responses, is a standard client server model

Understanding C/S Mode

Client and server (server) are often located on two separate computers, the client program's task is to submit the user's request to the server program, and then display the results returned by the server program to the user in a specific form The task of the server program is to accept the service request from the client program, handle it accordingly, and return the result to the client program.

iOS for Network programming

How HTTP protocol requests are implemented

The function and usage of network request address object Nsurl

The function and usage of Network Request object Nsurlrequest, Nsmutableurlrequest

The function and usage of network link object nsurlconnenction

The function and usage of Network link protocol nsurlconnectiondatadelegate

The function and usage of network request data information Nsurlresponse

Classes used in Network connections

We will encounter five classes on network connection in the following learning process, namely: Nsurl, Nsurlrequest, Nsmutableurlrequest, Nsurlresponse, Nserror.

Nsurl: This class encapsulates a network path.

Nsurlrequest: We will set up some network request information through this class.

Nsmutableurlrequest: This class is a subclass of Nsurlrequest, you can set some request parameters

Nsurlresponse: This class can accept some information returned from the server

Nserror: This class contains the error message we encountered in the network request

Nsurl

URL, Uniform Resource Locator, also known as Web address, standard resource URL on the Internet

A typical url:http://lily:[email protected]/search?hl=en&source=hp&q=mysql&aq=f&oq=&aqi=g10#page.

URL character Syntax: protocol://authorization/path? Query Note: A folder in the path server after URLs in URLs? followed by parameters, multiple parameters are linked through & (and)

Protocol: ftp://(Text Transfer Protocol)/HTTP (Hypertext Transfer Protocol)

https://(Secure Hypertext Transfer Protocol) file://(local file protocol)

How URLs are requested

There are two ways of network request: GET and POST

The same points in both ways are:

Can transmit data to the server.

The differences between the two request modes are:

1, the way to transfer data to the server:

GCT: Via URL string

POST: Via Data

2, the size of the transmitted data:

GET: URL string up to 255 bytes

POST: Using NSData, capacity exceeds 1G

3. Security:

GET: All data transmitted to the server, displayed in the URL, similar to the plaintext input of the password, directly visible

POST: Data is turned into nsdata (binary data), similar to cipher ciphertext input, cannot be read directly

Connection mode

Synchronization connection: Program prone to deadlock phenomenon

Asynchronous link: Waiting for data to return

Asynchronous links are implemented in two ways:

Set up agents to accept data

Implement block

Synchronous connection

A synchronous connection request allows you to request data from the Internet, and once the synchronization request is sent, the program stops the user interaction until the server returns data to complete before proceeding to the next step.

First we set up a Tableviewcontroller view controller in method Viewdidload we'll turn the URL string into a Nsurl object, then create a Nsurlrequest request object based on the Nsurl object, and then set up a synchronous link. And then parse the information through Jeson.

Asynchronous connections

Synchronous connection causes the program to "die" phenomenon, resulting in user interaction is not friendly, and asynchronous links just change this disadvantage, the asynchronous connection does not block the main thread, and will establish a new thread to operate, so the user after sending out an asynchronous request can still operate the UI, the program can continue.

Asynchronous connection (proxy)

Setting up the Nsurlconnection agent

Implement the appropriate Proxy method: Start responding to accepting data, receiving data, success, failure

After the URL is created and the proxy is specified, the corresponding method in the agent is implemented as follows:

Asynchronous connection (block)

IOS UI Advanced Network Programming (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.