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:57:24 -0700
Sorry, my last post was not a quality post. I didn't read your post
carefully -- you said this works with other sites, so obviously you
have a runLoop and receivedData is working (although I would still
initialize receivedData first just in case didReceiveData: is being
called within initWithRequest:delegate: ).
When you say it hits the NSMutableData line "and then finishes", do you
mean the URLConnection says that it's done, that is, it calls
connectionDidFinishLoading: ? Or do you mean the application becomes
unresponsive?
You might try tcpdump to find out whether you're getting a response
from the server but the Cocoa code is not giving it to you. I know of
at least one case where NSURLConnection times out even though the
server is responding. This happens on a synchronous HEAD request to
particular kinds of servers (I think it's Apache servers but it might
be Netscape servers). NSURLConnection sends a "Connection: Keep-Alive"
(ignoring my specifically-added header of "Connection: close"), and
tcpdump shows that the server immediately responds with the headers and
a Content-Length and a Keep-Alive with timeout 15 seconds... but
NSURLConnection sits there for 15 seconds then times out, never having
given me a response. My theory is that it's looking at the
Content-Length and assumes content will follow -- but for a response to
a HEAD request it won't. Your request is undoubtedly a GET, but you
never know.
One final stab in the dark: I've seen some issues where a method is in
the implementation but not the interface -- compiles and links ok --
but outside classes don't find it -- do you have
didReceiveAuthenticationChallenge: in your interface?
On Oct 6, 2004, at 8:16 AM, Rudi Sherry wrote:
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
_______________________________________________
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