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

📄 httpexampleengine.cpp

📁 symbian手机上记事本的程序
💻 CPP
字号:
/**
*
* @brief Definition of CHTTPExampleEngine
*
* Copyright (c) EMCC Software Ltd 2003
* @version 1.0
*/

// INCLUDE FILES

// Class include
#include "HTTPExampleEngine.h"

// System includes
#include <chttpformencoder.h>		// CHTTPFormEncoder
#include <httpstringconstants.h>	// HTTP string table
#include <rhttpheaders.h>			// RHTTPHeaders

// User includes
#include "NumericEditor.hrh"			// EMaxNameLength

// CONSTANTS
// HTTP header values
_LIT8(KUserAgent, "HTTPExample (1.0)");	// Name of this client app
_LIT8(KAccept, "text/*");				// Accept any (but only) text content
_LIT8(KPostParamName, "FILE");			// Name of the parameter sent in a POST request
_LIT8(KPostContentType, "text/plain");	// Content type sent in a POST request

// URL for POST request.
//_LIT8(KPostUri, "http://cgi.www.emccsoft.com/cgi-bin/www.emccsoft.com/post.pl");
//_LIT8(KPostUri, "http://www.magpiemobile.com/cgi-bin/book_post.cgi");


// ================= MEMBER FUNCTIONS =======================

/**
* 2-phase constructor
*
* @param aObserver An observer of this engine (e.g. the UI)
*/
CHTTPExampleEngine* CHTTPExampleEngine::NewL(MHTTPExampleEngineObserver& aObserver, CSynchToDoDb* aToDoDb)
	{
	CHTTPExampleEngine* self = new (ELeave) CHTTPExampleEngine(aObserver);
	CleanupStack::PushL(self);
	self->ConstructL(aToDoDb);
	CleanupStack::Pop(self);
	return self;
	}

/**
* C++ constructor
*
* @param aObserver An observer of this engine (e.g. the UI)
*/
CHTTPExampleEngine::CHTTPExampleEngine(MHTTPExampleEngineObserver& aObserver)
: iObserver(aObserver)
	{
	}

/**
* 2nd-phase constructor
*/
void CHTTPExampleEngine::ConstructL(CSynchToDoDb* aToDoDb)
	{
	// Open the RHTTPSession
	iSession.OpenL();
	iToDoDb = aToDoDb;
	// Construct the form encoder
	iFormEncoder = CHTTPFormEncoder::NewL();
	//iFormEncoder = CHTTPDataSupplier::NewL();
	//iUri = NULL;
	}

/**
* C++ destructor
*/
CHTTPExampleEngine::~CHTTPExampleEngine()
	{
	// Close session
	//iTransaction.Close();
	iSession.Close();	// Will also close any open transactions
	delete iResponseBuffer;
	delete iFormEncoder;
    delete iUri;
    //delete iToDoDb;
	}

/**
* Override of pure virtual method in MHTTPTransactionCallback.
* Called to report progress by a currently outstanding transaction.
*
* @param aTransaction The transaction reporting progress
* @param aEvent The event being notified
*/
void CHTTPExampleEngine::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
	{
	switch (aEvent.iStatus)
		{
		case THTTPEvent::EGotResponseHeaders:
			{
			// HTTP response headers have been received.
			// Pass status information to observer.
			RHTTPResponse resp = aTransaction.Response();

			// Get status code
			TInt statusCode = resp.StatusCode();//状态码 成功是200 不成功有可能是404,500常见的

			// Get status text 获得状态码后面的文本信息,例如OK ,不成功时弹出什么文本
			RStringF statusStr = resp.StatusText();
			HBufC* statusBuf = HBufC::NewLC(statusStr.DesC().Length());
			statusBuf->Des().Copy(statusStr.DesC());

			// Inform observer
			iObserver.ResponseStatusL(statusCode, *statusBuf);

			CleanupStack::PopAndDestroy(statusBuf);
			}
			break;

		case THTTPEvent::EGotResponseBodyData:
			{
			// Get text of response body
			MHTTPDataSupplier* dataSupplier = aTransaction.Response().Body();
			TPtrC8 ptr;
			dataSupplier->GetNextDataPart(ptr);

			// Convert to 16-bit descriptor
			HBufC8* buf = ptr.AllocLC();//HBufC::NewLC(ptr.Length());
			//buf->Des().Copy(ptr);
			//TPtrC16 ptr16((TUInt16*)ptr.Ptr(), ptr.size()/2);
			//HBufC* buf = ptr16.AllocLC();

			// Append to iResponseBuffer
			if (!iResponseBuffer)
				{
				iResponseBuffer = buf->AllocL();
				}
			else
				{
				iResponseBuffer = iResponseBuffer->ReAllocL(iResponseBuffer->Length() + buf->Length());//设置数据大小
				iResponseBuffer->Des().Append(*buf); //添加
				}

			// Release buf
			CleanupStack::PopAndDestroy(buf);

			// Release the body data
			dataSupplier->ReleaseData();
			}
			break;

		case THTTPEvent::EResponseComplete:
			{
			// Pass the response buffer by reference to the observer
			iObserver.ResponseReceivedL(*iResponseBuffer);//文件上传下载时是不一样的,可以在ui中处理,上传时可能没有数据
			aTransaction.Close();
			if ( !iResponseBuffer )
				{
				delete iResponseBuffer;
				iResponseBuffer = NULL;
				}
			}
			break;
		}
	}

/**
*/
TInt CHTTPExampleEngine::MHFRunError(TInt aError, RHTTPTransaction /*aTransaction*/, const THTTPEvent& /*aEvent*/)
	{
	return KErrNone;
	}


/**
* Initiate a GET request
*
* @param aUri The URI to get
*/
void CHTTPExampleEngine::GetRequestL(const TDesC& aUri)//准备下载
	{
	// Parse the URI
	//ParseUriL(aUri);
	_LIT(KDefaultUrlText, "http://192.168.0.33:8080/servlets-examples/test.html");
	ParseUriL(KDefaultUrlText);

	// Create the transaction
	iTransaction = iSession.OpenTransactionL(iUriParser, *this,
		iSession.StringPool().StringF(HTTP::EGET, RHTTPSession::GetTable()));

	// Set transaction headers
	RHTTPHeaders headers = iTransaction.Request().GetHeaderCollection();
	AddHeaderL(headers, HTTP::EUserAgent, KUserAgent);
	AddHeaderL(headers, HTTP::EAccept, KAccept);

	// Submit the request
	iTransaction.SubmitL();//发起请求
	}


/**
* Initiate a POST request
*
* @param aName The user's name
*/
void CHTTPExampleEngine::PostRequestL(const TDesC8& aName)//上传数据
	{
	
	// Build form encoder
	// Start by removing any previous content
	delete iFormEncoder;
	iFormEncoder = NULL;
	iFormEncoder = CHTTPFormEncoder::NewL();
	//iFormEncoder = CHTTPDataSupplier::NewL();
	//TBuf8<EMaxNameLength> buf8;//网络传输8位
	//_LIT8( KFileContent, "File:a  bc  d" );
	//buf8.Copy( KFileContent );
	iFormEncoder->AddFieldL(KPostParamName, aName);  //这里进行上传数据,aName
	//iFormEncoder->SetData(buf8);

	// Create transaction
	//iUriParser.Parse(KPostUri);
	
	//_LIT(KPostUri, "http://192.168.0.2:8080/servlets-examples/servlet/FileServlet?name=");
	_LIT(KPostUri, "http://192.168.0.33:8080/servlets-examples/servlet/FileServlet");
	//_LIT(KPostUri, "http://221.130.183.213:8080/upload/UploadDate.do");
	//_LIT(KPostUri, "http://192.168.0.240:8080/upload/UploadDate.do");
	//_LIT(KValue, "TESTTEST");
	TBuf<100> buf(KPostUri);
	//buf.Append(KValue);
	ParseUriL(buf); 

	iTransaction = iSession.OpenTransactionL(iUriParser, *this,
		iSession.StringPool().StringF(HTTP::EPOST, RHTTPSession::GetTable()));

	// Set transaction headers
	RHTTPHeaders headers = iTransaction.Request().GetHeaderCollection();
	AddHeaderL(headers, HTTP::EUserAgent, KUserAgent);
	AddHeaderL(headers, HTTP::EAccept, KAccept);
	AddHeaderL(headers, HTTP::EContentType, KPostContentType);

	// Set the form encoder as the data supplier
	iTransaction.Request().SetBody(*iFormEncoder);//付数据给他,才能上传

	// Submit the request
	iTransaction.SubmitL();
	
	}


//
// Utility methods
//

/**
* Parse a URI
*
* @param aUri The URI to be parsed
*/
void CHTTPExampleEngine::ParseUriL(const TDesC& aUri)
	{
	// Convert the URI to an 8-bit descriptor
	// then set iUriParser to point at it
	delete iUri;
	iUri = NULL;
	iUri = HBufC8::NewL(aUri.Length());
	iUri->Des().Copy(aUri);
	User::LeaveIfError(iUriParser.Parse(*iUri));
	}


/**
* Add a header to a header set
*
* @param aHeaders The header set to be modified
* @param aHeaderField The name of the header to be added
* @param aHeaderValue The value for the header to be added
*/
void CHTTPExampleEngine::AddHeaderL(RHTTPHeaders aHeaders, TInt aHeaderField, const TDesC8& aHeaderValue)
	{
	RStringPool stringPool = iSession.StringPool();
	RStringF valStr = stringPool.OpenFStringL(aHeaderValue);
	THTTPHdrVal headerVal(valStr);
	aHeaders.SetFieldL(stringPool.StringF(aHeaderField, RHTTPSession::GetTable()), headerVal);
	valStr.Close();
	}

/**
* Cancel any outstanding transaction
*/
void CHTTPExampleEngine::Cancel()
	{
	iTransaction.Cancel();
	}

CHTTPDataSupplier* CHTTPDataSupplier::NewL()
{
    CHTTPDataSupplier* self = new (ELeave) CHTTPDataSupplier;
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop(self);
    return self;
}

CHTTPDataSupplier::CHTTPDataSupplier()
{

}

void CHTTPDataSupplier::ConstructL()
{
}

CHTTPDataSupplier::~CHTTPDataSupplier()
{
	if(iData)
		delete iData;
}

TBool CHTTPDataSupplier::GetNextDataPart(TPtrC8& aDataPart)
{
	if(iData && iData->Length())
		aDataPart.Set(iData->Des());

	return ETrue;

}

void CHTTPDataSupplier::ReleaseData()
{

}

TInt CHTTPDataSupplier::OverallDataSize()
{
	if (iData)
		return iData->Length();
	return 0;
}

TInt CHTTPDataSupplier::Reset()
{
	return 0;
}

TInt CHTTPDataSupplier::SetData(const TDesC8& aData)
{
	if(iData)
	{
		delete iData;
		iData = NULL;
	}	

	iData = aData.AllocL();
	return 0;
}



⌨️ 快捷键说明

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