0

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);

}
4

1 に答える 1

0

Cookie の読み取り:

iPhone での HTTP Cookie の管理を参照してください。

クッキーの設定:

... Cookie プロパティでディクショナリを設定すると、次のようになります。

NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:[NSDictionary dictionaryWithObjects:object forKeys:keys]];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];

ただし、セッション Cookie はサーバー上で期限切れになる可能性があることに注意してください

于 2012-08-20T12:37:04.830 に答える