1

iOSプログラミングを始めたばかりで、これを理解できていません..ログインしているWebサイトに接続しようとしています..POSTを使用しNSURLConnectionて、ログイン資格情報を送信しています..

資格情報は正しく、connectionDidFinishLoading:呼び出されますが、サイトに完全にログインしていません..ログインは部分的に完了しています..応答を見たときにこれを特定しました..私の理解によると、「connectionDidFinishLoading」は接続が完了しました..私は間違いなくここに何かが欠けています..私のコードは以下です..アドバイスしてください..

- (IBAction)connect:(id)sender {
    // Login to the website
    urlRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.mywebsite.com/login"]
                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                   timeoutInterval:60.0];

    NSString *post = @"username=myname&password=mypassword";
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [urlRequest setHTTPBody:postData];

    // Start the connection request
    urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
}

ここで、応答をログに記録すると、不完全です.最終的な成功ページの間にあるページに着陸しています..したがって、認証は部分的に完了しています..遅延を含める必要がありますか? 助けてください..

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"Connection success.");
    NSURLResponse *responseTest;
    NSError *error;

    NSData *responseDataTest = [NSURLConnection sendSynchronousRequest:urlRequest       returningResponse:&responseTest error:&error];

    NSString *responsestring = [[NSString alloc] initWithData:responseDataTest encoding:NSASCIIStringEncoding];
    NSLog(@"Recieved response.. %@",responsestring);
}

前もって感謝します..

4

1 に答える 1

0
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

代わりにこの方法を使用してみてください。ドキュメントによると、「接続が要求に対する URL 応答を構築するのに十分なデータを受信したときに送信されます。(必須)」です。

于 2012-08-14T00:29:34.963 に答える