Ios 当代理质询提供带有代理身份验证标头的407响应时,弹出代理身份验证对话框

Ios 当代理质询提供带有代理身份验证标头的407响应时,弹出代理身份验证对话框,ios,objective-c,proxy,proxy-authentication,Ios,Objective C,Proxy,Proxy Authentication,在我的应用程序中,我为NSURLSessionConfiguration对象配置了一个代理,以便应用程序可以使用该代理。当代理需要身份验证时,应用程序将通过委托方法“URLSession:task:didReceiveChallenge:completionHandler:”请求用户名和密码,并确保请求可以继续 HTTP请求中正常,但HTTPS请求弹出一个对话框,说明需要代理身份验证,并允许用户选择“稍后”执行此操作或直接转到系统设置 此外,即使在上面NSUrlSession的委托方法“didR

在我的应用程序中,我为NSURLSessionConfiguration对象配置了一个代理,以便应用程序可以使用该代理。当代理需要身份验证时,应用程序将通过委托方法“URLSession:task:didReceiveChallenge:completionHandler:”请求用户名和密码,并确保请求可以继续

HTTP请求中正常,但HTTPS请求弹出一个对话框,说明需要代理身份验证,并允许用户选择“稍后”执行此操作或直接转到系统设置

此外,即使在上面NSUrlSession的委托方法“didReceiveChallenge”之前,该对话框也会弹出,在iOS显示之前,应用程序没有机会提供凭据

有没有其他人看到这一点并知道如何解决这一问题

代理服务器响应头

    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];
需要HTTP/1.1 407代理身份验证

内容类型:text/html

X-Squid-Error:错误缓存访问被拒绝0

代理身份验证:Basic realm=“Basic”

代理身份验证:Digest realm=“Digest”, nonce=“xxxxxxxxxxxxxxxxxxxxxxxx”,qop=“auth”,stale=false

X-Cache:proxy.xxxx.com未命中

通过: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);
    }
}
}

我找到了解决方案。似乎这些问题都是bug。与其使用URLSession:task:didReceiveChallenge:completionHandler:delegate,一个好的解决方法是在NSURLSessionConfiguration中添加一个“Proxy Authorization”头。下面是基于以下内容的代码: