首页 > 代码库 > Get host name and port(Object-c)

Get host name and port(Object-c)

#import <SystemConfiguration/SCDynamicStoreCopySpecific.h>
#import <SystemConfiguration/SCSchemaDefinitions.h>
#import <Security/Security.h>

void GetProxyHostNameAndPort((CFStringRef proxyEnableKey, CFStringRef proxyHostNameKey, CFStringRef proxyPortKey, NSString **hostNamePtr, int *portPtr)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

//Get general proxy info
CFDictionaryRef proxyInfoCPtr = SCDynamicStoreCopyProxies(NULL);
NSDictionary *proxyInfo = (__bridge NSDictionary *) proxyInfoCPtr;
NSNumber *proxyEnabled = proxyInfo[(__bridge NSString *)proxyEnableKey];

//Check proxy enabled?
if (![proxyEnabled intValue])
{
*hostNamePtr = nil;
*portPtr = 0;

[pool release];
return;
}

*hostNamePtr = proxyInfo[(__bridge NSString *)proxyHostNameKey];
NSNumber *portNumber = proxyInfo[(__bridge NSString *)proxyPortKey];
*portPtr = [portNumber intValue];

[pool release];
return;
}

-------------------------------------------------------------
GetProxyHostNameAndPort(kSCPropNetProxiesHTTPEnable, kSCPropNetProxiesHTTPProxy, kSCPropNetProxiesHTTPPort, hostName, port);
GetProxyHostNameAndPort(kSCPropNetProxiesHTTPSEnable, kSCPropNetProxiesHTTPSProxy, kSCPropNetProxiesHTTPSPort, hostName, port);

Get host name and port(Object-c)