How to receive authentication challenge?

Discussion in 'iOS Programming' started by lawicko, Oct 7, 2008.

  1. lawicko macrumors member

    Joined:
    Jul 17, 2008
    #1
    I need to connect to some server using https protocol. I will have to authenticate in order to get data, here's how I try to do this:
    Code:
    NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"some server address"]];
    NSURLConnection *manualConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
    
    then in the delegate object I implement following methods:
    Code:
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
    	NSLog(@"Logging response");
    	NSLog(@"Status code = %i",[((NSHTTPURLResponse*)response) statusCode]);
    }
    Code:
    -(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
    {
    	NSLog(@"Connection did receive authentification challenge");
    }
    The problem is that I never get authentication challenges, even though when I try to enter the same url via native Safari browser everything seems to work just fine. Moreover, received response shows error code 403, so forbidden. What am I doing wrong?
     
  2. lawicko thread starter macrumors member

    Joined:
    Jul 17, 2008
    #2
    Ok, so now I found out that my code properly gets authentication challenges from some servers, but does not get if from the others :confused: I was thinking that this may be caused by the servers that don't send authentication challenges, but it wouldn't work on Safari then :rolleyes:
    So if anyone has some hints on how to handle authentication it would be greatly appreciated.
     
  3. Taum macrumors member

    Joined:
    Jul 28, 2008
    #3
    Do you have control over these servers ? It's quite possible that some servers would have different behaviors depending on e.g. the User-Agent strings that is supposed to identify the client.
     
  4. lawicko thread starter macrumors member

    Joined:
    Jul 17, 2008
    #4
    Unfortunately I don't have control over the servers. Correct me if I'm wrong, but if user-agent string was the case then I think that I would not get authentication challenge on native Safari browser. For testing I use simulator and iPod touch, the behavior is the same on both.
    - I get authentication challenge on all servers on native safari on both simulator and device
    - I get authentication challenge on some servers in my app
    - I don't get authentication challenge on some servers in my app

    The code I'm using is similar to what can be found in Loading URL System document, except I allow all redirections (disallowing redirections doesn't make any change).
     
  5. lawicko thread starter macrumors member

    Joined:
    Jul 17, 2008
    #5
    I solved this! The problem was that NSURLConnection does not send any information in http header fields, so when server is configured to accept conections only from particular clients you may simply be given 403 Forbidden because you don't have your device type specified in http header.
     
  6. tyr2 macrumors 6502a

    tyr2

    Joined:
    May 6, 2006
    Location:
    Leeds, UK
    #6
    Just an aside. When developing in the Simulator I see this for a user agent on my web servers

    {appname}/1.0 CFNetwork/339.5 Darwin/9.5.0 (i386) (MacPro1,1)

    and when running on the phone I see this

    (appname)/1.0 CFNetwork/339.3 Darwin/9.4.1

    So the information is there, it just doesn't identifty as Safari, which is fair enough because it's not Safari making the request.
     
  7. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    So you added a header to your NSURLRequest? What header did you add?
     
  8. lawicko thread starter macrumors member

    Joined:
    Jul 17, 2008

Share This Page