我正在尝试获取需要密码和用户名才能访问的网页内容。我正在使用NSURLConnection对象来获取它但是当我编写返回到文件的NSMutableData对象时,我得到的只是登录页面。通常,当您在未登录时尝试加载受密码保护的页面时,它会重定向到登录页面,但我认为如果我提供了有效的凭据,那么我将能够查看受密码保护的页面。此外,我不知道网站是否在IIS(互联网信息服务器)上使用微软mysql数据库是否相关。
注意:[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!");
}
}
答案 0 :(得分:2)
您需要实现回调:
-(void) connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
并从里面传递信誉。
答案 1 :(得分: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");
}
}
我使用此代码,在我的情况下,我可以登录受保护的页面。
答案 2 :(得分:0)
尝试ASIHTTPRequest,它具有Basic,Digest和NTLM身份验证支持。
祝你好运!