PDA

View Full Version : NSURLConnection with HTTPS




TurboLag
Oct 12, 2009, 11:36 AM
I am trying to scrape a webpage which requires authentication. I have implemented the required delegate methods for NSURLConnection, but didReceiveAuthenticationChallenge is not hit. I am trying to scrape: https://www.rogers.com/web/myrogers/internetUsage?actionTab=DayToDay

Should my initial request be to this page, or should I first reach the logon page? And, is there some boiler template for sending the username and password once the authentication challenge is hit?

Thanks.



Sayer
Oct 12, 2009, 07:06 PM
What you should do is construct a request that simulates the login form if a user were to click on SUBMIT. Look at the source and find the action and the names of the form elements (sometimes there are hidden values). Make up a URL that points to the action with the proper form elements as parameters and start there.

Then you can use something like this:

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{

if ([MTUserName length] > 0) { // MTUserName is whatever login credentials you have
NSURLCredential *newCredential;
newCredential=[NSURLCredential credentialWithUser:MTUserName
password:MTPassWord
persistence:NSURLCredentialPersistenceNone];
[[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];
}
}

TurboLag
Oct 12, 2009, 08:28 PM
Okay, so it seems I should be pointing to https://www.rogers.com/web/link/signin. Looking at the source of that page, the names of the forms are 'USER' and 'password'.

However, I still dont get a call to didGetAuthenticationChallenge. What is the criterion for this delegate method to be called?