Asynchronous request HTTP

Source: Internet
Author: User

代码:

@interface HttpProcessor : NSObject <NSURLConnectionDataDelegate>{    NSMutableData *buffer;}@property BOOL finished;@property (strong, nonatomic) NSString *html;@end@implementation HttpProcessor@synthesize finished;@synthesize html;-(id)init{    if (self) {        finished = NO;    }        return self;}// 开始接收响应-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{    finished = NO;    buffer = [[NSMutableData alloc] init];}// 接收ing , 可能多次调用-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{    [buffer appendData:data];}// 结束响应-(void)connectionDidFinishLoading:(NSURLConnection *)connection{    finished = YES;    html = [[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding];    //NSLog(@"%@", html);    NSLog(@"OK");}@endvoid request(NSString *urlString){    NSLog(@"BEGIN");    // make request object    NSURL *url = [[NSURL alloc]initWithString:urlString];    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];    [request setHTTPMethod:@"GET"];    [request setTimeoutInterval:10];        // send request    HttpProcessor *processor = [[HttpProcessor alloc]init];    [NSURLConnection connectionWithRequest:request delegate:processor];}

 代码块:

void request(NSString *urlString){    NSLog(@"BEGIN");    // make request object    NSURL *url = [[NSURL alloc]initWithString:urlString];    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];    [request setHTTPMethod:@"GET"];    [request setTimeoutInterval:10];    // send request    [NSURLConnection     sendAsynchronousRequest:request     queue:[NSOperationQueue mainQueue]     completionHandler:        ^(NSURLResponse *response, NSData *result, NSError *error)        {            //只会进入一次,方法内部已经实现了Buffer作用            NSString *html = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];            NSLog(@"html = %@", html);        }          ];}

参考:

http://www.tuicool.com/articles/2Yru6f

 

异步请求HTTP

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.