NSURLConnection study notes, nsurlconnection

Source: Internet
Author: User

NSURLConnection study notes, nsurlconnection

Although we now use third-party libraries to obtain network data, we will also use Apple's official NSURLSession, but some things need to be learned before they can be qualified to say no, isn't it?

NSURLConnection sends requests in two ways: synchronous and asynchronous. synchronization, as the name suggests, means that the request must be processed immediately at the same time. Other operations will be blocked in the main thread. If the network request is time-consuming, this will affect the user experience. The synchronization method is as follows:

1 + (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error;

This is a class method that returns NSURLConnection. Why is there a returned value for synchronization asynchronous? Let's talk about it later.

 

Asynchronous requests will not block other operations of the main thread. There are two different data processing methods returned by the server:

Block callback

1 + (void)sendAsynchronousRequest:(NSURLRequest*) request queue:(NSOperationQueue*) queue                                                  completionHandler:(void (^)(NSURLResponse* response, NSData* data, NSError* connectionError)) handler;

Proxy

1 - (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate;2 3 - (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately;

Two object methods, the latter can control whether to request immediately. If NO is passed, the start method can be called to start the request when a request is needed later.

There is also a class method:

+ (NSURLConnection *)connectionWithRequest:(NSURLRequest *)request delegate:(id)delegate;

To act as a proxy for NSURLConnection, you must comply with the NSURLConnectionDataDelegate protocol instead of the NSURLConnectionDelegate protocol.

The proxy method is as follows:

Call-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response; when receiving the data returned by the server, call (when the data returned by the server is large, it will be called multiple times)-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data; call-(void) connectionDidFinishLoading :( NSURLConnection *) connection after the data returned by the server is fully received; Call (such as request timeout)-(void) connection :( NSURLConnection *) When a request error occurs *) connection didFailWithError :( NSError *) error;

 

Next let's take a look at different request methods.

GET: GET request data. parameters must be spliced in URLs. Therefore, do not use the GET Method for user information and other sensitive content.

POST: Use POST to request data. The parameters must be placed in the Request body, and the POST method must be used for upload operations.

Variable request NSMutableRequest must be used when POST is used.

Set the request timeout wait time (if this time is exceeded, the request fails)-(void) setTimeoutInterval :( NSTimeInterval) seconds; set the request method (such as GET and POST)-(void) setHTTPMethod :( NSString *) method; set the Request body-(void) setHTTPBody :( NSData *) data; set the request header-(void) setValue :( NSString *) value forHTTPHeaderField :( NSString *) field;

 

Note that if the request body (that is, the parameter) contains Chinese characters, UTF-8 must be used for encoding.

// GETNSString *urlStr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];// POSTrequest.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];

 

The reason why the asynchronous function does not return values is that the time for the asynchronous function to perform network requests is unknown. Therefore, you must wait until the data is returned to start further operations. If you directly use an object to receive the returned values, it may cause null values, so block or proxy is used to operate the returned data.

 

Related Article

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.