IOS-网络操作

演示请求百度首页
代码
//1创建请求的地址
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
    
 //2创建请求对象,告诉服务器一些信息
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
 //3建立网络连接,向服务器发送请求,并接收服务器返回的响应
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
           //网络请求完成后执行的方法
           //NSURLResponse *response, 服务器返回的响应对象
           //NSData *data,            服务器返回的二进制数据
           //NSError *connectionError 如果错误,返回的错误信息
            
           //  [data writeToFile:@"/Users/你的用户名/Desktop/123.html" atomically:YES];
            
           //把二进制数据转换成字符串
            NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"%@",str);
        }];

原文地址:https://www.cnblogs.com/DreamLinku/p/5654448.html