在我的应用程序中,我为 NSURLSessionConfiguration 对象配置了一个代理,因此应用程序可以使用该代理。当代理需要身份验证时,App 会通过委托方法“URLSession:task:didReceiveChallenge:completionHandler:”询问用户名和密码,并确保请求可以继续。
在 HTTP 请求中正常,但 HTTPS 请求会弹出一个对话框,说明需要代理身份验证,并让用户选择“稍后”执行此操作或直接转到系统设置。
此外,即使在上面 NSUrlSession 的委托方法“didReceiveChallenge”之前弹出对话框,应用程序也没有机会在 iOS 显示之前提供凭据。
有没有其他人看到这个并且知道如何解决这个问题?
代理服务器响应头:
需要 HTTP/1.1 407 代理身份验证
内容类型:文本/html
X-Squid-错误:ERR_CACHE_ACCESS_DENIED 0
代理验证:基本领域="基本"
代理验证:Digest realm="Digest", nonce="xxxxxxxxxxxxxxxxxxxxxxxxxxx", qop="auth", stale=false
X-Cache:来自 proxy.xxxx.com 的 MISS
通过:1.1 proxy.xxxx.com
我的演示代码:
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
config.connectionProxyDictionary = @{
@"HTTPEnable" : @(1),
(NSString *)kCFStreamPropertyHTTPProxyHost : @"proxy.xxxx.com",
(NSString *)kCFStreamPropertyHTTPProxyPort : @(1234),
@"HTTPSEnable": @(1),
(NSString *)kCFStreamPropertyHTTPSProxyHost :@"proxy.xxxx.com",
(NSString *)kCFStreamPropertyHTTPSProxyPort : @(4321)
};
self.session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.xxxxx.com"] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
NSURLSessionTask *task = [self.session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"result:%@ error: %@",data, error.description);
}];
[task resume];
#pragma 标记 - NSURLSessionTaskDelegate
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)taskdidReceiveChallenge:(NSURLAuthenticationChallenge *)challengecompletionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
NSLog(@"task:%@",challenge.protectionSpace.authenticationMethod);
if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPDigest) {
NSURLCredential *cren = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceNone];
if (completionHandler) {
completionHandler(NSURLSessionAuthChallengeUseCredential,cren);
}
}
}