iOS Http请求异步请求

jopen 6年前

- (void)viewDidLoad  {      [super viewDidLoad];   // Do any additional setup after loading the view.            _data = [[NSMutableData alloc] init];            // 组合一个搜索字符串      NSString *urlStr = [NSString stringWithFormat:@"http://www.baidu.com/s?wd=%@", @"php"];      NSURL *url = [NSURL URLWithString:urlStr];            NSURLRequest *request = [NSURLRequest requestWithURL:url];            //发起请求,定义代理      [NSURLConnection connectionWithRequest:request delegate:self];        }    // 分批返回数据  - (void)connection:(NSURLConnection *) connection didReceiveData:(NSData *)data {      [_data appendData:data];      NSLog(@"%@", _data);  }    // 数据完全返回完毕  - (void)connectionDidFinishLoading:(NSURLConnection *)connection {      NSString *dataString =  [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding];      NSLog(@"%@", dataString);  }