NSURLConnection sendSynchronousRequest - 缺少数据

时间:2012-06-15 09:45:29

标签: objective-c ios nsurlconnection

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

有人可以告诉我我做错了吗?

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: 

1 个答案:

答案 0 :(得分:8)

传递NSError以检查是否发生了错误

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