2011-11-16 16 views
1

我想獲取需要密碼和用戶名訪問的網頁的內容。我使用NSURLConnection對象來獲取它,但是當我編寫返回到文件的NSMutableData對象時,我所獲得的就是登錄頁面。通常,當您在未登錄時嘗試加載受密碼保護的頁面時,它會重定向到登錄頁面,但我認爲如果我提供了有效的憑據,那麼我可以查看受密碼保護的頁面。此外,我不知道它是否與IIS(互聯網信息服務器)上使用MicrosoftMySQL數據庫的網站相關。無法使用NSURLConnection獲取受密碼/用戶名保護的網頁的內容

注:[protectionSpace使用authenticationMethod]返回NSURLAuthenticationMethodServerTrust

我這個所以任何想法,將不勝感激相當陌生。

下面是所有相關代碼:

- (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!"); 
} 
} 

回答

2

你需要實現回調:

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

,並從裏邊有通過vim的信任狀。

2

嘗試使用,而不是你有什麼驗證碼:

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
if ([challenge previousFailureCount] == 0) { 
    NSLog(@"received authentication challenge"); 
    NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"???????" 
                   password:@"???????" 
                  persistence:NSURLCredentialPersistenceForSession]; 
    NSLog(@"credential created"); 
    [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; 
    NSLog(@"responded to authentication challenge");  
} 
else { 
    NSLog(@"previous authentication failure"); 
} 

}

我使用此代碼,並在我的情況,我可以登錄到受保護的頁面。