同步和异步都是表态方法:
#pragma mark 异步请求
- (void)post2 {
NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/备课.txt"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 这个操作队列是用来执行Block的
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:
// 请求结束后会回调这个Block
^(NSURLResponse *response, NSData *data, NSError *error) {
NSString *str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"%@", str);
NSLog(@"%@", [response MIMEType]);
}];
}
#pragma mark 同步请求-获取文件的MIMEType
- (void)getType {
NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/备课.txt"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 发送一个同步请求
NSURLResponse *response = nil;
// 发送一个同步请求
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString *type = [response MIMEType];
NSString *str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"%@", str);
NSLog(@"%@", type);
}