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

📄 httpxxxtools.cpp

📁 在Symbian OS S60 3rd上面
💻 CPP
字号:
/*
 ============================================================================
 Name		: HttpxxxTools.cpp
 Author	  : xxx
 Version	 : 1.0
 Copyright   : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 Description : CHttpxxxTools implementation
 ============================================================================
 */

#include "HttpxxxTools.h"
#include <rhttpconnectioninfo.h>
#include <COMMDB.H>
#include <http.h>
#include <CommDbConnPref.h>
#include <stringpool.h>
#include <RHTTPTransaction.h>
#include <thttphdrval.h>
#include <thttpevent.h>
#include <ECom.h>

_LIT8(KUserAgent, "Nokia");
_LIT8(KAccept, "*/*");

CHttpxxxTools::CHttpxxxTools(MHttpDataHandleObserver& aObserver)
:iObserver(aObserver), iPostData(NULL), iRunning(EFalse)
	{
	// No implementation required
	}

CHttpxxxTools::~CHttpxxxTools()
	{
	iSession.Close();
	iConnection.Close();
	iSocketServ.Close();
	if(iPostData)
		{
		delete iPostData;
		iPostData = NULL;
		}
	
	if(iBodyData)
			{
			delete iBodyData;
			iBodyData = NULL;
			}	
	}

