Re: Help With NSURLConnection Authentication
Re: Help With NSURLConnection Authentication
- Subject: Re: Help With NSURLConnection Authentication
- From: Topaness <email@hidden>
- Date: Wed, 6 Oct 2004 15:04:44 -0500
Rudi,
Thank you for your quick responses. Once again you'll have to forgive
me, I'm still quite new to Cocoa/Objective-C.
I have receivedData defined in the header file, I should have pasted
that code as well (I've included it this time at the bottom of the
message). I don't have a defined runLoop, the original code I included
was everything from the NSConTest.m file in my project. Do I actually
need to manually define a runLoop for this? I would think since it
works with pages not requiring authentication that this shouldn't be an
issue.
What happens when I run the program with break points on each of the
delegate functions is that it starts on awakeFromNib and runs through
to the "receivedData=[[NSMutableData data] retain];" line. Normally,
when I'm operating on a URL that does not require authentication,
continuing or stepping into this will take me to the breakpoint of the
next function, which is didReceiveResponse, and then goes through each
of the other functions as appropriate. When operating on this URL
however which requires authentication, when I click continue or step
into on the receivedData line, it then jumps to the closing } for
awakeFromNib, however the awakeFromNib thread does not close. The
program doesn't become unresponsive, I can enter text into a edit box I
have.
I checked tcpdump as you suggested. Safari and my app are definately
not doing the same things. My app appears to stop sending and
requesting info much earlier. Though I'm not yet sure where. I'm
installing ethereal at the moment and will try to better figure out
where its all breaking.
I would suspect at this point though that its something improper I've
done with my code. I realize it isn't normal to have this much stuff in
awakeFromNib, however I was trying to create the simplest app possible,
so I could get the bare functions I wanted to use working without
anything else in the way. From what you mentioned it sounds as though I
may need to add more and learn more Cocoa/Objective-C before I can use
NSURLConnection. I'm used to figuring things out from looking at code
and just jumping in, but i've yet to find any complete demo apps that
use NSURLConnection for authentication.
Again, thank you for your response.
Here is my code for NSConTest.h. I tried adding in definitions for all
of the delegate functions like didReceiveAuthenticationChallenge: but
that doesn't seem to have fixed it.
/* NSConTest */
#import <Cocoa/Cocoa.h>
@interface NSConTest : NSObject
{
IBOutlet id TextOut;
IBOutlet NSMutableData *receivedData;
IBOutlet NSURLDownload *theDownload;
}
@end
With extra stuff added:
/* NSConTest */
#import <Cocoa/Cocoa.h>
@interface NSConTest : NSObject
{
IBOutlet id TextOut;
IBOutlet NSMutableData *receivedData;
IBOutlet NSURLDownload *theDownload;
}
- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge
*)challenge;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData
*)data;
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error;
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
@end
On Oct 6, 2004, at 10:57 AM, Rudi Sherry wrote:
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