http://mycompany.com/page1...http://mycompany.com/page2から JSON をフェッチしたいと思います... Web サーバー側では、初期ログインhttp://mycompany.comが必要です。 /login、その後、ユーザーの Cookie が保持されます。毎回ログインを要求することなく、NSURLConnection でこの動作を取得するにはどうすればよいですか? NSURLCredential Storage を使用した動作しないコードを次に示します。ロギング時に Web サービスから Cookie を取得し、その後のリクエストと一緒に送信する必要がありますか? 私はしばらくこれに苦労しました。答えを明確にしてください。
- (IBAction)getJSON:(id)sender
{
NSURLCredential *credential = [NSURLCredential credentialWithUser:@"user"
password:@"pass"
persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
initWithHost:@"myCompany.com"
port:0
protocol:@"http"
realm:nil
authenticationMethod:nil];
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential
forProtectionSpace:protectionSpace];
//////////GET JSON//////////////
NSError *error;
NSURL *url = [NSURL URLWithString:@"http://mycompany.com.jsonpage1"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
//I am NOT getting JSON in this delegate
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@",responseString);
}