我正在开发一个非常简单的应用程序,它访问一个书面的 url。所以我想知道通过nsurlconnection访问和仅使用浏览器访问有什么区别。导致某些站点响应,但当我使用 nsurlconnection 时它们不发送数据。
- (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");
}
提前致谢。