NSURLConnection的非同步請求方式

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   使用   

iOS5.0 SDK NSURLConnection類新增的sendAsynchronousRequest:queue:completionHandler:方法

sendAsynchronousRequest可以很容易地使用NSURLRequest接收回調,完成http通訊。

1. post資料

 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. 拉取圖片

 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         }];

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.