iOS 发送异步请求
作者:
iOS_July | 来源:发表于
2019-03-28 12:18 被阅读0次//发送async异步请求
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSURLRequest *requset = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:requset queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//这里使用主队列 [NSOperationQueue mainQueue]
//response 服务器返回的相应头
//data 服务器返回的响应体
//connectionError 链接错误
//判断请求是否有错误
if (!connectionError) {
//把二进制数据转换成NSString
NSString *html = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"html的字符串=%@",html);
}else{
NSLog(@"连接错误");
}
}];
本文标题:iOS 发送异步请求
本文链接:https://www.haomeiwen.com/subject/okcqbqtx.html
网友评论