Symbian HTTP

Symbian中對HTTP支持最重要的類是 RHTTPSession。
下面講一下symbian中關於http接口api的使用,若有理解不正確的地方,歡迎各位朋友指正 ^_^
http執行事務處理的主要思想是:會話,事務,頭部,數據提供器和過濾器。下面我就以向網絡上的web服務器 發送請求和獲取數據的流程過一下這些api。
// 發送請求流程
在symbian中,http協議棧和相關資源由相關的服務器(注意這裏所指的服務器是symbian系統管理 手機資源的一種結構,我們暫且稱之爲SymbianHttpServ)來管理的。
RHTTPSession代表和SymbianHttpServ的客戶端會話,所以我們首先需要使用它來連接 SymbianHttpServ,以便使用相關資源,如http協議棧等等。
RHTTPSession iSession;
//iSession.SetSessionEventCallback(MHTTPSessionEventCallback *);
iSession.OpenL();
TUriParser8 iUriParser;
User::LeaveIfError(iUriParser.Parse(
    _L8("http://www.symbian.com/index.html")));
// Create the transaction
// for get
iTransaction = iSession.OpenTransactionL(iUriParser, MHTTPTransactionCallback &,
        iSession.StringPool().StringF(HTTP::EGET,
            RHTTPSession::GetTable()));
// for post
iTransaction = iSession.OpenTransactionL(iUriParser, *this,
        iSession.StringPool().StringF(HTTP::EPOST, RHTTPSession::GetTable()));
// Set transaction headers
RHTTPHeaders headers = iTransaction.Request().GetHeaderCollection() ;
// Add a header to a header set
//void Engine::AddHeaderL(RHTTPHeaders aHeaders, TInt aHeaderField,
//    const TDesC8& aHeaderValue)
//{
//    RStringPool stringPool = iSession.StringPool();
//    RStringF valStr = stringPool.OpenFStringL(aHeaderValue);
      // 用THTTPHdrVal類描述HTTP客戶請求包中的每個字段
//    THTTPHdrVal headerVal(valStr);
//    aHeaders.SetFieldL(stringPool.StringF(aHeaderField,
//        RHTTPSession::GetTable()), headerVal);
//    valStr.Close();
//}
//
// User-Agent頭部提供了一個字符串, 用於標識客戶程序
AddHeaderL(headers, HTTP::EUserAgent, _L8("HTTPExample (1.0)"));
// Accept頭部定義了在響應消息主體中接受的內容類型。
// "text
AddHeaderL(headers, HTTP::EContentType, _L8("text/plain"));
// Build form encoder for post request, Start by removing any previous content.
// The get request can ignore this step.
// post請求與get請求的主要不同之處在於post請求本身不僅包括頭,還包括主體(表單)。
delete iFormEncoder; iFormEncoder = NULL;
iFormEncoder = CHTTPFormEncoder::NewL();
TBuf8<32> buf8;
buf8.Copy(_L("cheney"));
iFormEncoder->AddFieldL(_L8("NAME"), buf8);

iTransaction.Request().SetBody(*iFormEncoder);
// Submit the request
iTransaction.SubmitL(); // 異步方法
// 以上過程中若想取消未完成的事務,調用iTransaction.Cancel() 方 法。
// 調用iTransaction.Close() 會 導致所有與事務關聯的資源得到清除,從而決不能再試圖使用它。
// 接收數據流程
當請求發出之後,http服務器會發回通知和數據,客戶端相應的處理全在MHTTPTransactionCallback::MHFRunL 方法中
void MHTTPTransactionCallback::MHFRunL(RHTTPTransaction aTransaction,
    const THTTPEvent& aEvent)
{
    switch (aEvent.iStatus)
    {
        case THTTPEvent::EGotResponseHeaders :
        {
            // HTTP response headers have been received.
            // Pass status information to observer.

            RHTTPResponse resp = aTransaction.Response();
            // Get status code
            TInt statusCode = resp.StatusCode();
            // Get status text
            RStringF statusStr = resp.StatusText();
            HBufC* statusBuf = HBufC::NewLC(statusStr.DesC().Length());
            statusBuf->Des().Copy(statusStr.DesC());
            // Inform observer
            iObserver.ResponseStatusL(statusCode, *statusBuf);
            CleanupStack::PopAndDestroy(statusBuf);
        }
            break;
         // 至少接受了部分主體數據。該事件會發生一次或多次,直至接受了所有主體數據爲止
        case THTTPEvent::EGotResponseBodyData:
        {
            // Get text of response body
            MHTTPDataSupplier* dataSupplier =
                aTransaction.Response().Body();
            TPtrC8 ptr;
            // 從數據提供器獲取主體數據。
           
dataSupplier->GetNextDataPart(ptr);
            // Convert to 16-bit descriptor
            HBufC* buf = HBufC::NewLC(ptr.Length());
            buf->Des().Copy(ptr);
            // Append to iResponseBuffer
            if (!iResponseBuffer)
            {
                iResponseBuffer = buf->AllocL();
            }
            else
            {
                iResponseBuffer = iResponseBuffer->ReAllocL(
                    iResponseBuffer->Length() + buf->Length() );
                iResponseBuffer->Des().Append(*buf);
            }
            // Release buf
            CleanupStack::PopAndDestroy(buf);
            // Release the body data
            dataSupplier->ReleaseData();
        }
            break;
        // 所有的主體數據都已被接受
        case THTTPEvent::EResponseComplete:
        {
            // Pass the response buffer by reference to the observer
            iObserver.ResponseReceivedL(*iResponseBuffer);
        }
            break;
    }
}
Called when RunL leaves from a transaction event. This works in the same way as CActve::RunError; return KErrNone if you have handled the error. If you don't completely handle the error, a panic will occur.
    TInt MHTTPTransactionCallback::MHFRunError(TInt aError,
        RHTTPTransaction aTransaction, const THTTPEvent &aEvent);
發佈了13 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章