する必要がある:
1) フォーム認証サービスに対して認証する
2) クッキーを保存する
3) Cookie を使用して Web サービスを呼び出す
そして、私はこれを機能させるために非常に苦労しています。これで運が良かった人はいますか?非常に一般的な操作のようです。
これが私のコードです:
認証 URL にアクセスして Cookie を取得します。
- (IBAction)buttonGetCookieTap:(id)sender {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.cookieURL];
[request setHTTPShouldHandleCookies:YES];
[request setHTTPMethod:@"POST"];
self.urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- ( void )connection: (NSURLConnection *)connection didReceiveResponse: (NSURLResponse *)response
{
NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
NSDictionary *fields = [HTTPResponse allHeaderFields];
self.cookie = [fields valueForKey:@"Set-Cookie"];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSURLCredential *credential = [NSURLCredential credentialWithUser:@"blah"
password:@"blah"
persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}
次に、Cookie を使用して認証サービスを呼び出します。
- (IBAction)buttonCallServiceTap:(id)sender {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.dataURL];
[request addValue:self.cookie forHTTPHeaderField:@"Cookie"];
self.urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
編集:申し訳ありませんが、私が抱えている問題は、セッション ID だけでベアボーン Cookie を取得していることですが、サーバー側では Cookie は問題ないように見えます。私のクッキー取得コードに問題がないことを誰かが確認できますか? 私はこれの多くのバリエーションを試しましたが、同じ問題があります。ありがとう!