1

我正在尝试使用 UIWebview 进行 NTLM 身份验证。我研究过,我是这样写的。但是在我的 NSLog 中,我只看到"got auth challange"但我没有看到"received response via nsurlconnection"。请帮我。我坚持这个超过5天。我正在使用ios 6.1进行测试。我很感激任何帮助:)

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request   navigationType:(UIWebViewNavigationType)navigationType{


if([request.URL.host isEqualToString:@"42.61.46.2"])
{
    NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request   delegate:self];
    [connection start];

    NSLog(@"%@",request);
    return NO;
}


self.lbl_error.hidden = YES; //hide error message label
return YES;
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSLog(@"got auth challange");

if ([challenge previousFailureCount] == 0)
{
    NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
                                             initWithHost: @"42.61.46.2"
                                             port: 80
                                             protocol: @"http"
                                             realm: nil authenticationMethod : NSURLAuthenticationMethodNTLM];                                                   

    [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:    [NSURLCredential credentialWithUser:@"example" password:@"example"     persistence:NSURLCredentialPersistenceForSession] forProtectionSpace:protectionSpace];



}
else
{
    [[challenge sender] cancelAuthenticationChallenge:challenge];
}

}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"received response via nsurlconnection");



NSURL* url = [NSURL URLWithString:@"http://42.61.46.2"]; //create a NSURL object

if(activeWebView == nil)
{
    //If there is no activeWebview i.e. no webview in the collection, create a new one and     show it

    [self addWebViewWithURL:url];
}
else
    {
           NSString *url = @"http://42.61.46.2";
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];

    [activeWebView loadRequest:urlRequest];
    }
}



- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
{
    return NO;
}
4

1 回答 1

0

通过将它用于您的 didReceiveAuthenticationChallenge,它对我有用:

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:    (NSURLAuthenticationChallenge *)challenge
{
    NSLog(@"got auth challange");

    if ([challenge previousFailureCount] ==0)
    {
        [[challenge sender]  useCredential:[NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession] forAuthenticationChallenge:challenge];


    }
    else
{
    NSLog(@"Did cancelAuthenticationChallenge");
    [[challenge sender] cancelAuthenticationChallenge:challenge];
}

}

于 2014-01-13T18:27:49.047 回答