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;
}

 

posted @ 2014-11-06 22:51  sz_leez  阅读(306)  评论(0编辑  收藏  举报