📄 webclientengine.cpp
字号:
// 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;
// Load a string from the resource file
HBufC* textResource = StringLoader::LoadLC( R_WEBCLIENT_CONNECTING );
iObserver.ClientEvent( *textResource );
CleanupStack::PopAndDestroy( textResource );
}
// ----------------------------------------------------------------------------
// CWebClientEngine::CancelTransactionL()
// Cancels currently running transaction and frees resources related to it.
// ----------------------------------------------------------------------------
//
void CWebClientEngine::CancelTransactionL()
{
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();
// Not running anymore
iRunning = EFalse;
// Load a string from the resource file
HBufC* textResource = StringLoader::LoadLC( R_WEBCLIENT_TRANSACTION_CANCEL );
iObserver.ClientEvent( *textResource );
CleanupStack::PopAndDestroy( textResource );
}
// ----------------------------------------------------------------------------
// CWebClientEngine::SetCallBack()
// Set pointer to CApplicationUi instance
// ----------------------------------------------------------------------------
//
void CWebClientEngine::SetCallBack( CWebClientAppUi* aCallBack )
{
iApplicationUi = aCallBack;
}
// ----------------------------------------------------------------------------
// CWebClientEngine::MHFRunL()
// Inherited from MHTTPTransactionCallback
// Called by framework to pass transaction events.
// ----------------------------------------------------------------------------
//
void CWebClientEngine::MHFRunL( RHTTPTransaction aTransaction,
const THTTPEvent& aEvent )
{
switch ( aEvent.iStatus )
{
case THTTPEvent::EGotResponseHeaders:
{
// HTTP response headers have been received. Use
// aTransaction.Response() to get the response. However, it's not
// necessary to do anything with the response when this event occurs.
// Get HTTP status code from header (e.g. 200)
RHTTPResponse resp = aTransaction.Response();
TInt status = resp.StatusCode();
// Get status text (e.g. "OK")
TBuf<KMaxStatusTextLength> statusText;
statusText.Copy( resp.StatusText().DesC() );
// Load a string from the resource file and add status and status text
HBufC* textResource = StringLoader::LoadLC( R_WEBCLIENT_HEADER_RECEIVED,
statusText, status );
iObserver.ClientEvent( *textResource );
CleanupStack::PopAndDestroy( textResource );
// Display header field names and value
DumpRespHeadersL( aTransaction );
}
break;
case THTTPEvent::EGotResponseBodyData:
{
// Part (or all) of response's body data received. Use
// aTransaction.Response().Body()->GetNextDataPart() to get the actual
// body data.
// Get the body data supplier
MHTTPDataSupplier* body = aTransaction.Response().Body();
TPtrC8 dataChunk;
// GetNextDataPart() returns ETrue, if the received part is the last
// one.
TBool isLast = body->GetNextDataPart( dataChunk );
iObserver.ClientBodyReceived( dataChunk );
// Load a string from the resource file and add data length to string
HBufC* textResource = StringLoader::LoadLC( R_WEBCLIENT_BODY_PART_RECEIVED,
dataChunk.Length() );
iObserver.ClientEvent( *textResource );
CleanupStack::PopAndDestroy( textResource );
// NOTE: isLast may not be ETrue even if last data part received.
// (e.g. multipart response without content length field)
// Use EResponseComplete to reliably determine when body is completely
// received.
if (isLast)
{
HBufC* textResource = StringLoader::LoadLC( R_WEBCLIENT_BODY_RECEIVED );
iObserver.ClientEvent( *textResource );
CleanupStack::PopAndDestroy( textResource );
}
// Always remember to release the body data.
body->ReleaseData();
}
break;
case THTTPEvent::EResponseComplete:
{
// Indicates that header & body of response is completely received.
// No further action here needed.
HBufC* textResource = StringLoader::LoadLC( R_WEBCLIENT_TRANSACTION_COMPLETE );
iObserver.ClientEvent( *textResource );
CleanupStack::PopAndDestroy( textResource );
}
break;
case THTTPEvent::ESucceeded:
{
// Indicates that transaction succeeded.
HBufC* textResource = StringLoader::LoadLC( R_WEBCLIENT_TRANSACTION_SUCCEED );
iObserver.ClientEvent( *textResource );
CleanupStack::PopAndDestroy( textResource );
if ( iApplicationUi )
iApplicationUi->RemoveWaitDialogL();
// Transaction can be closed now. It's not needed anymore.
aTransaction.Close();
iRunning = EFalse;
}
break;
case THTTPEvent::EFailed:
{
// Transaction completed with failure.
HBufC* textResource = StringLoader::LoadLC( R_WEBCLIENT_TRANSACTION_FAILED );
iObserver.ClientEvent( *textResource );
CleanupStack::PopAndDestroy( textResource );
if ( iApplicationUi )
iApplicationUi->RemoveWaitDialogL();
aTransaction.Close();
iRunning = EFalse;
}
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.
{
HBufC* textResource;
if ( aEvent.iStatus < 0 )
{
// Load a string from the resource file and add status to string
textResource = StringLoader::LoadLC( R_WEBCLIENT_TRANSACTION_ERROR,
aEvent.iStatus );
if ( iApplicationUi )
iApplicationUi->RemoveWaitDialogL();
// Just close the transaction on errors
aTransaction.Close();
iRunning = EFalse;
} else {
// Other events are not errors (e.g. permanent and temporary redirections)
textResource = StringLoader::LoadLC( R_WEBCLIENT_UNRECOGNISED_EVENT,
aEvent.iStatus );
}
iObserver.ClientEvent( *textResource );
CleanupStack::PopAndDestroy( textResource );
}
break;
}
}
// ----------------------------------------------------------------------------
// CWebClientEngine::MHFRunError()
// Inherited from MHTTPTransactionCallback
// Called by framework when *leave* occurs in handling of transaction event.
// These errors must be handled, or otherwise HTTP-CORE 6 panic is thrown.
// ----------------------------------------------------------------------------
//
TInt CWebClientEngine::MHFRunError( TInt aError,
RHTTPTransaction /*aTransaction*/,
const THTTPEvent& /*aEvent*/)
{
// Handle error and return KErrNone.
TRAPD( err, HandleRunErrorL( aError ) );
if( err )
Panic( EClientEngine );
return KErrNone;
}
// ----------------------------------------------------------------------------
// CWebClientEngine::GetCredentialsL()
//
// Inherited from MHTTPAuthenticationCallback
// Called by framework when we requested authenticated page and framework
// needs to know username and password.
// ----------------------------------------------------------------------------
TBool CWebClientEngine::GetCredentialsL( const TUriC8& /*aUri*/,
RString aRealm,
RStringF /*aAuthenticationType*/,
RString& aUsername,
RString& aPassword)
{
// aURI, aReal and aAuthenticationType are informational only. We only need
// to set aUsername and aPassword and return ETrue, if aUsername and
// aPassword are provided by user.
// Query user name and password
TBuf<KMaxUserNameLength> userName;
TBuf<KMaxPasswordLength> password;
CAknMultiLineDataQueryDialog* dlg =
CAknMultiLineDataQueryDialog::NewL( userName, password );
if ( !dlg->ExecuteLD( R_WEBCLIENT_DIALOG_USER_PASSWORD_QUERY ))
return EFalse; // No credentials given; must return EFalse
// Set aUsername and aPassword
TBuf8<KMaxUserNameLength> temp;
temp.Copy( userName );
TRAPD( err, aUsername = aRealm.Pool().OpenStringL( temp ));
if ( !err )
{
temp.Copy( password );
TRAP( err, aPassword = aRealm.Pool().OpenStringL( temp ));
if ( !err ) return ETrue;
}
// Return ETrue if user has given credentials (username and password),
// otherwise EFlase
return EFalse;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -