Hi,
I'm trying to convert the sample synchronous http example client to asynchronous with CActive object.
I'm trying to build a DLL.
The app registers a callback and calls the exported function in the DLL.
the function should make a http request and return immediately without waiting for response.
After receiving the server response, the DLL should send the response to the app with callback.
I've the following code:
class CHttpClient : public CBase, public MHTTPTransactionCallback
{
public:
static CHttpClient* NewL();
~CHttpClient();
void InvokeHttpMethodL(const TDesC8& aUri, TRequestStatus& aStatus);
void CancelTheRequest();
// methods inherited from MHTTPDataSupplier
virtual TBool GetNextDataPart(TPtrC8& aDataPart);
virtual void ReleaseData();
virtual TInt OverallDataSize();
virtual TInt Reset();
protected:
CHttpClient();
void ConstructL();
private: // Functions from base classes
void MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent);
TInt MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& aEvent);
void SetHeaderL(RHTTPHeaders aHeaders, TInt aHdrField, const TDesC8& aHdrValue);
void GetRequestBodyL(RStringF& aMethod);
RHTTPSession iHttpSession;
RHTTPTransaction iTransaction;
TBool iSavingResponseBody;
RFs iFileServ;
RFile iRespBodyFile;
TFileName iRespBodyFileName;
TParse iParsedFileName;
MHTTPDataSupplier* iRespBody;
};
class CActiveHttpObj : public CActive
{
public: // New functions
static CActiveHttpObj* NewL(CHttpClient* aHttpClient);
~CActiveHttpObj();
void RequestURL(const TDesC8& aUri);
private:
CActiveHttpObj(CHttpClient* aHttpClient);
void ConstructL();
private: // Functions from base classes
void RunL();
void DoCancel();
TInt RunError(TInt aError);
private: // Data members
CHttpClient* iHttpClient;
};
////
void CActiveHttpObj::RunL()
{
if (iStatus == KErrNone)
{
TRequestStatus* status = &iStatus;
User::RequestComplete( status, KErrNone );
}
}
///
void CActiveHttpObj::RequestURL(const TDesC8& aUri)
{
iStatus = KRequestPending;
if (IsActive())
{
return;
}
iHttpClient->InvokeHttpMethodL(aUri, iStatus);
SetActive();
}
///
in my exported function()
{
CActiveScheduler* scheduler = new(ELeave) CActiveScheduler();
CleanupStack::PushL(scheduler);
CActiveScheduler::Install(scheduler);
// Create and start the client
CHttpClient* httpCli = CHttpClient::NewL();
CActiveHttpObj* httpobj = CActiveHttpObj::NewL(httpCli);
httpobj->RequestURL(url);
CActiveScheduler::Start();
printf("\n done downloading");
CleanupStack::PopAndDestroy(2);
}
//////////////
It's still a blocking call. I don't see the printf until the request completes. How do I make it asynchronous? Do I need to create a thread and call CActiveScheduler::Start() from that thread?
I didn't change the code in CHttpClient::MHFRunL ().
Do I need any more changes? Could someone please tell me where is the mistake?
Thanks in advance,
jvinjam




