Network Request related Summary 2

Source: Internet
Author: User

1 nsurlconnettion

Nsurlconnettion is an abstraction on top of the Core foundation/cfnetwork framework API. nsurlconnettion is used to refer to a series of components in the Foundation framework: Nsurlrequest,nsurlresponse,nsurlprotocol,nsurlcache, Nshttpcookiestorage,nsurlcredentialstorage and the same name class Nsurlconnection.

Note 1: The original principle of use: a nsurlrequest sent to Nsurlconnect. Entrusted objects (subject to nsurlconnectiondeledate and nsurlconnectiondatadeledate informal agreements) Asynchronously returns the NSData of a nsurlresponce and a server.

Note 2: When a request is sent to the server, the system queries the cache information and then, depending on the policy and available writes, returns directly when the response is found in the cache. Otherwise, the system will cache the response to the request and return it based on our policy.

Note 3: When the request is sent to the server, the server will issue a authentication query, which can either have a shared cookie or a secret store to respond automatically, or by the object being delegated. The request can also be intercepted by Nsurlprotocol to change its request seamlessly when necessary. .

nsurlconnettion are the steps to use:

1 Get a URL

2 Creating a request from a URL

3 sending requests with Nsurlconnettion

2 nsurlsession

Like Nsurlconnection, nsurlsession not only includes nsurlsession of the same name, but also Nsurlrequest and Nsurlcache. At the same time, the nsurlconnection corresponds to the three subclasses of Nsurlsession,nsurlsessionfiguration and nsurlsession, namely Nsurlsessiontask, Nsurlsessionuploadtask and Nsulsessiondownloadtask.

Compared to Nsurlconnection, Nsurlsession's biggest change can be configured to share this information with each cached session cache, cookie value, protocol, and certificate policy, or even kua programs. This ensures that the program and the network framework are independent of each other. every Nsurlsession has a nsurlsessionconfiguration to initialize, nsurlsessionconfiguration specifies the policy, and the option to enhance performance on mobile devices.

Another feature of Nsurlsession is the session Task, which is used to process uploads and downloads. The biggest difference with nsurlconnection is that all tasks share their creator nsurlsession.

Note: Nsurlsessiontask analysis

Nsurlsessiontask is an abstract class that contains Nsurlsessiondatatask, Nsurlsessiondownloadtask, and Nsurlsessionuploadtask three sub-classes. These three subclasses encapsulate the basic tasks of the network: Get data, upload and download

Principle of Use: When a nsurlsessiondatatask is completed, there will be a return data, and a nsurlsessiondownloadtask will take the path of a temporary file when it is completed. When the file is uploaded it will also return certain data, so it can be said Nsurlsessionuploadtask is inherited from Nsurlsessiondatatask. All tasks can be canceled, paused and resumed, and when paused, the current location is logged so that the download continues to download from this point. What needs to be explained is Nsurlsessiontask is created with Nsurlsession.

nsurlsession Use steps: basically similar to nsurlconnection, and then use the Resume method to run it.

1 Get a URL

2 Creating a request from a URL

2.1 Create an uploaded nsdata (UpLoad upload)

3 Creating a single case of nsurlsession

4 sending requests via nsurlsession (Note: using resume)

Instance:

1 Datatask

Nsurl *url = [Nsurl urlwithstring:@ "http://example.com"];

Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];

Nsurlsession *session = [Nsurlsession sharedsession];

Nsurlsessiondatatask *task = [Session Datataskwithrequest:request

Completionhandler:

^ (NSData *data, Nsurlresponse *response, Nserror *error) {

// ...

}];

[Task resume];

2 Uploadtask

Nsurl *url = [Nsurl urlwithstring:@ "Http://example.com/upload"];

Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];

NSData *data = ...;

Nsurlsession *session = [Nsurlsession sharedsession];

Nsurlsessionuploadtask *uploadtask = [Session Uploadtaskwithrequest:request

Fromdata:data

Completionhandler:

^ (NSData *data, Nsurlresponse *response, Nserror *error) {

// ...

}];

[Uploadtask resume];

3 Downloadtask

Note: The Download task also requires a request, except that the block is Completionhandler. The data task and the upload task are returned one time when the task is completed, but the Download task is a little bit of writing to the local temporary file. So in Completionhandler this block, we need to move the files from a temporary address to a permanent address to save them.

Nsurl *url = [Nsurl urlwithstring:@ "Http://example.com/file.zip"];

Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];

Nsurlsession *session = [Nsurlsession sharedsession];

Nsurlsessiondownloadtask *downloadtask = [Session Downloadtaskwithrequest:request

Completionhandler:

^ (Nsurl *location, Nsurlresponse *response, Nserror *error) {

NSString *documentspath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask, YES) Firstobject];

Nsurl *documentsdirectoryurl = [Nsurl Fileurlwithpath:documentspath];

Nsurl *newfilelocation = [Documentsdirectoryurl urlbyappendingpathcomponent:[[response URL] lastPathComponent]];

[[Nsfilemanager Defaultmanager] copyitematurl:location tourl:newfilelocation Error:nil];

}];

[Downloadtask resume];

3 The difference between nsurlsession and nsurlconnection

Nsurlsession is the proxy method of the session and the proxy method of the task. The proxy method of the session is used to deal with the problem of the connection layer (trust of the server, evaluation of the client certificate, etc.), and the task's proxy method is used to deal with the authentication query and the problem related to the network request.

4 AFN Frame

AFN is a layer of encapsulation of the nsurlconnection

Main functions of AFN:

    • Nsurlconnection

Afurlconnectionoperation

Afhttprequestoperation

Afhttprequestoperationmanager (Encapsulation of common HTTP methods)

Properties of the Afhttprequestoperationmanager

1 BaseURL: Developer pin to customize a single example class for Afhttprequestoperationmanager, set BaseURL, all network access, use only relative paths.

2 Requestserializer: Request data Format/default is binary HTTP

3 Responseserializer: Response data Format/default is JSON format

4 Operationqueue

5 Reachabilitymanager: Network Connection Manager

Afhttprequestoperationmanager Method:

1 Manager: Easy class method for creating manager

2 Httprequestoperationwithrequest: When accessing the server, if you want to tell the server some additional information, you need to set in Request

3 GET

4 POST

    • Nsurlsession

Afurlsessionmanager

Afhttpsessionmanager (Encapsulation of common HTTP methods)

1 GET

2 POST

3 uikit+afnetworking Classification

4 nsprogress (using KVO)

    • Semi-automatic serialization and deserialization

Afurlrequestserialization: The requested data format/default is binary

Afurlresponseserialization: Data format for response/default is JSON format

    • Additional Features

1 Security Policy: (HTTPS and Afsecuritypolicy)

2 Network detection: (Encapsulation and Afnetworkingreachabilimanager of the link mode)

Use Link: http://www.cnblogs.com/worldtraveler/p/4736643.html

AFN Steps to use:

1 Create a Request Operations Manager

2 Declaration response results are json,xml and other data parsing, return

3 Setting Request Parameters

4 Sending requests

Supplemental ASI: (very powerful, but not updated)

ASI Use steps:

1 Get URL

2 Getting the ASI request object

3 Sending requests

Network Request related Summary 2

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.