Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

lawicko

macrumors member
Original poster
Jul 17, 2008
44
0
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?
 

lawicko

macrumors member
Original poster
Jul 17, 2008
44
0
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.
 

Taum

macrumors member
Jul 28, 2008
56
0
this may be caused by the servers that don't send authentication challenges, but it wouldn't work on Safari then

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.
 

lawicko

macrumors member
Original poster
Jul 17, 2008
44
0
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).
 

lawicko

macrumors member
Original poster
Jul 17, 2008
44
0
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.
 

tyr2

macrumors 6502a
May 6, 2006
826
217
Leeds, UK
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.