我正试图获取一个网页的内容,该网页需要密码和用户名称才能查阅。 然而,当我写了被送回档案的NSMutableData物体时,我就使用了NSURLConnection的标语。 通常当你试图装上密码保护的网页时,如果你没有把密码贴上黑页,但我认为,如果我提供了有效的全权证书,那么我就能够看到密码保护页面。 另外,我不知道该网站是否相关,是否正在使用一个微波中继系统(内联网信息服务器)的缩微薄我的仪数据库。
注:[保护空间认证] 方法 方法 信托基金
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!");
}
}