Nsurlconnection asynchronous request Method

Source: Internet
Author: User

Sendasynchronousrequest: queue: completionhandler: method added to the ios5.0 SDK nsurlconnection class

Sendasynchronousrequest can easily use nsurlrequest to receive callbacks and complete HTTP Communication.

1. Post Data

 1 - (void)httpAsynchronousRequest{ 2  3     NSURL *url = [NSURL URLWithString:@"http://url"]; 4      5     NSString *post=@"postData"; 6      7     NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 8  9     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];10     [request setHTTPMethod:@"POST"];11     [request setHTTPBody:postData];12     [request setTimeoutInterval:10.0];13     14     NSOperationQueue *queue = [[NSOperationQueue alloc]init];15     [NSURLConnection sendAsynchronousRequest:request16                                        queue:queue17                            completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){18                                if (error) {19                                    NSLog(@"Httperror:%@%d", error.localizedDescription,error.code);20                                }else{21                                    22                                    NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];23                                    24                                    NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];25                                    26                                    NSLog(@"HttpResponseCode:%d", responseCode);27                                    NSLog(@"HttpResponseBody %@",responseString);28                                }29                            }];30 31     32 }

 

2. Pull Images

 1         [NSURLConnection sendAsynchronousRequest: 2          [NSMutableURLRequest requestWithURL:[NSURL URLWithString:imageStr]] 3                                        queue:[NSOperationQueue mainQueue] 4                            completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 5         { 6                            NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode]; 7                            if (!error && responseCode == 200) { 8                                9                                UIImage *_img = [[UIImage alloc] initWithData:data];10                                11                            }else{12                                // placeHolder image  13                            }14         }];

 

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.