【发布时间】:2013-07-13 07:21:40
【问题描述】:
我在我的应用程序中使用代理支持,它适用于输入到 wifi 设置面板的手动代理,但当我们将自动代理 pac 文件放入该特定 HTTP 代理部分的“自动”选项中时,它不能正常工作无线网络连接。
信息不包含在 PAC 文件中,因此是动态的,那么我如何让我的 iOS 应用程序使用 pac 文件来获取代理信息?
【问题讨论】:
标签: iphone ios cocoa-touch networking proxy
我在我的应用程序中使用代理支持,它适用于输入到 wifi 设置面板的手动代理,但当我们将自动代理 pac 文件放入该特定 HTTP 代理部分的“自动”选项中时,它不能正常工作无线网络连接。
信息不包含在 PAC 文件中,因此是动态的,那么我如何让我的 iOS 应用程序使用 pac 文件来获取代理信息?
【问题讨论】:
标签: iphone ios cocoa-touch networking proxy
如果您使用的是 Objective C,您可以参考以下 sn-p 获取代理详细信息(使用 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];
}
}
}
}
【讨论】: