Get host name and port(Object-c)

本文提供了一个Cocoa代码片段,用于获取系统级别的HTTP和HTTPS代理主机名和端口号。通过调用特定的方法并使用字典结构存储配置信息,该示例展示了如何灵活地获取和处理代理设置。
/**************************************************************************
@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;
}

 

转载于:https://www.cnblogs.com/sz-leez/p/4080153.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值