NSURLErrorDomain Code=401

1,800 views
Skip to first unread message

webguyatwork

unread,
May 18, 2010, 12:15:24 AM5/18/10
to Three20
I've been testing successfully all day, pulling down data from an
external site with basic HTTP authentication. Now all of a sudden,
I'm getting this error when I try to load the data:

Did fail load with error: Error Domain=NSURLErrorDomain Code=401
"Operation could not be completed. (NSURLErrorDomain error 401.)"

I've deleted and reinstalled the app, and it's giving the same error
on the iPhone as it is in the simulator. Is this a caching issue?

--
You received this message because you are subscribed to the Google Groups "Three20" group.
To post to this group, send email to thr...@googlegroups.com.
To unsubscribe from this group, send email to three20+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/three20?hl=en.

webguyatwork

unread,
May 18, 2010, 8:25:50 AM5/18/10
to Three20
I think I see what happened, but I don't know how to get around it. I
was apparently logged off the remote site at some point, and my
authentication response function didn't pick up and reauthorize me
properly. I was able to temporarily fix the situation by passing the
remote site URL into a TTWebController and authenticating that way.
Here are the function calls from my data model class:

- (void)load:(TTURLRequestCachePolicy)cachePolicy more:(BOOL)more {
if (!self.isLoading) {
NSString* url = @"http://<remotesite>/data.json";
TTURLRequest* request = [TTURLRequest requestWithURL:url
delegate:self];
id<TTURLResponse> response = [[TTURLDataResponse alloc] init];
request.cachePolicy = TTURLRequestCachePolicyNone;
request.response = response;
[request send];
}

}

- (void)request:(TTURLRequest *)request
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge
*)challenge {
NSLog(@"Received authentication challenge");
if ([challenge previousFailureCount] == 0) {
NSURLCredential *newCredential = [NSURLCredential
credentialWithUser:_username password:_password
persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential
forAuthenticationChallenge:challenge];
}
else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}

}

The NSLog output from my didReceiveAuthenticationChallenge function
never got called while I was experiencing the 401 error. I thought
that it was supposed to be called when the site returns a 401 error.
In fact, I've personally witnessed the log entry from this function in
this application in the recent past.

Any suggestions on how I can properly pass authentication to the
remote site? I'm guessing that the user/password info need to be put
into the headers of the TTURLRequest?

Anton Chirkunov

unread,
May 18, 2010, 10:32:42 AM5/18/10
to Three20
Does your remote site return a "WWW-Authenticate" header?
A simple 401 with not authentication realm will trigger
request:didFailLoadWithError:
Reply all
Reply to author
Forward
0 new messages