⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpengine.cpp

📁 symbian http/post引擎 xml引擎 支持IAP设置
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			{
			MHTTPDataSupplier* body = aTransaction.Response().Body();
			if(body==NULL) return;
			TPtrC8 dataChunk;
			body->GetNextDataPart(dataChunk);
			body->ReleaseData();			
		    // Append to iReceivedData
		    if (!iReceivedData)
		    {
			    iReceivedData = dataChunk.Alloc();
		    }
		    else
		    {
		    	iReceivedData = iReceivedData->ReAllocL(iReceivedData->Length() + dataChunk.Length());
		    	iReceivedData->Des().Append(dataChunk);
		    }
			
			}
			break;

		// Indicates that header & body of response is completely received.
		case THTTPEvent::EResponseComplete:
			{

			}
			break;
		
		// Indicates that transaction succeeded.
		case THTTPEvent::ESucceeded:
			{
			iTransaction.Close();
			iRunning = EFalse;
			
			RFileLogger::Write(KLogDir,KLogFileName,EFileLoggingModeAppend,_L("服务器返回数据成攻!"));
			
			CResponse* response = CResponse::NewLC();
			
			// After transaction has been closed, leaves are no 
			// longer handled by the calling active object.
			TRAPD(error,
			response->InputDataL(*iReceivedData);
			iTransactionObserver.SuccessL(*response);
			CheckRefreshL();)
			PanicIfError(error);
			
			CleanupStack::Pop(response);
			if(response)
				{
				delete response;
				response=NULL;
				}
			
  	        if(iReceivedData != NULL)
  	        	{
		       delete iReceivedData;
		       iReceivedData = NULL;
  	        	}			

			}
			break;

		// Transaction completed with failure.
		case THTTPEvent::EFailed:
			{			 
			iTransaction.Close();
			iRunning = EFalse;

			RFileLogger::Write(KLogDir,KLogFileName,EFileLoggingModeAppend,_L("服务器返回数据失败!"));

			// After transaction has been closed, leaves are no 
			// longer handled by the calling active object.
			TRAPD(error,
			iTransactionObserver.FailedL(0);
			CheckRefreshL();)
			PanicIfError(error);
			}
			break;

		default:
			// There are more events in THTTPEvent, but they are not usually 
			// needed. However, event status smaller than zero should be handled 
			// correctly since it's error.
			{
			if (aEvent.iStatus < 0)
				{
				// Just close the transaction on errors
				iTransaction.Close();
				iRunning = EFalse;
				
				RFileLogger::Write(KLogDir,KLogFileName,EFileLoggingModeAppend,_L("服务器返回数据时出现错误!"));

				// After transaction has been closed, leaves are no 
				// longer handled by the calling active object.
				TRAPD(error,
				iTransactionObserver.FailedL(aEvent.iStatus);
				CheckRefreshL();)
				PanicIfError(error);
				} 
			else 
				{
				// Other events are not errors (e.g. permanent and temporary
				// redirections)
				}
			}
			break;
		}
	}

// ----------------------------------------------------
// CHttpEngine::MHFRunError()
// Called when a leave occurs in handling a transaction 
// event.
// ----------------------------------------------------
//	
TInt CHttpEngine::MHFRunError(	TInt aError, 
										RHTTPTransaction /*aTransaction*/, 
										const THTTPEvent& /*aEvent*/)
	{
	// only report about the error.
	TRAPD(error, iTransactionObserver.FailedL(aError);)
	return error;
	}

// ----------------------------------------------------
// CHttpEngine::CancelTransaction()
// Used for cancelling an HTTP transaction or the 
// creation of a GPRS connection.
// ----------------------------------------------------
//		
void CHttpEngine::CancelTransaction()
	{
	// if we are opening up a GPRS connection.
	RFileLogger::Write(KLogDir,KLogFileName,EFileLoggingModeAppend,_L("用户取消连接!"));

	if (iOpeningConnection)
		{
		iConnOpener->Cancel();
		TRAPD(error, iTransactionObserver.CancelledL())
		PanicIfError(error);
		return;
		}
		
	// HTTP transaction not running
	if(!iRunning) 
		return;

	// Close() also cancels transaction (Cancel() can also be used but 
	// resources allocated by transaction must be still freed with Close())
	iTransaction.Close();

	iRunning = EFalse;
	TRAPD(	error,
			iTransactionObserver.CancelledL());
	PanicIfError(error);
	}

// ----------------------------------------------------
// CHttpEngine::ResetData()
// Resets the received data.
// ----------------------------------------------------
//	
void CHttpEngine::ResetData()
	{
	if(iReceivedData)
		{
		delete iReceivedData;
		iReceivedData = NULL;
		}
	if (iDataSupplier)
		{
			delete iDataSupplier;
			iDataSupplier=NULL;
		}
	}
	
// ----------------------------------------------------
// CHttpEngine::CheckRefreshL()
// An SMS update message, indicating an updated task
// list on the server, may have been received during
// an HTTP transaction. This function checks if such a
// postponed request has been made and, assuming an
// HTTP transaction isn't running, executes the
// request.
// ----------------------------------------------------
//
void CHttpEngine::CheckRefreshL()
	{
	// if HTTP transaction is running, don't fetch tasks.
	if (iRunning)
		{
		return;
		}
		
	if (iDoRefresh)
		{
		iDoRefresh = EFalse;
		PostRequestL(ETrue,_L8("<test>"));
		}
	}

// ----------------------------------------------------
// CHttpEngine::SetAutomaticUpdateL()
// Automatic task download can be set on or off through
// this function. If on, the client will try to fetch
// a new task list as soon as it receives an SMS
// message.
// ----------------------------------------------------
//	
void CHttpEngine::SetAutomaticUpdateL(const TBool& aOn)
	{
	iAutomaticUpdate = aOn;
	if (iAutomaticUpdate)
		{
		CheckRefreshL();
		}
	}

// ----------------------------------------------------
// CHttpEngine::LoadIapsL()
// Loads all IAPs of the device.
// ----------------------------------------------------
//	
void CHttpEngine::LoadIapsL()
	{
	// open commdb
	CCommsDatabase* commDb = CCommsDatabase::NewL(EDatabaseTypeIAP);
	CleanupStack::PushL(commDb);

	// open IAP table
	CCommsDbTableView* commView = commDb->OpenIAPTableViewMatchingBearerSetLC(ECommDbBearerCSD|ECommDbBearerGPRS,ECommDbConnectionDirectionOutgoing);
	
	// search all IAPs
	if (commView->GotoFirstRecord() == KErrNone)
		{
		do
			{
			TIap iap;
			commView->ReadTextL(TPtrC(COMMDB_NAME), iap.iName);
			commView->ReadUintL(TPtrC(COMMDB_ID), iap.iId);
			User::LeaveIfError(iIAPs.Append(iap));
			}
		while (commView->GotoNextRecord() == KErrNone);
		}
	CleanupStack::PopAndDestroy(/*commView*/);
	CleanupStack::PopAndDestroy(/*commDb*/);
	}

// ----------------------------------------------------
// CHttpEngine::Iaps()
// Returns all IAPs of the device.
// ----------------------------------------------------
//		
RArray<TIap>& CHttpEngine::Iaps() 
	{
	return iIAPs;
	}
	
