×
Namespaces

Variants
Actions
Revision as of 05:32, 8 March 2012 by liuting (Talk | contribs)

在QT中如何通过网络接入点ID来获得相应的网关信息

From Nokia Developer Wiki
Jump to: navigation, search

介绍

在开发过程中,我们有时候需要获得接入点得网关信息,遗憾的是QT 没有提供这样的 函数,所以我们必须在通过SYMBIAN API 传入相应的接入点ID,来获得网关信息,实现代码如下:

具体实现

首先需要设置.PRO文件

LIBS += -lcmmanager

其次引入头文件

#include <cmmanager.h>
#include <cmconnectionmethoddef.h>

实现proxyL函数

void  proxyL( int aIapId)
{
TBool usesProxy( EFalse );
RCmManager cm;
cm.OpenLC();
usesProxy = cm.GetConnectionMethodInfoBoolL( aIapId, CMManager::ECmProxyUsageEnabled );
TUint32 proxyServerPort = 0;
HBufC* proxyServerName = NULL;
if(usesProxy)
{
proxyServerPort = cm.GetConnectionMethodInfoIntL( aIapId, CMManager::ECmProxyPortNumber );
proxyServerName = cm.GetConnectionMethodInfoStringL( aIapId, CMManager::ECmProxyServerName );
}
if(!proxyServerName)
{
noproxy();
}
else
{
CleanupStack::PushL(proxyServerName);
QString proxyHostName("");
int len = proxyServerName->Length();
if(len > 0)
{
proxyHostName = QString((QChar*)proxyServerName->Ptr(), len);
}
if (proxyHostName.isEmpty())
{
noproxy();
}
else
{
m_isProxyUsed = true;
m_proxy.setType(QNetworkProxy::HttpProxy);
m_proxy.setHostName(proxyHostName);
m_proxy.setPort(proxyServerPort);
}
CleanupStack::PopAndDestroy( proxyServerName );
}
CleanupStack::PopAndDestroy(); // cm
qDebug() << "<< DownloadMgtServer::proxyL()";
 
}

实现noproxy函数

void noproxy(){
m_isProxyUsed = false;
m_proxy.setType(QNetworkProxy::NoProxy);
m_proxy.setHostName("");
m_proxy.setPort(0);
 
}

最后在我们的应用中通过QNetWorkConfigutation得到接入点ID传入proxy 函数,并设置应用程序网关

QNetworkConfiguration config = session->configuration();
QString iapID = config.identifier()
 
if (iapId != "") {
if (iapId.startsWith("i_", Qt::CaseInsensitive)) {
 
iapId.remove(0, 2);
}
 
int identifier = iapId.toInt();
proxyL(identifier);
 
QNetworkProxy::setApplicationProxy(m_proxy);

相关链接


Add categories below. Remove Category:Draft when the page is complete or near complete

105 page views in the last 30 days.
×