如何在OSX上使用WPAD检索PAC脚本?提取“ http://wpad/wpad.dat”的内容是否足够,希望DNS为该约定预先配置“ wpad”?

还有更“正式”的方法吗?

最佳答案

以下是获取给定URL的PAC代理的方法:

#import <Foundation/Foundation.h>
#import <CoreServices/CoreServices.h>
#import <SystemConfiguration/SystemConfiguration.h>

CFArrayRef CopyPACProxiesForURL(CFURLRef targetURL, CFErrorRef *error)
{
    CFDictionaryRef proxies = SCDynamicStoreCopyProxies(NULL);
    if (!proxies)
        return NULL;

    CFNumberRef pacEnabled;
    if ((pacEnabled = (CFNumberRef)CFDictionaryGetValue(proxies, kSCPropNetProxiesProxyAutoConfigEnable)))
    {
        int enabled;
        if (CFNumberGetValue(pacEnabled, kCFNumberIntType, &enabled) && enabled)
        {
            CFStringRef pacLocation = (CFStringRef)CFDictionaryGetValue(proxies, kSCPropNetProxiesProxyAutoConfigURLString);
            CFURLRef pacUrl = CFURLCreateWithString(kCFAllocatorDefault, pacLocation, NULL);
            CFDataRef pacData;
            SInt32 errorCode;
            if (!CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault, pacUrl, &pacData, NULL, NULL, &errorCode))
                return NULL;

            CFStringRef pacScript = CFStringCreateFromExternalRepresentation(kCFAllocatorDefault, pacData, kCFStringEncodingISOLatin1);
            if (!pacScript)
                return NULL;

            CFArrayRef pacProxies = CFNetworkCopyProxiesForAutoConfigurationScript(pacScript, targetURL, error);
            return pacProxies;
        }
    }

    return NULL;
}

int main(int argc, const char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    CFURLRef targetURL = (CFURLRef)[NSURL URLWithString : @"http://stackoverflow.com/questions/4379156/retrieve-pac-script-using-wpad-on-osx/"];
    CFErrorRef error = NULL;
    CFArrayRef proxies = CopyPACProxiesForURL(targetURL, &error);
    if (proxies)
    {
        for (CFIndex i = 0; i < CFArrayGetCount(proxies); i++)
        {
            CFDictionaryRef proxy = CFArrayGetValueAtIndex(proxies, i);
            NSLog(@"%d\n%@", i, [(id)proxy description]);
        }
        CFRelease(proxies);
    }

    [pool drain];
}


为了简单起见,此代码充满了泄漏(您应释放通过Copy和Create函数获得的所有内容),并且不处理任何潜在的错误。

关于cocoa - 在OSX上使用WPAD检索PAC脚本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4379156/

10-09 02:34