[英]Pop the Proxy Authentication dialog when a proxy challenge provides a 407 response with a Proxy-Authenticate header
In my App I've configured a proxy for the NSURLSessionConfiguration object so the App can use the proxy. 在我的应用程序中,我为NSURLSessionConfiguration对象配置了代理,以便应用程序可以使用该代理。 When the proxy requires authentication, the App will ask for username and password via the delegate method "URLSession:task:didReceiveChallenge:completionHandler:" and makes sure that the request can continue.
当代理需要身份验证时,应用程序将通过委托方法“ URLSession:task:didReceiveChallenge:completionHandler:”请求用户名和密码,并确保请求可以继续。
Normal in HTTP requests, but the HTTPS requests popup a dialog stating the proxy authentication is required and gives the user the choice to do this "later" or directly go to the system settings. 在HTTP请求中正常,但在HTTPS请求中会弹出一个对话框,指出需要进行代理身份验证,并为用户提供了“稍后”执行此操作或直接进入系统设置的选择。
In addition, the dialog pops up even before the delegate method "didReceiveChallenge" of the NSUrlSession above, the application does not get an opportunity to provide credentials before iOS displays it. 此外,甚至在上述NSUrlSession的委托方法“ didReceiveChallenge”之前,该对话框也会弹出,该应用程序没有机会在iOS显示它之前提供凭据。
Does anyone else seeing this and knows how to fix this? 还有其他人看到这个并且知道如何解决吗?
proxy server response header : 代理服务器响应标头 :
HTTP/1.1 407 Proxy Authentication Required 需要HTTP / 1.1 407代理身份验证
Content-Type: text/html 内容类型:text / html
X-Squid-Error: ERR_CACHE_ACCESS_DENIED 0 X-乌贼错误:ERR_CACHE_ACCESS_DENIED 0
Proxy-Authenticate: Basic realm="Basic" 代理验证:基本领域=“ Basic”
Proxy-Authenticate: Digest realm="Digest", nonce="xxxxxxxxxxxxxxxxxxxxxxxxxxx", qop="auth", stale=false 代理验证:摘要域=“摘要”,nonce =“ xxxxxxxxxxxxxxxxxxxxxxxxxxx”,qop =“ auth”,陈旧= false
X-Cache: MISS from proxy.xxxx.com X缓存:来自proxy.xxxx.com的MISS
Via: 1.1 proxy.xxxx.com 通过:1.1 proxy.xxxx.com
my demo code : 我的演示代码 :
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 mark - NSURLSessionTaskDelegate #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);
}
}
} }
I found the solution here .It seems like such problems are bugs.Instead of using the URLSession:task:didReceiveChallenge:completionHandler: delegate, a good workaround is to add a "Proxy-Authorization" header to the NSURLSessionConfiguration.Here is the code based on here : 我在这里找到了解决方案。似乎这些问题都是bug。与其使用URLSession:task:didReceiveChallenge:completionHandler:委托,一种不错的解决方法是在NSURLSessionConfiguration中添加“ Proxy-Authorization”标头。此处是基于代码的在这里 :
// 1 - define credentials as a string with format:
// "username:password"
//
NSString *username = @"USERID";
NSString *password = @"SECRET";
NSString *authString = [NSString stringWithFormat:@"%@:%@",
username,
password];
// 2 - convert authString to an NSData instance
NSData *authData = [authString dataUsingEncoding:NSUTF8StringEncoding];
// 3 - build the header string with base64 encoded data
NSString *authHeader = [NSString stringWithFormat: @"Basic %@",
[authData base64EncodedStringWithOptions:0]];
// 4 - create an NSURLSessionConfiguration instance
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
// 5 - add custom headers, including the Authorization header
[configuration setHTTPAdditionalHeaders:@{
@"Proxy-Authorization": authHeader
}
];
// 6 - set proxy dictionary
NSDictionary *proxyDict = @{
@"HTTPEnable" : @1,
(NSString *)kCFStreamPropertyHTTPProxyHost : @"ip",
(NSString *)kCFStreamPropertyHTTPProxyPort : proxyPortNumber,
@"HTTPSEnable" : @1,
(NSString *)kCFStreamPropertyHTTPSProxyHost : @"ip",
(NSString *)kCFStreamPropertyHTTPSProxyPort : proxyPortNumber,
};
configuration.connectionProxyDictionary = proxyDict;
// 7 - create an NSURLSession instance
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:operationQueue];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.