// ----------------------------------------------------------------------------
// CHTTPEngine::IssueHTTPGetL()
//
// Start a new HTTP GET transaction.
// ----------------------------------------------------------------------------
void CHTTPEngine::IssueHTTPGetL(const TDesC8& aUri)
{
SetupConnectionL();
// Parse string to URI (as defined in RFC2396)
TUriParser8 uri;
uri.Parse(aUri);
// Get request method string for HTTP GET
RStringF method = iSession.StringPool().StringF(HTTP::EGET,
RHTTPSession::GetTable());
// Open transaction with previous method and parsed uri. This class will
// receive transaction events in MHFRunL and MHFRunError.
iTransaction = iSession.OpenTransactionL(uri, *this, method);
// Set headers for request; user agent and accepted content type
RHTTPHeaders hdr = iTransaction.Request().GetHeaderCollection();
SetHeaderL(hdr, HTTP::EUserAgent, KUserAgent);
SetHeaderL(hdr, HTTP::EAccept, KAccept);
// Submit the transaction. After this the framework will give transaction
// events via MHFRunL and MHFRunError.
iTransaction.SubmitL();
iRunning = ETrue;
_LIT(KConnecting,"Connecting...");
iObserver.ClientEvent(KConnecting);
}
// ----------------------------------------------------------------------------
// CHTTPEngine::SetupConnectionL()
//
// The method set the internet access point and connection setups.
// ----------------------------------------------------------------------------
void CHTTPEngine::SetupConnectionL()
{
if( iConnectionSetupDone )
return;
iConnectionSetupDone = ETrue;
//open socket server and start the connection
User::LeaveIfError(iSocketServ.Connect());
User::LeaveIfError(iConnection.Open(iSocketServ));
// open the IAP communications database
CCommsDatabase* commDB = CCommsDatabase::NewL(EDatabaseTypeIAP);
CleanupStack::PushL(commDB);
// initialize a view
CCommsDbConnectionPrefTableView* commDBView =
commDB->OpenConnectionPrefTableInRankOrderLC(ECommDbConnectionDirectionUnknown);
// go to the first record
User::LeaveIfError(commDBView->GotoFirstRecord());
// Declare a prefTableView Object.
CCommsDbConnectionPrefTableView::TCommDbIapConnectionPref pref;
// read the connection preferences
commDBView->ReadConnectionPreferenceL(pref);
TUint32 iapID = pref.iBearer.iIapId;
// pop and destroy the IAP View
CleanupStack::PopAndDestroy(commDBView);
// pop and destroy the database object
CleanupStack::PopAndDestroy(commDB);
// Now we have the iap Id. Use it to connect for the connection.
// Create a connection preference variable.
TCommDbConnPref connectPref;
// setup preferences
connectPref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
connectPref.SetDirection(ECommDbConnectionDirectionUnknown);
connectPref.SetBearerSet(ECommDbBearerGPRS);
//Sets the CommDb ID of the IAP to use for this connection
connectPref.SetIapId(iapID);
//set the sessions connection info
RStringPool strPool = iSession.StringPool();
RHTTPConnectionInfo connInfo = iSession.ConnectionInfo();
//to use our socket server and connection
connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketServ,
RHTTPSession::GetTable() ), THTTPHdrVal (iSocketServ.Handle()) );
// System includes
#include <chttpformencoder.h> // CHTTPFormEncoder
#include <httpstringconstants.h> // HTTP string table
#include <rhttpheaders.h> // RHTTPHeaders
// User includes
#include "HTTPExample.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, "NAME"); // 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)
{
CHTTPExampleEngine* self = new (ELeave) CHTTPExampleEngine(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
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()
{
// Open the RHTTPSession
iSession.OpenL();
// Construct the form encoder
iFormEncoder = CHTTPFormEncoder::NewL();
}
/**
* C++ destructor
*/
CHTTPExampleEngine::~CHTTPExampleEngine()
{
// Close session
iSession.Close(); // Will also close any open transactions
delete iResponseBuffer;
delete iFormEncoder;
delete iUri;
}
/**
* 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();
// Get status text
RStringF statusStr = resp.StatusText();
HBufC* statusBuf = HBufC::NewLC(statusStr.DesC().Length());
statusBuf->Des().Copy(statusStr.DesC());
case THTTPEvent::EGotResponseBodyData:
{
// Get text of response body
MHTTPDataSupplier* dataSupplier = aTransaction.Response().Body();
TPtrC8 ptr;
dataSupplier->GetNextDataPart(ptr);
// Convert to 16-bit descriptor
HBufC* buf = HBufC::NewLC(ptr.Length());
buf->Des().Copy(ptr);
// 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);
}
break;
}
}
/**
* Initiate a GET request
*
* @param aUri The URI to get
*/
void CHTTPExampleEngine::GetRequestL(const TDesC& aUri)
{
// Parse the URI
ParseUriL(aUri);
// Create the transaction
iTransaction = iSession.OpenTransactionL(iUriParser, *this,
iSession.StringPool().StringF(HTTP::EGET, RHTTPSession::GetTable()));
// 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();
}