Unable to use NSURLConnection to get contents of password/username protected webpage
- by bubster
I am trying to get the contents of a webpage that requires a password and user name to access. I am using a NSURLConnection object to get it however when I write the NSMutableData object that is returned to a file all I get is the login page. Normally when you try to load the password protected page when you are not logged in it redirects to the login page however I thought that if I provided valid credentials then this I would be able to view the password protected page. Also I do not know if it is relevant the website is using a microsoft mysql database on an IIS (internet information server).
Note: [protectionSpace authenticationMethod] returns NSURLAuthenticationMethodServerTrust
I am pretty unfamiliar with this so any ideas would be greatly appreciated.
Below is all of the relevant code:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse.
// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.
// receivedData is an instance variable declared elsewhere.
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
// release the connection, and the data object
//[connection release];
// receivedData is declared as a method instance elsewhere
//[receivedData release];
// inform the user
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data
// receivedData is declared as a method instance elsewhere
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
// release the connection, and the data object
//[connection release];
//[receivedData release];
//Write data to a file
[receivedData writeToFile:@"/Users/matsallen/Desktop/receivedData.html" atomically:YES];
}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace: (NSURLProtectionSpace *)protectionSpace
{
NSLog(@"The connection encountered a protection space. The authentication method is %@", [protectionSpace authenticationMethod]);
secureTrustReference = [protectionSpace serverTrust];
//SecTrustResultType *result;
//OSStatus status = SecTrustEvaluate(secureTrustReference, result);
//NSLog(@"Result of the trust evaluation is %@",status);
return YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSURLCredential *newCredential;
newCredential = [NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession];
newCredential = [NSURLCredential credentialForTrust:secureTrustReference];
// [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
// [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
receivedData = [[NSMutableData alloc] init];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.markallenonline.com/secure/maoCoaching.aspx"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [NSMutableData data];
NSLog(@"Connection succeeded!");
} else {
// Inform the user that the connection failed.
NSLog(@"Connection failed!");
}
}