1

私のアプリでは、アプリがプロキシを使用できるように、NSURLSessionConfiguration オブジェクトのプロキシを構成しました。プロキシが認証を必要とする場合、アプリはデリゲート メソッド「URLSession:task:didReceiveChallenge:completionHandler:」を介してユーザー名とパスワードを要求し、要求が続行できることを確認します。

HTTP 要求では正常ですが、HTTPS 要求では、プロキシ認証が必要であることを示すダイアログがポップアップ表示され、ユーザーはこれを「後で」行うか、システム設定に直接移動するかを選択できます。

さらに、上記の NSUrlSession のデリゲート メソッド「didReceiveChallenge」の前でもダイアログがポップアップします。アプリケーションは、iOS が表示する前に資格情報を提供する機会を得ません。

他の誰かがこれを見て、これを修正する方法を知っていますか?

プロキシ サーバーの応答ヘッダー:

HTTP/1.1 407 プロキシ認証が必要です

コンテンツタイプ: text/html

X-Squid-エラー: ERR_CACHE_ACCESS_DENIED 0

Proxy-Authenticate: Basic realm="Basic"

プロキシ認証: Digest realm="Digest"、nonce="xxxxxxxxxxxxxxxxxxxxxxxxxxx"、qop="auth"、stale=false

X-キャッシュ: 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 mark - 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);
    }
}

}

4

1 に答える 1