Re: Help With NSURLConnection Authentication
Re: Help With NSURLConnection Authentication
- Subject: Re: Help With NSURLConnection Authentication
- From: Rudi Sherry <email@hidden>
- Date: Wed, 06 Oct 2004 08:16:04 -0700
First, I think you need to initialized receivedData *before* you call
initWithRequest (I assume it's an instance variable of NSConTest),
since initWithRequest starts the connection and you may start receving
data before you get to the next line. If receivedData is uninitialized
then didReceiveData: could be getting an exception with results I'm not
sure (maybe what you're seeing).
I assume you put breakpoints at all the delegate functions to see if
anything is triggering there and nothing is?
Also, I assume you're in an application that has a RunLoop going
because NSURLConnection needs a RunLoop in order to function -- that
is, if you're not within some kind of application run loop
NSURLConnection gets no time and the delegates never hit. Any sample
application has that, so I imagine you're OK there.
That's all I can think of,
Rudi
On Oct 6, 2004, at 7:38 AM, Topaness wrote:
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
_______________________________________________
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