Help With NSURLConnection Authentication
Help With NSURLConnection Authentication
- Subject: Help With NSURLConnection Authentication
- From: Topaness <email@hidden>
- Date: Wed, 6 Oct 2004 09:38:46 -0500
I'm new to Cocoa development and am having trouble with the authentication features of NSURLConncetion. Admittedly I've not done that much with Objective-C or Xcode yet, but i've read through a couple tutorials including the "Developing Cocoa Objective-C Applications: A Tutorial" and wrote some small apps. Most of my experience lies in Perl and Ruby for Windows and Linux development but i'm trying to make the move to OSX, so please forgive me for any simple mistakes.
The application connects to a site (http://rpc.bloglines.com/listsubs) and uses basic http authentication to login. The site then sends its response in the form of an XML file. This is what is supposed to happen anyway. The problem is that while all of this code works fine with other sites that don't use authentication, it doesn't work here. I can access the URL and login just fine from Safari, so its nothing with my connection. What I find odd is that according to the log i'm not even receiving a response. The log says the session started, and it still responds, but after stepping through the code in debug mode, it looks like it hits the "receivedData=[[NSMutableData data] retain];" line and then finishes. I've been trying to figure this out for about a week now, and have done more searches than I can count. It's becoming rather frustrating and I'm close to giving up on Objective-C.
I have included the code below. Most(all) of the code is example code from the Apple docs (hopefully this isn't a violation, i looked through the FAQ and guidelines and didn't see anything about this, and the code did not contain any copyright info). I cannot find any example code elsewhere or enough documentation to go beyond this yet. If anyone can offer any help or knows of any place or even books that have more comprehensive info on the NSURLConnection and using authentication with it it would be greatly appreciated. Thanks.
#import "NSConTest.h"
@implementation NSConTest
- (void)awakeFromNib
{
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://rpc.bloglines.com/listsubs/"]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
receivedData=[[NSMutableData data] retain];
} else {
NSLog(@"Connection could not be made");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// release the connection, and the data object
[connection release];
[receivedData release];
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"Got response");
[receivedData setLength:0];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
NSString *urlData;
if (nil != receivedData) {
urlData = [[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding] autorelease];
}
[TextOut replaceCharactersInRange: NSMakeRange(0,0) withString:urlData];
// release the connection, and the data object
[connection release];
[receivedData release];
}
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLog(@"Auth Request");
if ([challenge previousFailureCount] == 0) {
NSURLCredential *newCredential;
newCredential=[NSURLCredential credentialWithUser:(NSString *)@"email@hidden"
password:(NSString *)@"yeyinde99"
persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:newCredential
forAuthenticationChallenge:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
// inform the user that the user name and password
// in the preferences are incorrect
//[self showPreferencesCredentialsAreIncorrectPanel:self];
}
}
@end
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden