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

Source: Internet
Author: User

#pragma mark-This is a private method, try not to use the property directly in the method, because generally the property is associated with the interface, we can use the attribute #pragma the Mark Post login method by the way of the parameter-(void) Loginwithpostwithname: (NSString *) userName pwd: (NSString *) pwd{//1 determine address nsurl nsstring *urlstring = [NSString StringW    ithformat:@ "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 if we use the POST request, we must declare the request way.    [Request sethttpmethod:@ "POST"]; 2) The data body of the POST request, the data body in the POST request, if there is Chinese, do not need to convert.    Because the Atausingencoding method has already 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]; 3 Establish and start the connection nsrulconnection//nsurlconnection *conn = [nsurlconnection connectionwithrequest:request delegate:self];//[Conn start]; Start the connection, which is where the network request has occurred.        This is an asynchronous connection request, and when the request is sent out, it is referred to the agent for processing.    3 No proxy synchronization request, nsurlresponse *response = nil;    Nserror *error = nil; The second, three parameters are pointers to the pointer, all to use the accessor, this method is a synchronous method.    The synchronization operation is not completed and the following code will not execute.    NSData *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error]; Subsequent processing of the data that is accessed synchronously if (data = nil) {//accepts data, indicates work nsstring *str = [[NSString alloc]initwithdata:data Encoding:ns        Utf8stringencoding];    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. }////server notification ready, ready for transit data//Self.serverdata = [Nsmutabledata data];}

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.