📄 http1.cpp
字号:
iObserver->ReportBody(bodyData);
body->ReleaseData();
break;
}
case THTTPEvent::EResponseComplete: // transaction's response is complete (got all content etc)
_LIT(KPostEResponseComplete,"Post:EResponseComplete");
iObserver->LogInfo(KPostEResponseComplete);
break;
case THTTPEvent::ESucceeded: // transaction finished - all ok
aTransaction.Close();
ReleaseData(); // incase its not already released
iObserver->ReportEvent(KErrNone,iStatusCode,iStatusText);
iIsBusy=EFalse;
break;
case THTTPEvent::EFailed: // transaction finished - something fell over
aTransaction.Close();
ReleaseData(); // incase its not already released
iObserver->ReportEvent(KErrGeneral,iStatusCode,iStatusText);
iIsBusy=EFalse;
break;
case THTTPEvent::ERedirectedPermanently:
_LIT(KPostERedirectedPermanently,"Post:ERedirectedPermanently");
iObserver->LogInfo(KPostERedirectedPermanently);
break;
case THTTPEvent::ERedirectedTemporarily:
_LIT(KPostERedirectedTemporarily,"Post:ERedirectedTemporarily");
iObserver->LogInfo(KPostERedirectedTemporarily);
break;
default:
{ // -46 will mean you dont have NetworkServices CAPABILITY !
TBuf<64>bb;
_LIT(KPostUnhandled,"Post:Unhandled: %d");
bb.Format(KPostUnhandled,aEvent.iStatus);
iObserver->LogInfo(bb);
break;
}
}
}
TInt CHttpPostEngine::MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
//
// Called by internal active object if an error occurs.
//
{
TBuf<64>bb;
_LIT(KPostMHFRunError,"Post:MHFRunError: %d,%d");
bb.Format(KPostMHFRunError,aError,aEvent.iStatus);
iObserver->LogInfo(bb);
return(KErrNone);
}
void CHttpPostEngine::SetHeaderL(RHTTPHeaders aHeaders,TInt aHdrField,const TDesC8& aHdrValue)
// Set a header field to be the value indicated
{
RStringF str=iSession.StringPool().OpenFStringL(aHdrValue);
CleanupClosePushL(str);
THTTPHdrVal val(str);
aHeaders.SetFieldL(iSession.StringPool().StringF(aHdrField,RHTTPSession::GetTable()),val);
CleanupStack::PopAndDestroy(&str);
}
void CHttpPostEngine::PostContentL(const TDesC8& aUrl,HBufC8* aData)
// Post content to the indicated indicated URL. In the real world the URL needs to be something the
// remote device can interpret to cause content to be 'accepted' or 'stored'. This in turn leads to some
// specific system.
{
ReleaseData(); // any previous data if code below happened to LEAVE
iData=aData; // take ownership
// use the system URI parser to contain the consituient parts of a URL
TUriParser8 uri;
User::LeaveIfError(uri.Parse(aUrl));
// the system contains pre-constructed 'pools' of non modifiable strings (tokens).
// We want the string identified by HTTP::EPOST from the Http session string table
RStringF method=iSession.StringPool().StringF(HTTP::EPOST, RHTTPSession::GetTable());
// Create a POST transaction...
iTransaction=iSession.OpenTransactionL(uri,*this,method);
// a transaction contains a set of headers, refer to that set of hdrs
RHTTPHeaders hdrs=iTransaction.Request().GetHeaderCollection();
TRAPD(err,
// configure whichever headers are appropriate to your application
_LIT8(KUserAgent,"UIQ Http Example (1.0)");
SetHeaderL(hdrs,HTTP::EUserAgent,KUserAgent);
// content type were sending..
_LIT8(KContentType,"text/plain");
SetHeaderL(hdrs,HTTP::EContentType,KContentType);
// We supply the body to POST. This will call back the MHTTPDataSupplier methods to when
// content needs to be accessed.
iTransaction.Request().SetBody(*this);
// submitting the transaction means it will start..This will call back MHTTPTransactionCallback
// methods
iTransaction.SubmitL();
iIsBusy=ETrue;
);
if (err!=KErrNone)
{
iTransaction.Close();
ReleaseData();
User::Leave(err);
}
}
//////////////////////////////////////////////////////////////////////////////////
class CAppSpecificView : public CQikViewBase, public MHttpEngineObserver
{
protected:
// from CQikViewBase
TVwsViewId ViewId() const;
void HandleCommandL(CQikCommand& aCommand);
void ViewConstructL();
void ViewDeactivated();
void ViewActivatedL(const TVwsViewId& aPrevViewId,const TUid aCustomMessageId,const TDesC8& aCustomMessage);
// from MHttpEngineObserver
void ReportEvent(const TInt aErr,const TInt aStatusCode,const TDesC& aStatusText);
void ReportBody(const TDesC8& aBody);
void LogInfo(const TDesC& aBuf);
// new methods
void RebuildListbox();
public:
// new methods
~CAppSpecificView();
CAppSpecificView(CAppSpecificUi& aAppUi);
protected:
CHttpEngine* iGetEngine;
CHttpPostEngine* iPostEngine;
CLogText* iLogText;
};
CAppSpecificView::~CAppSpecificView()
{
delete(iGetEngine);
delete(iPostEngine);
delete(iLogText);
}
CAppSpecificView::CAppSpecificView(CAppSpecificUi& aAppUi) :
CQikViewBase(aAppUi,KNullViewId)
{}
TVwsViewId CAppSpecificView::ViewId() const
//
// All views are uniquely identified within the entire system. A TVwsViewId consists of
// the application uid (uid3) and app specific view uid
//
{
return(KViewIdAppView);
}
void CAppSpecificView::HandleCommandL(CQikCommand& aCommand)
//
// Handle the commands coming in from the controls that can deliver cmds..
//
{
switch (aCommand.Id())
{
case EAppCmdGet:
// since HTTP transactions are asynchronous we need to do something about stopping a
// 2nd HTTP request whilst the first is busy. In this example we simply display a busy msg
// to demonstrate its truely asynchronous. Commerical apps may want to Cancel() the current
// transaction, then start a new one.
if (iGetEngine->IsBusy())
{
_LIT(KHTTPbusy,"HTTP busy");
iEikonEnv->InfoMsg(KHTTPbusy);
break;
}
// reset log and display to blank
iLogText->ResetText();
RebuildListbox();
// start an HTTP get
_LIT8(KContentAddr233,"http://195.84.17.233");
iGetEngine->GetContentL(KContentAddr233);
break;
case EAppCmdPost:
// since HTTP transactions are asynchronous we need to do something about stopping a
// 2nd HTTP request whilst the first is busy. In this example we simply display a busy msg
// to demonstrate its truely asynchronous. Commerical apps may want to Cancel() the current
// transaction, then start a new one.
if (iPostEngine->IsBusy())
{
_LIT(KHTTPbusy2,"HTTP busy");
iEikonEnv->InfoMsg(KHTTPbusy2);
break;
}
// reset log and display to blank
iLogText->ResetText();
RebuildListbox();
// start an HTTP POST
_LIT8(KContentAddrUiqCom,"http://www.uiq.com");
iPostEngine->PostContentL(KContentAddrUiqCom,_L8("Hello from example app").AllocL());
break;
default: // e.g. the back button...
CQikViewBase::HandleCommandL(aCommand);
break;
}
}
void CAppSpecificView::RebuildListbox()
//
// Rebuild the listbox from scratch
//
{
TRAPD(err,
CQikListBox* listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId);
listbox->RemoveAllItemsL();
MQikListBoxModel& model(listbox->Model());
model.ModelBeginUpdateLC();
const TInt count=iLogText->Count();
for (TInt i=0;i<count;i++)
{
MQikListBoxData* lbData=model.NewDataL(MQikListBoxModel::EDataNormal);
CleanupClosePushL(*lbData);
lbData->AddTextL(iLogText->At(i),EQikListBoxSlotText1);
CleanupStack::PopAndDestroy(lbData);
}
model.ModelEndUpdateL();
);
}
void CAppSpecificView::ReportEvent(const TInt aErr,const TInt aStatusCode,const TDesC& aStatusText)
// Called back by the engine to add some event text to output log so operations are visible
{
TBuf<128>bb;
_LIT(KReportEventStr,"Err:%d,Code:%d,T:%S");
bb.Format(KReportEventStr,aErr,aStatusCode,&aStatusText);
// This is an example app. Logging content to the list box in this fashion cannot be
// recommended for commerical quality applications
iLogText->LogText(bb);
RebuildListbox();
}
void CAppSpecificView::ReportBody(const TDesC8& aBody)
// Called back by the engine when some body has arrived
{
// extract upto first 64 bytes of body, converting cheaply to unicode !!.
TBuf<64>bb;
if (aBody.Length()>64)
{
TPtrC8 q(aBody.Ptr(),64);
bb.Copy(q);
}
else
bb.Copy(aBody);
// This is an example app. Logging content to the list box in this fashion cannot be
// recommended for commerical quality applications
TBuf<128>buf;
_LIT(KReportBodyStr,"Len:%d,Body:%S");
buf.Format(KReportBodyStr,aBody.Length(),&bb);
iLogText->LogText(buf);
RebuildListbox();
}
void CAppSpecificView::LogInfo(const TDesC& aBuf)
// Called back by the engine to add some log text to output log so operations are visible
{
// This is an example app. Logging content to the list box in this fashion cannot be
// recommended for commerical quality applications
iLogText->LogText(aBuf);
RebuildListbox();
}
void CAppSpecificView::ViewConstructL()
{
// Loads information about the UI configurations this view supports
// together with definition of each view.
ViewConstructFromResourceL(R_APP_VIEW_CONFIGURATIONS);
// the HTTP GET engine
iGetEngine=new(ELeave)CHttpEngine;
iGetEngine->ConstructL(this);
// the HTTP POST engine
iPostEngine=new(ELeave)CHttpPostEngine;
iPostEngine->ConstructL(this);
// we store logged text here
iLogText=new(ELeave)CLogText;
iLogText->ConstructL();
}
void CAppSpecificView::ViewDeactivated()
//
// Here we choose to remove all items from the list box. This saves memory.
//
{
}
void CAppSpecificView::ViewActivatedL(
//
// The view is being activated. Generate the listbox content. Move highlight to current entry.
//
const TVwsViewId& aPrevViewId,
const TUid aCustomMessageId,
const TDesC8& aCustomMessage)
{
}
//////////////////////////////////////////////////////////////////////////////
CAppSpecificUi::~CAppSpecificUi()
{
}
void CAppSpecificUi::ConstructL()
//
// Normal primary entry point to a Symbian App
//
{
CQikAppUi::ConstructL();
CAppSpecificView* list=new(ELeave)CAppSpecificView(*this);
CleanupStack::PushL(list);
list->ConstructL();
AddViewL(*list); // takes ownership
CleanupStack::Pop(list);
SetDefaultViewL(*list);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Standard Symbian application framework code when creating an application
class CAppSpecificDocument : public CQikDocument
{
protected:
CQikAppUi* CreateAppUiL();
public:
CAppSpecificDocument(CQikApplication& aApp);
static CAppSpecificDocument* NewL(CQikApplication& aApp);
protected:
};
CAppSpecificDocument::CAppSpecificDocument(CQikApplication& aApp) :
CQikDocument(aApp)
{
__DECLARE_NAME(_S("CAppSpecificDocument"));
}
CAppSpecificDocument* CAppSpecificDocument::NewL(CQikApplication& aApp)
{
return(new(ELeave)CAppSpecificDocument(aApp));
}
CQikAppUi* CAppSpecificDocument::CreateAppUiL()
{
return(new(ELeave)CAppSpecificUi);
}
//////////////////////////////////////////////////////////////////////////////////
// Standard Symbian application framework code when creating an application
class CAppSpecificApplication : public CQikApplication
{
protected:
TUid AppDllUid() const;
CApaDocument* CreateDocumentL();
};
TUid CAppSpecificApplication::AppDllUid() const
{
return(KAppSpecificUid);
}
CApaDocument* CAppSpecificApplication::CreateDocumentL()
{
return(CAppSpecificDocument::NewL(*this));
}
//////////////////////////////////////////////////////////////////////////////////
// Standard Symbian application start up code
LOCAL_C CApaApplication* NewApplication()
{
return(new CAppSpecificApplication);
}
GLDEF_C TInt E32Main()
{
return(EikStart::RunApplication(NewApplication));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -