iOS elearning------3 implementing asynchronous post requests with non-proxy methods

Source: Internet
Author: User

#pragma mark-This is a private method. Try not to use the attribute directly in the method, since the general attribute is associated with the interface, we can use the attribute #pragma mark post Sign-in Method-(void) Loginwithpostwithname: (NSString *)    UserName pwd: (NSString *) pwd{//1 determine address nsurl nsstring *urlstring = [NSString stringwithformat:@ "www.baidu.com"];        Nsurl *url = [Nsurl urlwithstring:urlstring];    2 Build Request Nsmutableurlrequest (post requires this) nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];    Network access timeout time [request settimeoutinterval:2.0f];    1) The Post request method, the network request is the Get method by default, so suppose we use the POST request, must declare the request way.    [Request sethttpmethod:@ "POST"]; 2) The data body of the POST request, when the data body in the post request, is assumed to have Chinese, does not need to convert.    Since the Atausingencoding method has been implemented transcoding.    NSString *bodystr = [NSString stringwithformat:@ "username=%@&password=%@", username, pwd];    Convert nstring to NSData nsdata *body = [Bodystr datausingencoding:nsutf8stringencoding];    NSLog (@ "Body data%@", body);        [Request Sethttpbody:body];    Here is a non-proxy asynchronous request, and the asynchronous request does not prevent the main thread from continuing to run, without waiting for the network to end. [Nsurlconnection sendasynchronousrequest:request Queue:[nsoperAtionqueue Mainqueue] completionhandler:^ (nsurlresponse *response, NSData *data, Nserror * error) {//This block of code is only available after the network request has ended    of the process. if (data! = nil) {//accepted to nsstring *str = [[NSString alloc]initwithdata:data encoding:nsutf8stringencoding        ];    NSLog (@ "%@", str); }else if (data = = Nil && Error! = nil)//does not receive data, but error is nil.

Indicates that an empty data is accepted. {NSLog (@ "accept null data"); }else{NSLog (@ "%@", error.localizeddescription); Request error.

} }];

The synchronization method is used in the most typical place when the user logs in: The user must log in to run other operations.

The Async method is when the network request is complete, then the page is updated, and so on.

The only difference between proxy and non-proxy is in the third part of the network request, which is nsurlconnecttion.

。。 The others are the same,. One is implemented by proxy. One is implemented by Nsurlconnection's two class methods. All the others are the same.


Cache Policy for Nsurlrequest:


2 Build Request Nsurlrequest    //Cache side abuse//    timeout length    nsurlrequest *request = [Nsurlrequest requestwithurl:url cachepolicy: Nsurlrequestuseprotocolcachepolicy timeoutinterval:2.0f];
The advantage of using a caching strategy is that you can enter the page when you enter the application, even if there is no network. Normal use of the default cache is possible.

These caches are cached in memory, and they need to be handled if they need to be preserved.


iOS elearning------3 Using non-proxy methods to implement asynchronous post 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.