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

📄 webclientengine.cpp

📁 介绍一个简单的WEb应用你可以此功能 的基础上做更多的操作功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
* ==============================================================================
*  Name        : WebClientEngine.cpp
*  Part of     : WebClient
*  Interface   : 
*  Description : 
*  Version     : 
*
*  Copyright (c) 2005-2006 Nokia Corporation.
*  This material, including documentation and any related 
*  computer programs, is protected by copyright controlled by 
*  Nokia Corporation.
* ==============================================================================
*/

// INCLUDE FILES
#include <avkon.hrh>
#include <aknnotewrappers.h>
#include <stringloader.h>
#include <http.h>
#include <WebClient.rsg>
#include "WebClient.pan"
#include "WebClient.hrh"
#include "WebClientEngine.h"
#include "WebClientAppUi.h"

// ----------------------------------------------------------------------------
// CWebClientEngine::NewL()
// Creates instance of CWebClientEngine.
// ----------------------------------------------------------------------------
//
CWebClientEngine* CWebClientEngine::NewL( MWebClientObserver& aObserver )
    {
    CWebClientEngine* self = CWebClientEngine::NewLC( aObserver );
    CleanupStack::Pop( self );
    return self;
    }


// ----------------------------------------------------------------------------
// CWebClientEngine::NewLC()
// Creates instance of CWebClientEngine.
// ----------------------------------------------------------------------------
//
CWebClientEngine* CWebClientEngine::NewLC( MWebClientObserver& aObserver )
    {
    CWebClientEngine* self = new (ELeave) CWebClientEngine( aObserver );
    CleanupStack::PushL( self );
    self->ConstructL();
    return self;
    }


// ----------------------------------------------------------------------------
// CWebClientEngine::CWebClientEngine()
// First phase constructor.
// ----------------------------------------------------------------------------
//
CWebClientEngine::CWebClientEngine( MWebClientObserver& aObserver )
:    iObserver( aObserver ),
     iRunning( EFalse ),
     iApplicationUi( NULL )
    {
    // no implementation required
    }


// ----------------------------------------------------------------------------
// CWebClientEngine::~CWebClientEngine()
// Destructor.
// ----------------------------------------------------------------------------
//
CWebClientEngine::~CWebClientEngine()
    {
    iSession.Close();
    iApplicationUi = NULL;
    }


// ----------------------------------------------------------------------------
// CWebClientEngine::ConstructL()
// Second phase construction.
// ----------------------------------------------------------------------------
//
void CWebClientEngine::ConstructL()
    {
    // Open RHTTPSession with default protocol ("HTTP/TCP")
    TRAPD( err, iSession.OpenL() );
    if( err != KErrNone ) {
        // Most common error; no access point configured, and session creation
        // leaves with KErrNotFound.
        // Load a string from the resource file and add the error code to string
        HBufC* textResource = StringLoader::LoadLC( R_WEBCLIENT_IAP_CONF_ERR, err );
        CAknErrorNote* errorNote;
        errorNote = new (ELeave) CAknErrorNote;
        // Show the error Note with textResource loaded with StringLoader.
        errorNote->ExecuteLD( *textResource);
        // Pop HBuf from CleanUpStack and Destroy it.
        CleanupStack::PopAndDestroy( textResource );
        User::Leave( err );
    }

    // Install this class as the callback for authentication requests. When 
    // page requires authentication the framework calls GetCredentialsL to get 
    // user name and password.
    InstallAuthenticationL( iSession );
    }


// ----------------------------------------------------------------------------
// CWebClientEngine::SetHeaderL()
// Used to set header value to HTTP request
// ----------------------------------------------------------------------------
//
void CWebClientEngine::SetHeaderL( RHTTPHeaders aHeaders, 
                                   TInt aHdrField, 
                                   const TDesC8& aHdrValue )
    {
    RStringF valStr = iSession.StringPool().OpenFStringL( aHdrValue );
    CleanupClosePushL( valStr );
    THTTPHdrVal val( valStr );
    aHeaders.SetFieldL( iSession.StringPool().StringF( aHdrField,
        RHTTPSession::GetTable() ), val );
    CleanupStack::PopAndDestroy( &valStr );
    }

