×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    The application crashes when I tried to download again.

    Hi, Experts,
    I used following code to download a file from the server, I can download the file successfully first time, but failed when request for second time, any body can help to point where the problem is? Thanks very much.
    ======================================================================================
    #include "WebClientEngine.h"

    #include <avkon.hrh>
    #include <aknnotewrappers.h>
    #include <stringloader.h>
    #include <http.h>
    #include <e32const.h>
    #include <es_enum.h>
    #include <commdbconnpref.h>
    #include <commdb.h>
    #include "FileLogger.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 ),
    //iWebClientRequest( NULL ),
    CActive( EPriorityStandard )
    {
    }

    // ----------------------------------------------------------------------------
    // CWebClientEngine::~CWebClientEngine()
    // Destructor.
    // ----------------------------------------------------------------------------
    //
    CWebClientEngine::~CWebClientEngine()
    {
    }


    // ----------------------------------------------------------------------------
    // CWebClientEngine::ConstructL()
    // Second phase construction.
    // ----------------------------------------------------------------------------
    //
    void CWebClientEngine::ConstructL()
    {

    }

    // ----------------------------------------------------------------------------
    // 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:umpRespHeadersL(RHTTPTransaction& aTransaction)
    // Used to display HTTP header field names and values
    // ----------------------------------------------------------------------------
    //
    void CWebClientEngine:umpRespHeadersL( 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 );

    CFileLogger::init();
    CFileLogger::write("IssueHttp dup resp ");
    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() );
    iCurrentFileLength =fieldVal.Int();
    break;
    // the value is a case-insensitive string
    case THTTPHdrVal::KStrFVal:
    {
    CFileLogger::write("IssueHttp dup resp KStrFVal ");
    //RStringF fieldValStr = strP.StringF( fieldVal.StrF() );
    //const TDesC8& fieldValDesC = fieldValStr.DesC();
    //fieldValBuf->Des().Copy( fieldValDesC.Left(KMaxHeaderValueLength ));
    //if(headerField->Des().Match(_L("OlympusFileName")) == 0)
    //{
    // iCurrentFileName->Des().Append(fieldValBuf->Des());
    //}
    }
    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.
    // ----------------------------------------------------------------------------
    //

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Re: The application crashes when I tried to download again.

    TInt CWebClientEngine::GetAPNID(const TDesC& aApn)
    {
    __UHEAP_MARK;
    CCommsDatabase* db = CCommsDatabase::NewL(EDatabaseTypeIAP);
    CleanupStack::PushL(db);

    //get apn's name
    TBuf<32> name;
    TUint32 id = 0;
    {
    CCommsDbTableView* view = db->OpenTableLC(TPtrC(OUTGOING_WCDMA));
    TInt err = view->GotoFirstRecord();
    while (err == KErrNone)
    {
    TBuf<32> apn;

    view->ReadUintL(TPtrC(COMMDB_ID), id);
    view->ReadTextL(TPtrC(COMMDB_NAME), name);
    view->ReadTextL( TPtrC(GPRS_APN), apn );

    ///LOG_FORMAT((_L("id = %d name = %S apn = %S"), id, &name, &apn));
    apn.LowerCase();
    if(apn.Compare(aApn) == 0)
    {
    //LOG_FORMAT((_L("Finded APN id = %d, name = %S"), id, &name));
    break;
    }
    err = view->GotoNextRecord();
    }
    if ( err != KErrNone ) {
    CleanupStack::PopAndDestroy(view);
    CleanupStack::PopAndDestroy(db);
    User::Leave(KErrNotFound);
    }
    CleanupStack::PopAndDestroy(view);
    }

    {
    CCommsDbTableView* view = db->OpenTableLC(TPtrC(IAP));
    TInt err = view->GotoFirstRecord();
    while (err == KErrNone)
    {
    TBuf<32> name2;

    view->ReadUintL(TPtrC(COMMDB_ID), id);
    view->ReadTextL(TPtrC(COMMDB_NAME), name2);

    //LOG_FORMAT((_L("id = %d name = %S "), id, &name2));
    if( !name.Compare(name2) ) {
    //LOG_FORMAT((_L("Finded APN id = %d, name = %S"), id, &name));
    break;
    }
    err = view->GotoNextRecord();
    }
    CleanupStack::PopAndDestroy(view);
    }

    CleanupStack::PopAndDestroy(db);
    __UHEAP_MARKEND;
    return id;
    }


    void CWebClientEngine::IssueHTTPGetL( const TDesC8& aUri )
    {
    CFileLogger::init();
    CFileLogger::write("IssueHttp Get ");
    TRAPD( err, iSession.OpenL() );
    if( err != KErrNone ) {//err
    CAknErrorNote* errorNote;
    errorNote = new (ELeave) CAknErrorNote;
    User::Leave( err );
    }

    CFileLogger::write("Set filter");

    RStringPool pool = iSession.StringPool();
    iSession.FilterCollection().RemoveFilter( pool.StringF( HTTP::ERedirect, RHTTPSession::GetTable() ) );

    CFileLogger::write("IssueHttp Get 1");


    User::LeaveIfError(iSockServ.Connect());
    User::LeaveIfError(iConnection.Open(iSockServ));


    TCommDbConnPref pref;
    pref.SetIapId(GetAPNID(_L("cmnet"))); // IAP ID for connection to be used
    pref.SetDialogPreference( ECommDbDialogPrefDoNotPrompt );
    pref.SetDirection( ECommDbConnectionDirectionOutgoing );
    pref.SetBearerSet( ECommDbBearerGPRS | ECommDbBearerCdma2000
    | ECommDbBearerLAN | ECommDbBearerCSD );
    CFileLogger::write("IssueHttp Get 2");
    iConnection.Start(pref);
    CFileLogger::write("IssueHttp Get 3");

    //_LIT(KMyDir,"c:\\");
    //iCurrentFileName->Des().Copy("c:\\");
    CFileLogger::write("IssueHttp Get 4");
    bWritingFileDone = true;
    // Parse string to URI (as defined in RFC2396)
    TUriParser8 uri;
    uri.Parse( aUri );
    // Get request method string for HTTP GET
    //CFileLogger::write("IssueHttp Get 5");
    /*
    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 );
    iTransaction.SubmitL();
    */


    RHTTPConnectionInfo connInfo = iSession.ConnectionInfo();
    RStringPool poolPro = iSession.StringPool();
    CFileLogger::write("IssueHttp Get 6");
    // Attach to socket server
    connInfo.SetPropertyL(poolPro.StringF(HTTP::EHttpSocketServ, RHTTPSession::GetTable()), THTTPHdrVal(iSockServ.Handle()));

    // Attach to connection
    TInt connPtr = REINTERPRET_CAST(TInt, &iConnection);
    connInfo.SetPropertyL(poolPro.StringF(HTTP::EHttpSocketConnection, RHTTPSession::GetTable()), THTTPHdrVal(connPtr));

    // Open transaction...
    iTransaction = iSession.OpenTransactionL(uri, *this, poolPro.StringF(HTTP::EGET, RHTTPSession::GetTable()));
    RHTTPHeaders hdr = iTransaction.Request().GetHeaderCollection();


    CFileLogger::write("IssueHttp Get 7");
    iTransaction.SubmitL();
    CFileLogger::write("IssueHttp Get 8");
    /*
    RStringPool strP = iSession.StringPool();
    RHTTPConnectionInfo connInfo = iSession.ConnectionInfo();
    connInfo.SetPropertyL ( strP.StringF(HTTP::EGET,
    RHTTPSession::GetTable() ), THTTPHdrVal (socketServ.Handle()) );
    /*TInt connPtr = REINTERPRET_CAST(TInt, &connection);
    connInfo.SetPropertyL ( strP.StringF(HTTP::EHttpSocketConnection,
    RHTTPSession::GetTable() ), THTTPHdrVal (connPtr) );
    CFileLogger::write(_L("CWebClientEngine::IssueHTTPGetL()2..."));
    // Submit the transaction. After this the framework will give transaction
    // events via MHFRunL and MHFRunError.
    iTransaction = iSession.OpenTransactionL( uri, *this, method );
    iTransaction.SubmitL();*/
    /*
    RHTTPConnectionInfo connInfo = iSession.ConnectionInfo();
    RStringPool pool = iSession.StringPool();

    // Attach to socket server
    connInfo.SetPropertyL(pool.StringF(HTTP::EHttpSocketServ, RHTTPSession::GetTable()), THTTPHdrVal(socketServ.Handle()));

    // Attach to connection
    TInt connPtr = REINTERPRET_CAST(TInt, &iConnection);
    connInfo.SetPropertyL(pool.StringF(HTTP::EHttpSocketConnection, RHTTPSession::GetTable()), THTTPHdrVal(connPtr));

    // Open transaction...
    iTransaction = iSession.OpenTransactionL(uri, *this, pool.StringF(HTTP::EGET, RHTTPSession::GetTable()));
    RHTTPHeaders hdr = iTransaction.Request().GetHeaderCollection();
    */
    iRunning = ETrue;

    }
    // ----------------------------------------------------------------------------
    // CWebClientEngine::CancelTransactionL()
    // Cancels currently running transaction and frees resources related to it.
    // ----------------------------------------------------------------------------
    //

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Re: The application crashes when I tried to download again.

    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;
    }

    // ----------------------------------------------------------------------------
    // CWebClientEngine::SetCallBack()
    // Set pointer to CApplicationUi instance
    // ----------------------------------------------------------------------------
    //

    //void CWebClientEngine::SetCallBack( CWebClientRequest* aCallBack )
    // {
    // iWebClientRequest = aCallBack;
    // }

    // ----------------------------------------------------------------------------
    // CWebClientEngine::MHFRunL()
    // Inherited from MHTTPTransactionCallback
    // Called by framework to pass transaction events.
    // ----------------------------------------------------------------------------
    //
    void CWebClientEngine::MHFRunL( RHTTPTransaction aTransaction,
    const THTTPEvent& aEvent )
    {
    //CFileLogger::init();
    //CFileLogger::write("MHFRunL ");
    switch ( aEvent.iStatus )
    {
    case THTTPEvent::EGotResponseHeaders:
    {
    CFileLogger::write("MHFRunL EGotResponseHeaders");
    // 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() );
    RStringF location = iSession.StringPool().StringF(HTTP::ELocation,RHTTPSession::GetTable());
    RHTTPHeaders responseHeaders( aTransaction.Response().GetHeaderCollection() );
    THTTPHdrVal locationValue;
    if( responseHeaders.GetField(location, 0, locationValue) == KErrNone )
    {
    TBuf8<255> urides( locationValue.StrF().DesC() );
    CFileLogger::write("Get redirect url");

    TUriParser8 uri;
    uri.Parse( urides );
    aTransaction.Cancel();
    iTransaction.Request().SetURIL( uri );
    iTransaction.SubmitL();
    CFileLogger::write("Start Get file name");
    HBufC* file = uri.GetFileNameL(EUriFileNameFull);
    iCurrentFileName.Append(file->Mid(file->Length()-4));
    CFileLogger::write(iCurrentFileName);

    }
    //Retrieved from "http://wiki.forum.nokia.com/index.php/How_to_resolve_302_Redirect_Status"

    // Display header field names and value
    DumpRespHeadersL( aTransaction );
    }
    break;

    case THTTPEvent::EGotResponseBodyData:
    {
    // Get the body data supplier
    CFileLogger::write("MHFRunL EGotResponseBodyData");
    MHTTPDataSupplier* body = aTransaction.Response().Body();
    TPtrC8 dataChunk;

    // GetNextDataPart() returns ETrue, if the received part is the last
    // one.
    TBool isLast = body->GetNextDataPart( dataChunk );
    RFs fsSession;
    User::LeaveIfError(fsSession.Connect());

    RFile myFile;

    if(bWritingFileDone == true)//if not current ,delete old
    fsSession.Delete(iCurrentFileName);

    TInt err = myFile.Open(fsSession,iCurrentFileName,EFileShareExclusive|EFileWrite|EFileStream);
    if (err!= KErrNone) // file does not exist - create it
    {
    err=myFile.Create(fsSession,iCurrentFileName,EFileShareExclusive|EFileWrite|EFileStream);
    bWritingFileDone = false;
    }

    // Find the end of the file
    TInt pos = 0;
    err = myFile.Seek( ESeekEnd , pos );
    if( err == KErrNone ) {
    myFile.Write(pos,dataChunk);
    }

    myFile.Close();
    fsSession.Close();

    //iObserver.ClientBodyReceived( dataChunk );
    // Always remember to release the body data.
    body->ReleaseData();
    }
    break;

    case THTTPEvent::EResponseComplete:
    {
    CFileLogger::write("MHFRunL EResponseComplete");
    // Indicates that header & body of response is completely received.
    // No further action here needed.
    }
    break;

    case THTTPEvent::ESucceeded:
    {
    iObserver.ReceivedComplete(iCurrentFileName);

    CFileLogger::write("MHFRunL ESucceeded");
    // Transaction can be closed now. It's not needed anymore.
    aTransaction.Close();
    iRunning = EFalse;
    }
    break;

    case THTTPEvent::EFailed:
    {
    CFileLogger::write("MHFRunL EFailed");
    aTransaction.Close();
    iRunning = EFalse;
    }
    break;
    case THTTPEvent::ERedirectedTemporarily:
    {
    CFileLogger::write("MHFRunL ERedirectedTemporarily");
    }
    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.
    {
    CFileLogger::write("MHFRunL default");
    //HBufC* textResource;
    //if ( aEvent.iStatus < 0 )
    // {
    // aTransaction.Close();
    // iRunning = EFalse;
    // } else {
    // }
    //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;
    }

    void CWebClientEngine:oCancel()
    {

    }


    void CWebClientEngine::RunL()
    {

    }

    void CWebClientEngine::StartRequest(const TDesC& httpURL,const TDesC& phoneNumber)
    {
    CFileLogger::init();
    CFileLogger::write("Start in WebClient");
    this->CancelTransactionL();
    CFileLogger::write("Start in WebClient 1");
    TBuf<KPrefixedUriNameLength> uri;
    uri.Append(httpURL);

    iCurrentFileName.Copy(_L("c:\\Data\\"));
    iCurrentFileName.Append(phoneNumber);
    TBuf8<KPrefixedUriNameLength> uri8;
    uri8.Copy( uri );
    CFileLogger::write("Start in WebClient 2");
    this->IssueHTTPGetL( uri8 );
    }

    ===============================

    Waiting for nice man response, thanks very much!

  4. #4
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,671

    Re: The application crashes when I tried to download again.

    You may want to get the panic code: http://wiki.forum.nokia.com/index.ph...ded_panic_code
    Since your code can be executed in the emulator, you can also make use of the Just In Time debugging feature of it (you can enable that in the preferences of the emulator) - then execute your code in the debugger, and it will catch you the panic.
    If you experience problems when trying to open preferences, check http://wiki.forum.nokia.com/index.ph...ge_in_emulator

Similar Threads

  1. Do Symbian rejects the Express Signing request on any Business Development ground?
    By arpit2agrawal in forum Symbian Signed Support, Application Packaging and Distribution and Security
    Replies: 23
    Last Post: 2009-02-19, 10:46
  2. Rich Edit Control application crashes...
    By VinodRaut in forum Symbian User Interface
    Replies: 3
    Last Post: 2007-08-20, 11:59
  3. how download application into application folder
    By 13nesta in forum Browsing and Mark-ups
    Replies: 0
    Last Post: 2006-06-14, 19:20
  4. Replies: 2
    Last Post: 2006-03-17, 06:40
  5. Download Application and Technology
    By arizkohli in forum Symbian
    Replies: 0
    Last Post: 2003-06-27, 15:14

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×