5

我正在尝试使用同步请求读取文本文件。它不起作用,但我也没有收到任何错误或警告。

谁能告诉我我做错了什么,好吗?

NSString *url = @"http://pappons.com/test.txt" ;

NSLog(@"getHTTPData: %@" , url ) ;
NSURLResponse* response = nil;

NSURLRequest* urlRequest =  [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

NSData* data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil] ;

NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog( @"data: %@" , myString ) ;

输出:

2012-06-15 11:33:42.209 FrederikTest[1365:707] getHTTPData: http://pappons.com/test.txt 
2012-06-15 11:33:42.306 FrederikTest[1365:707] data: 
4

1 回答 1

8

传入NSError检查是否发生错误

NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
于 2012-06-15T09:54:50.487 回答