5

我注意到我的应用程序没有使用机器可用的代理设置(我正在使用Charles 代理来测试代理配置)。使用 NSURLConnection 进行调用的应用程序的一部分正确使用代理并使用代理发出请求,应用程序的另一部分(显然是在 Mono 上运行的 MonoMac 应用程序)没有。

它不断发出请求,就好像没有配置代理一样。是否有一个函数或对象可以用来获取 NSURLConnection 正在使用的相同代理配置?

4

2 回答 2

10

为您提供所有代理信息的函数是SCDynamicStoreCopyProxies(),可以如下例所示调用它(完成后,您还必须访问CFRelease所有这些对象,因为它们都来自 CF 而不是直接 Cocoa 对象):

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

int main(int argc, const char * argv[])
{

  @autoreleasepool {

    CFDictionaryRef proxies = SCDynamicStoreCopyProxies(NULL);

    CFIndex count = CFDictionaryGetCount(proxies);

    NSLog(@"Number of keys is %ld", count);

    NSDictionary * proxyConfiguration = (NSDictionary*) proxies;

    for ( id key in proxyConfiguration.keyEnumerator ) {
      NSLog(@"Pair is %@ -> %@", key, [proxyConfiguration valueForKey: key]);
    }

  }
    return 0;
}

输出将类似于:

2012-11-07 16:33:57.844 network-test[6501:403] Number of keys is 12
2012-11-07 16:33:57.847 network-test[6501:403] Pair is HTTPEnable -> 1
2012-11-07 16:33:57.848 network-test[6501:403] Pair is HTTPSProxy -> 127.0.0.1
2012-11-07 16:33:57.848 network-test[6501:403] Pair is ExceptionsList -> (
    "www.google.com"
)
2012-11-07 16:33:57.849 network-test[6501:403] Pair is HTTPSPort -> 8888
2012-11-07 16:33:57.850 network-test[6501:403] Pair is __SCOPED__ -> {
    en1 =     {
        ExceptionsList =         (
            "www.google.com"
        );
        FTPPassive = 1;
        HTTPEnable = 1;
        HTTPPort = 8888;
        HTTPProxy = "127.0.0.1";
        HTTPSEnable = 1;
        HTTPSPort = 8888;
        HTTPSProxy = "127.0.0.1";
        SOCKSEnable = 1;
        SOCKSPort = 8889;
        SOCKSProxy = "127.0.0.1";
    };
}
2012-11-07 16:33:57.850 network-test[6501:403] Pair is HTTPProxy -> 127.0.0.1
2012-11-07 16:33:57.851 network-test[6501:403] Pair is SOCKSPort -> 8889
2012-11-07 16:33:57.852 network-test[6501:403] Pair is SOCKSProxy -> 127.0.0.1
2012-11-07 16:33:57.852 network-test[6501:403] Pair is HTTPSEnable -> 1
2012-11-07 16:33:57.853 network-test[6501:403] Pair is SOCKSEnable -> 1
2012-11-07 16:33:57.853 network-test[6501:403] Pair is HTTPPort -> 8888
2012-11-07 16:33:57.854 network-test[6501:403] Pair is FTPPassive -> 1
于 2012-11-07T22:04:07.697 回答
1

SystemConfiguration框架中的代理字典将包含此信息

于 2012-11-07T19:00:16.123 回答