Get host name and port(Object-c)
/************************************************************************** @param pChHostName: [output]Get system proxy host name @param pChPort: [output]Get system proxy port @param iProxyType:[input]ProxyType: 1(HTTP);2(HTTPS);.... **************************************************************************/ bool MacGetProxyHostNameAndPort(int iProxyType, char *pChHostName, char *pChPort) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; bool bRet = false; NSDictionary *proxyConfiguration = NSMakeCollectable([(NSDictionary*) SCDynamicStoreCopyProxies(NULL) autorelease]); if (!proxyConfiguration) { [pool release]; return bRet; } //Init proxy type NSString *nsStrProxyEnable = nil; NSString *nsStrProxyHostName = nil; NSString *nsStrProxyPort = nil; if (2 == iProxyType) { nsStrProxyEnable = (NSString*)[proxyConfiguration objectForKey:@"HTTPSEnable"]; nsStrProxyHostName = (NSString*)[proxyConfiguration objectForKey:@"HTTPSProxy"]; nsStrProxyPort = [NSString stringWithFormat:@"%d", [[proxyConfiguration objectForKey:@"HTTPSPort"] intValue]]; } else { nsStrProxyEnable = (NSString*)[proxyConfiguration objectForKey:@"HTTPEnable"]; nsStrProxyHostName = (NSString*)[proxyConfiguration objectForKey:@"HTTPProxy"]; nsStrProxyPort = [NSString stringWithFormat:@"%d", [[proxyConfiguration objectForKey:@"HTTPPort"] intValue]]; } int iProxyEnable = [nsStrProxyEnable intValue]; if (1 == iProxyEnable) { const char *pHostName = [nsStrProxyHostName UTF8String]; int iLen = [nsStrProxyHostName length]; memset(pChHostName, 0, 200); memcpy(pChHostName, pHostName, iLen); const char *pPort = [nsStrProxyPort UTF8String]; iLen = [nsStrProxyPort length]; memset(pChPort, 0, 200); memcpy(pChPort, pPort, iLen); bRet = true; } [pool drain]; return bRet; }
ps:本博文只用于日常备注及学习交流,请勿用于任何商业用途,涉及网摘等资料如有侵犯原作者权利,请联系确保及时更正!