Error while Proxy authentication fails.

Hi,


My application is doing a normal HTTP request that goes through a proxy server. The proxy server returns a response with some error code when the authenticaton fails. But when I make a request from my app, I am getting the following error "Error in service call The operation couldn’t be completed. (kCFErrorDomainCFNetwork error 310.)". What does this error mean? I want to perform a task upon proxy authentication failure. How could I achieve that?


Below is my code snippet:


   NSString* proxyHost = @"aa:bb:cc:dd";
    NSNumber* proxyPort = [NSNumber numberWithInt: 80];
    NSDictionary *proxyDict = @{
                                @"HTTPEnable"  : [NSNumber numberWithInt:1],
                                (NSString *)kCFStreamPropertyHTTPProxyHost  : proxyHost,
                                (NSString *)kCFStreamPropertyHTTPProxyPort  : proxyPort,
                             
                                @"HTTPSEnable" : [NSNumber numberWithInt:1],
                                (NSString *)kCFStreamPropertyHTTPSProxyHost : proxyHost,
                                (NSString *)kCFStreamPropertyHTTPSProxyPort : proxyPort,
                                };

    NSURLSessionConfiguration *configuration1 = [NSURLSessionConfiguration defaultSessionConfiguration];
    configuration1.connectionProxyDictionary = proxyDict;
    NSString *proxyUsername = @"username";
    NSString *proxypassword = @"password";
    NSString *authenticationString = [NSString stringWithFormat:@"%@:%@", proxyUsername, proxypassword];
    NSData *authenticationData = [authenticationString dataUsingEncoding:NSASCIIStringEncoding];
    NSString *authenticationValue = [authenticationData base64Encoding];
    authenticationValue = [@"Basic " stringByAppendingString:authenticationValue];
    [request  setValue:authenticationValue forHTTPHeaderField:@"Proxy-Authorization"];


    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration1 delegate:self delegateQueue:nil];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:
                                  ^(NSData *data, NSURLResponse *response, NSError *error) {
                                      NSLog(@"error is %@",error);
                                      NSLog(@"NSURLSession got the response [%@]", response);
                                      NSLog(@"NSURLSession got the data [%@]", data);
                                  }];

    [dataTask resume];




Thanks in advance!!

  • Just wanted to say thank you so much, your code helped me a lot 🙏🏻

Add a Comment

Replies

In the

kCFErrorDomainCFNetwork
domain, error 310 is
kCFErrorHTTPSProxyConnectionFailure
, which is kinda vague.
[request  setValue:authenticationValue forHTTPHeaderField:@"Proxy-Authorization"];

To be clear, setting the

Proxy-Authorization
header on a request is not something we officially support. Proxy authorisation, along with a bunch of other core HTTP related stuff, is handled by NSURLSession itself. HTTP status 407 responses from the proxy should be turned into authentication challenges (with the
isProxy
property set on the protection space), delivered via NSURLSession’s authentication challenge delegate callback.

Note The documentation is quite specific about what HTTP headers are ‘owned’ by NSURLSession.

Having said that, there’s been a bunch of problems recently where the (nominally correct) authentication challenge mechanism is not working as designed — especially in the context of custom authenticating proxies — and the only way to work around that is to monkey with the

Proxy-Authorization
header.

My recommendation right now is that you try to use the preferred mechanism for proxy authentication and then, if that fails, get back to us with the details.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"