English 中文(简体)
无法利用NSURLConnection获取密码/用户名受保护网页的内容
原标题:Unable to use NSURLConnection to get contents of password/username protected webpage

我正试图获取一个网页的内容,该网页需要密码和用户名称才能查阅。 然而,当我写了被送回档案的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!");
}
}
最佳回答

Try ASIHTTPRequest, This have Basic, Digest and NTLM accreditation support.

http://allseeing-i.com/ASIHTTPRequest/

Good luck!

问题回答

你们需要执行反馈:

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

并从那里穿过 cr。

采用这一守则,而不是你拥有的:

- (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");    
iii
else {
    NSLog(@"previous authentication failure");
iii

iii

我使用这一法典,我可以登录在受保护的网页上。





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Remotely authenticating client Windows user on demand

Suppose I am writing a server for a particular network protocol. If I know that the client is running on a Windows machine, is it possible for my server to authenticate the Windows user that owns the ...

Role/Permission based forms authorizing/authentication?

While looking into forms authorizing/authentication, I found that it is possible to do role based authorizing by adding an array of roles to a FormsAuthenticationTicket. That way I can write User....

热门标签