1

以下のコードを使用して認証しようとしています

NSString *urlAsString =[NSString stringWithFormat:@"http://www.myurl.com/abc/authenticate.php"];

    NSURL *url = [NSURL URLWithString:urlAsString];

    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; 

    [urlRequest setTimeoutInterval:30.0f];

    [urlRequest setHTTPMethod:@"POST"];

    [urlRequest addValue:@"test" forHTTPHeaderField:@"m_username" ];

    [urlRequest addValue:@"123" forHTTPHeaderField:@"m_password" ];

    [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response,NSData *data, NSError *error) {

        if ([data length] >0 && error == nil){

            html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

            NSLog(@"HTML = %@", html);

            receivedData = [NSMutableData data];

        }
        else if ([data length] == 0 && error == nil){

            NSLog(@"Nothing was downloaded."); 

        }

        else if (error != nil){

            NSLog(@"Error happened = %@", error);
        } 
    }];

    // Start loading data
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
    if (theConnection) 
    {
        // Create the NSMutableData to hold the received data
        receivedData = [NSMutableData data];

    } 
    else {
        // Inform the user the connection failed.

    }

ユーザー名のパスワードが正しいのですが、適切な呼び出しを行っていないため、目的の結果が得られず、Webサービスがnullパラメーターを受け取っています。

何が問題になる可能性がありますか?

助けていただければ幸いです。

4

2 に答える 2

1

これをデバッグするための最初のステップは、ブラウザーで目的の応答を表示するか、フィドラーなどを使用することです。使用しているURLを確認し、実際のP​​OST値を確認してください。提出されているユーザー名/パスワードはどこにありますか?通常、認証はサーバー側の認証を使用しますが、URL自体に含まれる場合もあります。ブラウザではどのように機能しますか?

説明とあなたのコードに基づいて、私はあなたが含めているフィールドが多分あると思います

 [urlRequest addValue:@"test" forHTTPHeaderField:@"m_username" ];
 [urlRequest addValue:@"123" forHTTPHeaderField:@"m_password" ];

投稿先のURLに追加する必要がありますか?おそらく次のようなものです:

NSString *urlAsString =[NSString stringWithFormat:@"http://www.myurl.com/abc/authenticate.php?username/password"];
于 2012-06-27T21:12:22.817 に答える
0

認証チャレンジを期待している場合は、connection:didReceiveAuthenticationChallenge:NSURLConnectionメソッドを使用する必要があります

そのメソッドを追加し、ブレークポイントを含めることで、少なくともチャレンジが発生しているかどうか、実際に適切に処理されているかどうかを確認できます。以下は、プロジェクトで認証チャレンジ方式を使用した例です。

 - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
 {
     NSURLCredential *credential = [NSURLCredential credentialWithUser:@"username" password:@"passwordvalue" persistence:NSURLCredentialPersistenceForSession];
     [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
 }

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/AuthenticationChallenges.html#//apple_ref/doc/uid/TP40009507-SW1

于 2012-06-27T19:15:13.637 に答える