您可以使用stringWithContentsOfURL:encoding:error:
, 记录在这里:
http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
例如:
[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://localhost/v/p.xml"] encoding:NSUTF8StringEncoding error:nil];
更新:
正如 Jim 所说,NSURLConnection
它是非阻塞的,如果您想保护您的文件不被公众访问,它允许您提交凭证。如果你想使用它,它看起来像这样:
NSString *authenticationURL = @"http://foo.bar";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:authenticationURL]];
NSString *escUsername = [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *escPassword = [password stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:[[NSString stringWithFormat:@"username=%@&password=%@", escUsername, escPassword] dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
#pragma mark Url connection delegate methods
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSString *responseText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", responseText);
}