iOS shows a dialogue "Proxy authentication required" even after giving the credentials in "Proxy-Authorization" header of the request

Hi, I am trying to direct all the requests made by the webview through a proxy by implementing a custom HTTP protocol. Below is my code snippet:



- (void)startLoading {
  
    /------------BEGIN ATTACHING PROXY TO THE REQUEST-----------*/
    NSString* proxyHost = @"xx.xx.xx.xx";
    NSNumber* proxyPort = [NSNumber numberWithInt: 8080];
    NSDictionary *proxyDict = @{
                                @"HTTPEnable"  : [NSNumber numberWithInt:1],
                                (NSString *)kCFStreamPropertyHTTPProxyHost  : proxyHost,
                                (NSString *)kCFStreamPropertyHTTPProxyPort  : proxyPort,
                               
                                @"HTTPSEnable" : [NSNumber numberWithInt:1],
                                (NSString *)kCFStreamPropertyHTTPSProxyHost : proxyHost,
                                (NSString *)kCFStreamPropertyHTTPSProxyPort : proxyPort,
                                };
    NSMutableURLRequest *req = [correctedRequest mutableCopy];
   
    /
    NSString *proxyUsername = @"abcd";
    NSString *proxypassword = @"abcd123";
    NSString *authenticationString = [NSString stringWithFormat:@"%@:%@", proxyUsername, proxypassword];
    NSData *authenticationData = [authenticationString dataUsingEncoding:NSASCIIStringEncoding];
    NSString *authenticationValue = [authenticationData base64Encoding];
    authenticationValue = [@"Basic " stringByAppendingString:authenticationValue];
   
    [req  setValue:authenticationValue forHTTPHeaderField:@"Proxy-Authorization"];
    correctedRequest = [req copy];
    NSURLSessionConfiguration *configuration1 = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    configuration1.connectionProxyDictionary = proxyDict;
    /------------END ATTACHING PROXY TO THE REQUEST-----------*/
   
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration1 delegate:self delegateQueue:nil];
    NSURLSessionDataTask * dataTask = [session dataTaskWithRequest:correctedRequest];
    [dataTask resume];
}


The above code works for some URL's/websited without asking the user to enter the proxy credentials manually but the same code doesnt work for some websites and a pop appears asking the user to enter the authentication credentials manually. What is the reason for such a behaviour? Do I need to make any changes in my code to get this fixed?


Thanks in advance!!

Replies

First things first, please file an enhancement request for WKWebView to support custom proxies directly. Doing this sort of thing via NSURLProtocol has two serious problems:

  • It’s a bit of a hack.

  • It only works with UIWebView (WKWebView does all of its networking out of process, and thus doesn’t ‘see’ your NSURLProtocol).

The last one is important because WKWebView is the future of web views on our platforms, whereas UIWebView is very much the past.

Please post your bug number, just for the record.

With regards your actual problem, you should take a look at this thread.

Share and Enjoy

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

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

Hi Eskimo,


I have two reason for not using WKWebkit. As you have earlier mentioned that WKWebView’s authentication challenge support is broken for

NSURLAuthenticationMethodClientCertificate
challenges, I cannot use this because I should also be able to handle 2-way SSL Authentication. Secondly, As of now, I can only do it for UIWebView as it is a requirement from my enterprise. Moreover, I should be able to attach the proxy for normal HTTP requests made by NSUrlSession too. I am facing an issue with the prompt asking me to enter the Proxy Authentication credentials manually even after providing those in the "Proxy-Authorization" header of the request. Is it a bug or is there any work-around to avoid this dialogue??


Note : The Popup is appearing only for certain URL's/Requests.


Thanks!!

Hi Eskimo,


I have reported this bug(#29751488).


Thanks!!