CHttpxxxTools* CHttpxxxTools::NewLC(MHttpDataHandleObserver& aObserver)
	{
	CHttpxxxTools* self = new (ELeave)CHttpxxxTools(aObserver);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CHttpxxxTools* CHttpxxxTools::NewL(MHttpDataHandleObserver& aObserver)
	{
	CHttpxxxTools* self=CHttpxxxTools::NewLC(aObserver);
	CleanupStack::Pop(); // self;
	return self;
	}

void CHttpxxxTools::ConstructL()
	{
	TRAPD(err, iSession.OpenL());
		if(err != KErrNone) 
			{
			User::Leave(err);
			}
	InstallAuthenticationL(iSession);
//	SetupConnectionL();
	}
void CHttpxxxTools::SetupConnectionL()
	{
	if(iConnectionSetupDone)
		{
		return;
		}
	iConnectionSetupDone = ETrue;
	
	iSession.Close();

	TRAPD(err,iSession.OpenL());
	if(err != KErrNone)
		{
		User::Leave(err);		
		}
	// creat the socket connet;
	User::LeaveIfError(iSocketServ.Connect());
	User::LeaveIfError(iConnection.Open(iSocketServ));
	
	// creat the IAP connection communication database
	CCommsDatabase* communicationDataBase = CCommsDatabase::NewL(EDatabaseTypeIAP);
	CleanupStack::PushL(communicationDataBase);
	//initialize a database view
	CCommsDbConnectionPrefTableView* dataBaseView = 
	communicationDataBase->OpenConnectionPrefTableInRankOrderLC(ECommDbConnectionDirectionUnknown);
//	communicationDataBase->
//	communicationDataBaseOpenConnectionPrefTableInRankOrderLC(ECommDbConnectionDirectionUnknown);
	// go to the first record
	User::LeaveIfError(dataBaseView-> GotoFirstRecord());
	
	// declare a IAP connetction preference prefer
	
	CCommsDbConnectionPrefTableView :: TCommDbIapConnectionPref IAPpref;
	
	// read the IAP preference
	dataBaseView->ReadConnectionPreferenceL(IAPpref);
	
	//get the IAPID
	TUint32 IAPID = IAPpref.iBearer.iIapId;
	
	CleanupStack::PopAndDestroy(dataBaseView);
	CleanupStack::PopAndDestroy(communicationDataBase);
	
	
	// Now we have the iap Id. Use it to connect for the connection.
	// Create a connection preference variable.
	TCommDbConnPref connectionDBPref;
	//setup preference
	connectionDBPref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
	connectionDBPref.SetDirection(ECommDbConnectionDirectionUnknown);
	connectionDBPref.SetBearerSet(ECommDbBearerGPRS);
	//set the commDB ID of IAP for this connection;
	connectionDBPref.SetIapId(IAPID);
	
	User::LeaveIfError(iConnection.Start(connectionDBPref));
	
	//set the session connetion info;
	RStringPool stringPool = iSession.StringPool();
	RHTTPConnectionInfo connectinInfo = iSession.ConnectionInfo();
	
	//to use our socket and connection
	connectinInfo.SetPropertyL ( stringPool.StringF(HTTP::EHttpSocketServ,
	        RHTTPSession::GetTable() ), THTTPHdrVal (iSocketServ.Handle()) );
	
	connectinInfo.SetPropertyL ( stringPool.StringF(HTTP::EHttpSocketConnection,
	        RHTTPSession::GetTable() ), 
	        THTTPHdrVal (REINTERPRET_CAST(TInt, &(iConnection))) );

//#ifndef __WINS__
//	
//#endif
//	
//#ifdef __WINS__
//	User::LeaveIfError(iSocketServ.Connect());
//	User::LeaveIfError(iConnection.Open(iSocketServ));
//	
////	CCommsDatabase communicationDatebase = CCommsDatabase::NewL(EDatabaseTypeIAP);
//#endif
	}

void CHttpxxxTools::SetHeaderL(RHTTPHeaders aHeaders, TInt aHdrField,
					const TDesC8& aHdrValue)
{
	RStringF strString = iSession.StringPool().OpenFStringL(aHdrValue);
	//R class used;
	CleanupClosePushL(strString);
	
	THTTPHdrVal httpHeaderValue(strString);
	aHeaders.SetFieldL(iSession.StringPool().StringF(aHdrField, RHTTPSession::GetTable()), httpHeaderValue);
	
	CleanupStack::Pop(1);
}

void CHttpxxxTools::IssueHTTPGetL(const TDesC8& aUri)
	{
		delete iBodyData;
		iBodyData = HBufC8::NewL(409600);
		
		SetupConnectionL();
		
		TUriParser8 UriParser;
		UriParser.Parse(aUri);
		
		RStringF connectMethod = iSession.StringPool().StringF(HTTP::EGET, RHTTPSession::GetTable());
		iTransaction = iSession.OpenTransactionL(UriParser, *this, connectMethod);
		
		RHTTPHeaders header = iTransaction.Request().GetHeaderCollection();
		
		SetHeaderL(header, HTTP::EUserAgent, KUserAgent);
		SetHeaderL(header,HTTP::EAccept, KAccept);
		
		iTransaction.SubmitL();
		iRunning = ETrue;
	}

void CHttpxxxTools::IssueHTTPPostL(const TDesC8& aUri,
		const TDesC8& aContentType,
		const TDesC8& aBody)
	{
		delete iBodyData;
		iBodyData = HBufC8::NewL(409600);
		
		TUriParser8 UriParser;
		UriParser.Parse(aUri);
			
		delete iPostData;
		iPostData = aBody.AllocL();
		
		RStringF connectMethod = iSession.StringPool().StringF(HTTP::EPOST, RHTTPSession::GetTable());
		
		iTransaction = iSession.OpenTransactionL(UriParser, *this, connectMethod);
		
		RHTTPHeaders header = iTransaction.Request().GetHeaderCollection();
				
		SetHeaderL(header, HTTP::EUserAgent, KUserAgent);
		SetHeaderL(header,HTTP::EAccept, KAccept);
		SetHeaderL(header, HTTP::EContentType, aContentType);
		
		MHTTPDataSupplier* dataSupplier = this;
		iTransaction.Request().SetBody(*dataSupplier);
		
		iTransaction.SubmitL();
		iRunning = ETrue;
	}

void CHttpxxxTools::CancelTransaction()
	{
	if(iRunning)
		{
		iTransaction.Close();
		}
	else
		{
		return;
		iRunning = EFalse;
		}
	}

void CHttpxxxTools::MHFRunL(RHTTPTransaction aTransaction,
		  const THTTPEvent& aEvent)
	{
		switch (aEvent.iStatus)
			{
			case THTTPEvent::EGotResponseHeaders:
			break;
			
			case THTTPEvent::EGotResponseBodyData:
				{
				MHTTPDataSupplier* dataSupplier = aTransaction.Response().Body();
				TPtrC8 dataChunk;
//				TBool isLast = dataSupplier->GetNextDataPart(dataChunk);
//				TInt chunkDataLength =dataChunk.Length();
				dataSupplier->GetNextDataPart(dataChunk);
				iBodyData->Des().Append(dataChunk.Ptr(), dataChunk.Length());
				
				dataSupplier->ReleaseData();
				}
				
				break;
			case THTTPEvent::EResponseComplete:
				break;
			case THTTPEvent::ESucceeded:
				{
				aTransaction.Close();
				
				char* data = (char*)iBodyData->Des().PtrZ();
				int dataLenth = iBodyData->Des().Length();
				iRunning = EFalse;
				iObserver.WriteNULL();
				iObserver.WriteDAtaToFile("c:\\Data\\httptest.jpg", data, dataLenth);
				}
				break;
			case THTTPEvent::EFailed:
				{
				aTransaction.Close();
				iRunning = EFalse;
				delete iBodyData;
				}
				break;
			default:
				break;
			}
	}

TInt CHttpxxxTools::MHFRunError(TInt aError,
						RHTTPTransaction aTransaction,
						const THTTPEvent& aEvent)
	{
	delete iBodyData;
	return KErrNone;
	}

TBool CHttpxxxTools::GetNextDataPart(TPtrC8& aDataPart)
	{
		if(iPostData)
			{
			aDataPart.Set(iPostData->Des());
			}
		return ETrue;
	}

void CHttpxxxTools::ReleaseData()
	{
	// It's safe to delete iPostData now
	if(iPostData)
		{
		delete iPostData;
		iPostData = NULL;
		}
	}

TInt CHttpxxxTools::Reset()
	{
	return KErrNone;
	}

TInt CHttpxxxTools::OverallDataSize()
	{
	if(iPostData)
		{
		return iPostData->Length();
		}
	else
		{
		return KErrNotFound;
		}
	}

void CHttpxxxTools::HttpGet(char* uri)
	{
	TPtrC8 ptr((unsigned char*)uri);
	IssueHTTPGetL(ptr);
	}

void CHttpxxxTools::HttpPost(char* uri,char* contenttype, char* body)
	{
	TPtrC8 ptr_uri((unsigned char*)uri);
	TPtrC8 ptr_contenttype((unsigned char*)contenttype);
	TPtrC8 ptr_body((unsigned char*)body);
	IssueHTTPPostL(ptr_uri,ptr_contenttype,ptr_body);
	}

TBool CHttpxxxTools::GetCredentialsL(	const TUriC8& aURI,
							RString aRealm,
							RStringF aAuthenticationType,
							RString& aUsername,
							RString& aPassword)
	{
	
	}

⌨️ 快捷键说明

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