2

我在我的应用程序中使用代理支持,它适用于输入到 wifi 设置面板的手动代理,但是当我们在该特定 Wifi 连接的 HTTP 代理部分的“自动”选项中放入自动代理 pac 文件时,它不能正常工作。

该信息不包含在 PAC 文件中,因此是动态的,那么如何让我的 iOS 应用程序使用 pac 文件获取代理信息?

4

1 回答 1

1

如果您使用的是 Objective C,您可以参考以下代码片段来获取代理详细信息(使用kCFNetworkProxiesProxyAutoConfigURLString):

 CFDictionaryRef dicRef = CFNetworkCopySystemProxySettings();
//Manual Proxy Details
const NSString* proxyStr = (const NSString*)CFDictionaryGetValue(dicRef, (const void*)kCFNetworkProxiesHTTPProxy);

//Auto Config Proxy Details
NSString* proxyStr2 = ( NSString*)CFDictionaryGetValue(dicRef, (const void*)kCFNetworkProxiesProxyAutoConfigURLString);
// Return something similar to http://someIPAddress:someport/pacfile

if (proxyStr2 != nil)
{
    NSLog(@"Proxy %@", proxyStr2);
    // Create an url with the proxy pac
    NSURL *url = [NSURL URLWithString:proxyStr2];
    if (url != nil)
    {
        NSString* urlPath = [url path];
        // Url path: /pacfile
        NSLog(@"Url %@", urlPath);
        NSError* error;

        // Go fetch the content the url (PAC Content)
        NSString *content = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];

        if (content == nil)
        {
            NSLog(@"StringWithContentsOfUrl Error: %@", error);
        }

        if (content != nil)
        {
            NSLog(@"PAC content: %@", content);

            // Parsing of the pac file. This is just a sample parsing script and may change based on what your PAC returns
            NSUInteger firstMatch = [content rangeOfString:@"PROXY "].location + 6;
            NSUInteger secondMatch = [content rangeOfString:@"\"" options:0 range:NSMakeRange(firstMatch , [content length] - firstMatch)].location;

            if (firstMatch < 10000 && secondMatch < 10000 && firstMatch > 0 && secondMatch > 0)
            {
               //Just a random label
                self.randLabel3.text=[NSString stringWithFormat: @"First match and second match: %tu %tu", firstMatch, secondMatch];

            }
        }
    }
}
于 2018-07-22T19:52:00.777 回答