// ----------------------------------------------------------------------------
// CWebClientEngine::DumpRespHeadersL(RHTTPTransaction& aTransaction)
// Used to display HTTP header field names and values
// ----------------------------------------------------------------------------
//
void CWebClientEngine::DumpRespHeadersL( RHTTPTransaction& aTransaction )
    {
    RHTTPResponse resp = aTransaction.Response();
    RStringPool strP = aTransaction.Session().StringPool();
    RHTTPHeaders hdr = resp.GetHeaderCollection();
    THTTPHdrFieldIter it = hdr.Fields();

    HBufC* headerField = HBufC::NewLC( KMaxHeaderNameLength + KMaxHeaderValueLength );
    HBufC* fieldValBuf = HBufC::NewLC( KMaxHeaderValueLength );

    while ( it.AtEnd() == EFalse )
        {
        RStringTokenF fieldName = it();
        RStringF fieldNameStr = strP.StringF( fieldName );
        THTTPHdrVal fieldVal;
        if ( hdr.GetField( fieldNameStr, 0, fieldVal ) == KErrNone )
            {
            const TDesC8& fieldNameDesC = fieldNameStr.DesC();
            headerField->Des().Copy( fieldNameDesC.Left( KMaxHeaderNameLength ));
            fieldValBuf->Des().Zero();
            switch ( fieldVal.Type() )
                {
            // the value is an integer
            case THTTPHdrVal::KTIntVal:
                fieldValBuf->Des().Num( fieldVal.Int() );
                break;
            // the value is a case-insensitive string
            case THTTPHdrVal::KStrFVal:
                {
                RStringF fieldValStr = strP.StringF( fieldVal.StrF() );
                const TDesC8& fieldValDesC = fieldValStr.DesC();
                fieldValBuf->Des().Copy( fieldValDesC.Left(KMaxHeaderValueLength ));
                }
                break;
            // the value is a case-sensitive string
            case THTTPHdrVal::KStrVal:
                {
                RString fieldValStr = strP.String( fieldVal.Str() );
                const TDesC8& fieldValDesC = fieldValStr.DesC();
                fieldValBuf->Des().Copy( fieldValDesC.Left(KMaxHeaderValueLength) );
                }
                break;
            // the value is a date/time
            case THTTPHdrVal::KDateVal:
                {
                TDateTime date = fieldVal.DateTime();
                TBuf<KMaxDateTimeStringLength> dateTimeString;
                TTime t( date );
                t.FormatL( dateTimeString,KDateFormat );
                fieldValBuf->Des().Copy( dateTimeString );
                } 
                break;
            // the value is type is unknown
            default:
                break;
                }

            // Display HTTP header field name and value
            headerField->Des().Append( KColon );
            headerField->Des().Append( *fieldValBuf );
            iObserver.ClientHeaderReceived( *headerField );
            
            // Display realm for WWW-Authenticate header
            RStringF wwwAuth = strP.StringF( HTTP::EWWWAuthenticate,RHTTPSession::GetTable() );
            if ( fieldNameStr == wwwAuth )
                {
                // check the auth scheme is 'basic'
                RStringF basic = strP.StringF( HTTP::EBasic,RHTTPSession::GetTable() );
                RStringF realm = strP.StringF( HTTP::ERealm,RHTTPSession::GetTable() );
                THTTPHdrVal realmVal;
                if (( fieldVal.StrF() == basic ) && 
                    ( !hdr.GetParam( wwwAuth, realm, realmVal )))
                    {
                    RStringF realmValStr = strP.StringF( realmVal.StrF() );
                    fieldValBuf->Des().Copy( realmValStr.DesC() );
                    headerField->Des().Copy( Krealm );
                    headerField->Des().Append( *fieldValBuf );
                    iObserver.ClientHeaderReceived( *headerField );
                    }
                }
            }
        ++it;
        }
        CleanupStack::PopAndDestroy( fieldValBuf );
        CleanupStack::PopAndDestroy( headerField );
    }

// ----------------------------------------------------------------------------
// CWebClientEngine::HandleRunErrorL()
// Called from MHFRunError() when *leave* occurs in handling of transaction event.
// ----------------------------------------------------------------------------
//
void CWebClientEngine::HandleRunErrorL( TInt aError )
    {
    // Load a string from the resource file and add the leave code to string
    HBufC* textResource = StringLoader::LoadL( R_WEBCLIENT_MHFRUN_ERROR, aError );
    // Notify about the error
    iObserver.ClientEvent( *textResource );
    CleanupStack ::PopAndDestroy( textResource );
    if ( iApplicationUi )
        iApplicationUi->RemoveWaitDialogL();
    }

// ----------------------------------------------------------------------------
// CWebClientEngine::IssueHTTPGetL()
// Start a new HTTP GET transaction.
// ----------------------------------------------------------------------------
//
void CWebClientEngine::IssueHTTPGetL( const TDesC8& aUri )
    {
    // 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 );

⌨️ 快捷键说明

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