1

I am trying to authenticate using the below code

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.

    }

My username password is correct, I think I am not making the proper call thats why I dont get the desired results and the web service is receiving the null parameters.

What can be the issue?

Any help appreciated.

3
  • Are you sure your authenticate.php page is expecting the username/password to be passed in the HTTP headers and not in the url query string? (or as form POST values?) Commented Jun 27, 2012 at 19:03
  • how to make call in the url query string and as form post values ? Can you help me with that Commented Jun 27, 2012 at 19:11
  • Accepted answer but no vote up? wah. Commented Mar 6, 2015 at 23:12

2 Answers 2

1

First step to debugging this is trying to view the desired response in a browser or using something like fiddler. Look at what the url is that you are using and look at what the actual POST values are. Where is the username/password being submitted? Usually authentication uses server side authentication but sometimes its in the url itself. How does it work in your browser?

Based on the description and your code I sort of think that maybe the fields you are including in

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

should maybe be added to the url you are posting to? Perhaps something like:

NSString *urlAsString =[NSString stringWithFormat:@"http://www.myurl.com/abc/authenticate.php?username/password"];
Sign up to request clarification or add additional context in comments.

3 Comments

Actually the username and password are not to be concatenated with the url. These are to be sent as parameters in post method.
Correct what, Nate Flink
Omer, just looking to correct my answer to be the most updated code here
0

If you are expecting an authentication challenge you should use the connection:didReceiveAuthenticationChallenge: NSURLConnection method

By adding that method and including a breakpoint you will at least be able to see if your challenge is occurring and it is in fact being handled properly. Below is an example of how i've used the authentication challenge method for a project

 - (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

1 Comment

Thanks, but im not expecting authentication challenge

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.