// ----------------------------------------------------
// CHttpEngine::ConnectL()
// Will open up a (GPRS) connection.
// ----------------------------------------------------
//		
void CHttpEngine::ConnectL()
	{
	TBool connected = EFalse;
	
	// Lets first check are we already connected.
	TUint connectionCount;
	User::LeaveIfError(iConnection.EnumerateConnections(connectionCount));
	TPckgBuf<TConnectionInfoV2> connectionInfo; 
	for (TUint i = 1; i <= connectionCount; i++)
		{
		User::LeaveIfError(iConnection.GetConnectionInfo(i, connectionInfo));
		if (connectionInfo().iIapId == iIap)
			{
			connected = ETrue;
			break;
			}
		}
	
	// Not yet connected, start connection
	if (!connected)
		{
		//Define preferences for connection
		TCommDbConnPref prefs;
		prefs.SetIapId(iIap);
		prefs.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);

		//Start Connection
		iOpeningConnection = ETrue;
		iTransactionObserver.OpeningConnectionL();
		iConnOpener->OpenConnection(prefs);
		return;
		}

	//Set properties for the HTTP session
	RStringPool strP = iHttpSession.StringPool();
	RHTTPConnectionInfo connInfo = iHttpSession.ConnectionInfo();
	connInfo.SetPropertyL ( strP.StringF(HTTP::EHttpSocketServ, RHTTPSession::GetTable() ), THTTPHdrVal (iSockServ.Handle()) );
	TInt connPtr = REINTERPRET_CAST(TInt, &iConnection);
	connInfo.SetPropertyL ( strP.StringF(HTTP::EHttpSocketConnection, RHTTPSession::GetTable() ), THTTPHdrVal (connPtr) );
	
	DoPostL();
	}

// ----------------------------------------------------
// CHttpEngine::ConnectionCreated()
// Called when opening a GPRS connection has finished.
// ----------------------------------------------------
//	
void CHttpEngine::ConnectionCreated(const TInt& aError)
	{
	iOpeningConnection = EFalse;
	
	RFileLogger::Write(KLogDir,KLogFileName,EFileLoggingModeAppend,_L("HTTP连接成攻!"));
	
	if (aError != KErrNone)
		{
		TRAPD(error, iTransactionObserver.FailedL(aError))
		PanicIfError(error);
		}
	else
		{
		TRAPD(error,
			//Set properties for the HTTP session
			RStringPool strP = iHttpSession.StringPool();
			RHTTPConnectionInfo connInfo = iHttpSession.ConnectionInfo();
			connInfo.SetPropertyL ( strP.StringF(HTTP::EHttpSocketServ, RHTTPSession::GetTable() ), THTTPHdrVal (iSockServ.Handle()) );
			TInt connPtr = REINTERPRET_CAST(TInt, &iConnection);
			connInfo.SetPropertyL ( strP.StringF(HTTP::EHttpSocketConnection, RHTTPSession::GetTable() ), THTTPHdrVal (connPtr) );
			DoPostL();
			)
		PanicIfError(error);
		}
	}
void CHttpEngine::ConvGbk2Uni(TDesC8& original, TDes& res,TUint characterSetIdentifier) 
	{
		RFs aFileServerSession;
		aFileServerSession.Connect();
		CCnvCharacterSetConverter* converter=CCnvCharacterSetConverter::NewLC();
		
		if(converter->PrepareToConvertToOrFromL(characterSetIdentifier,aFileServerSession)!=CCnvCharacterSetConverter::EAvailable)
		User::Leave(KErrNotSupported);
		
		TInt state=CCnvCharacterSetConverter::KStateDefault;
		
		TPtrC8 str( original );
		HBufC* iInfoText = HBufC::NewL( str.Length() );
		TPtr16 ptr = iInfoText->Des();
		
		if(CCnvCharacterSetConverter::EErrorIllFormedInput == converter->ConvertToUnicode(ptr, str, state))
		User::Leave(KErrArgument);
		
		res.Zero() ; 
		res.Copy(ptr) ; 
		aFileServerSession.Close();
		CleanupStack::PopAndDestroy(); 
		delete iInfoText;
	}

void CHttpEngine::ConvUni2Gbk(TDesC& original, TDes8& res,TUint characterSetIdentifier)
	{
		RFs aFileServerSession;
		aFileServerSession.Connect();
		TInt state=CCnvCharacterSetConverter::KStateDefault ;
		CCnvCharacterSetConverter* iConv ; 
		iConv = CCnvCharacterSetConverter::NewLC();
		if(iConv->PrepareToConvertToOrFromL(characterSetIdentifier,
		aFileServerSession)!=CCnvCharacterSetConverter::EAvailable)
		User::Leave(KErrNotSupported);
		iConv->ConvertFromUnicode(res, original, state) ; 
		aFileServerSession.Close();
		CleanupStack::PopAndDestroy() ; 
	}

// End of file

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -