Network request for iOS network programming (i)

Source: Internet
Author: User

Small knowledge:

1, HTTP protocol, Hyper Text Transfer Protocol (Ultra-This Transfer Protocol) is the Transfer protocol from the Web server to the local browser, HTTP is the? Layer protocol, which consists of a request and a response, is a standard client server model.

2,url, the resource Locator, also known as the site, is a standard resource on the site.

URL character Syntax: protocol://authorization/path? query

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

Network programming under 3,ios is divided into three main steps:

1, the client sends the request

How to send requests:GET and POST

GET Request: The information is directly stitched to the back of the URL , in real development with more

POST Request: Converts some important information into a binary stream. In real development is often used to verify the user name, password is correct, upload data to the server, etc.

Note:1), the request method is determined by the background, through the interface document can know the way of the request

2), the request method also has DELETE,PUT and so on. In addition to get can use nsurlrequst , other requests must use Nsurlmutablerequst to explicitly specify the current HTTP request Mode

2, connect with server

3, the server responds

One or two ways of requesting

1, transfer data to the server type: Get: Through the address string.

POST: Via Data

2, transmission of data?? : GET: Address string up to 255 bytes.

POST: Make? NSData, capacity over 1G

3, Security: Get: All data transmitted to the server, explicit? In the address, similar to the password of the Ming and the loss, directly can?

POST: The data is transferred to NSData (????), similar to the secret of the password, the method of direct reading.

Second, GET request

get the requested URL

Nsurl *url = [nsurl urlwithstring: URL string];

encapsulate Request Object

Nsurlrequest *requst = [Nsurlrequest Requestwithurl:url];

Send Request

Nsurlresponse *response = nil;

Nserror *error = nil;

NSData *data = [nsurlconnection sendsynchronousrequest:requst returningresponse:&response error:&error];

  /*

Request method

[Nsurlconnection sendsynchronousrequest: (Nsurlrequest *) Returningresponse: (Nsurlresponse *) Error: (NSError *)]

The following three parameters

Nsurlrequst

Nsurlresponse

Nserror

The last two parameters are addressed for the sake of the word. When you execute the method, the value of the parameter is modified inside the method, This usage is equivalent to having multiple return values for a method

*/

Parse the received data (the received data type depends on the return type)

Nsdictionary *dic = [nsjsonserialization jsonobjectwithdata:data options: (Nsjsonreadingmutablecontainers | nsjsonreadingmutableleaves) Error:nil];

Third, POST request

GET request URL

Nsurl *url = [nsurl urlwithstring: URL string];

encapsulate As Object

nsmutableurlrequest *requst = [nsmutableurlrequest requestwithurl:url cachepolicy: ( Nsurlrequestuseprotocolcachepolicy) timeoutinterval:10];

/*

parameters of the upper-face method:

1, url URLs

2, cachepolicy: Caching mechanism, general use of nsurlrequestuseprotocolcachepolicy

3, timeoutinterval timeout mechanism

*/

Send Request

[Requst sethttpmethod:@ "POST"];

NSData *data = [string requiring encryption datausingencoding:nsutf8stringencoding];

[Requst Sethttpbody:data];

Nsurlresponse *response = nil;

Nserror *error = nil;

NSData *contentdata = [nsurlconnection sendsynchronousrequest:requst returningresponse:&response error:& ERROR];

Data Parsing

Nsdictionary *dic = [nsjsonserialization jsonobjectwithdata:contentdata options: (nsjsonreadingmutableleaves | nsjsonreadingmutablecontainers) Error:nil];

Network request for iOS network programming (i)

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.