NSURL连接和浏览器访问之间的区别



我正在开发一个非常简单的应用程序,它访问一个书面的URL。所以我想知道通过 nsurlconnection 访问和仅使用浏览器访问有什么区别。导致某些站点响应,但是当我使用nsurl连接时,它们不会发送数据。

- (void)getWikiData:(NSString *)keyword{
NSString* tmpURL = @"http://wikipedia.simpleapi.net/api?keyword=";
NSString* encodedString;
CFStringRef strRef = CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)keyword, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]~", kCFStringEncodingUTF8);
encodedString = [NSString stringWithString:(NSString *)strRef];
CFRelease(strRef);
[tmpURL stringByAppendingString:encodedString];
[tmpURL stringByAppendingString:@"&output=html"];
NSURL *url = [NSURL URLWithString:tmpURL];
NSString *userAgent = @"Custom User Agent";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response{
NSLog(@"Receive Response");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSLog(@"Receive Data");
}

提前谢谢。

区别在于生成的应用程序的用户代理字符串。MobileSafari将自己报告为"Safari,iOS像Mac OS X",但是,普通NSURLConnection发送CFNetwork描述,这对于大多数站点进行"浏览器"(而不是"客户端")检测不是很有用,这就是为什么他们可能会拒绝将数据发送到无法识别的用户代理。

最新更新