标签:style blog http io color ar os sp strong
例子:
- (void)sendSynNetwork{ NSLog(@"Wo are here..."); NSString *urlString = @"http://www.baudu.com"; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f]; //同步没有block //这样解决比较方便 NSURLResponse *response = nil; NSError *error = nil; NSLog(@"开始同步请求"); NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error]; if ([data length] > 0 && error == nil){ NSLog(@"%lu bytes of data was returned.", (unsigned long)[data length]); } else if ([data length] == 0 && error == nil){ NSLog(@"No data was returned."); } else if (error != nil){ NSLog(@"Error happened = %@", error); } NSLog(@"We are done."); }
Wo are here...
开始同步请求
8875 bytes of data was returned.
We are done.
//简单的同步请求 - (void)sendSynNetwork{ NSLog(@"Wo are here..."); NSString *urlString = @"http://www.baudu.com"; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f]; dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(dispatchQueue, ^{ //同步没有block //这样解决比较方便 NSURLResponse *response = nil; NSError *error = nil; NSLog(@"开始同步请求"); NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error]; if ([data length] > 0 && error == nil){ NSLog(@"%lu bytes of data was returned.", (unsigned long)[data length]); } else if ([data length] == 0 && error == nil){ NSLog(@"No data was returned."); } else if (error != nil){ NSLog(@"Error happened = %@", error); } }); NSLog(@"We are done."); }
Wo are here...
We are done.
开始同步请求
9138 bytes of data was returned.
标签:style blog http io color ar os sp strong
原文地址:http://www.cnblogs.com/safiri/p/4089096.html