gpt4 book ai didi

cocoa - 在 OSX 上使用 WPAD 检索 PAC 脚本

转载 作者:行者123 更新时间:2023-12-03 16:13:39 25 4
gpt4 key购买 nike

如何在 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];
}

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com