在OSX上使用WPAD检索PAC脚本 [英] Retrieve PAC script using WPAD on OSX

查看:137
本文介绍了在OSX上使用WPAD检索PAC脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在OSX上使用WPAD检索PAC脚本?是否足以获取http://wpad/wpad.dat的内容,希望DNS为此约定预配置了wpad?

How do I retrieve the PAC script using WPAD on OSX? is it enough to fetch the contents of "http://wpad/wpad.dat" in hopes that the DNS has "wpad" pre-configured for this convention?

是否有更多的正式方法这样做?

is there a more "formal" method of doing this?

推荐答案

这里是如何获取PAC代理给定的URL: / p>

Here is how to get PAC proxies for a given URL:

#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];
}

为了简单起见,这个代码充满了漏洞您通过 创建功能所获得的一切),并且不会处理任何潜在错误。

For the sake of simplicity, this code is full of leaks (you should release everything you got through Copy and Create functions) and does not handle any potential error.

这篇关于在OSX上使用WPAD检索PAC脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