?? httpengine.cpp
字號:
#include "HTTPEngine.h"
// System includes
#include <chttpformencoder.h> // CHTTPFormEncoder
#include <httpstringconstants.h> // HTTP string table
#include <rhttpheaders.h> // RHTTPHeaders
#include "Assistant.hrh"
_LIT8(KUserAgent, "HTTPExample (1.0)"); // Name of this client app
_LIT8(KAccept, "text/*"); // Accept any (but only) text content
_LIT8(KPostParamName, "NAME"); // Name of the parameter sent in a POST request
_LIT8(KPostContentType, "text/plain"); // Content type sent in a POST request
// URL for POST request.
//_LIT8(KPostUri, "http://cgi.www.emccsoft.com/cgi-bin/www.emccsoft.com/post.pl");
_LIT8(KPostUri, "http://www.ldci.mobi/snsProject/register?mobile=13800138001&pass=123456");
CHTTPEngine* CHTTPEngine::NewL(MHTTPEngineObserver& aObserver)
{
CHTTPEngine* self = new (ELeave) CHTTPEngine(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
CHTTPEngine::CHTTPEngine(MHTTPEngineObserver& aObserver) : m_Observer(aObserver)
{
}
void CHTTPEngine::ConstructL()
{
// Open the RHTTPSession
m_Session.OpenL();
// Construct the form encoder
m_pFormEncoder = CHTTPFormEncoder::NewL();
}
CHTTPEngine::~CHTTPEngine()
{
// Close session
m_Session.Close(); // Will also close any open transactions
delete m_pResponseBuffer;
delete m_pFormEncoder;
delete m_pUri;
}
void CHTTPEngine::GetRequestL(const TDesC& aUri)
{
// Parse the URI
ParseUriL(aUri);
// Create the transaction
m_Transaction = m_Session.OpenTransactionL(m_pUriParser, *this,
m_Session.StringPool().StringF(HTTP::EGET, RHTTPSession::GetTable()));
// Set transaction headers
RHTTPHeaders headers = m_Transaction.Request().GetHeaderCollection();
AddHeaderL(headers, HTTP::EUserAgent, KUserAgent);
AddHeaderL(headers, HTTP::EAccept, KAccept);
// Submit the request
m_Transaction.SubmitL();
}
void CHTTPEngine::PostRequestL(const TDesC& aContent,const TDesC8& aUri)
{
// Build form encoder
// Start by removing any previous content
delete m_pFormEncoder;
m_pFormEncoder = NULL;
m_pFormEncoder = CHTTPFormEncoder::NewL();
TBuf8<EMaxNameLength> buf8;
buf8.Copy(aContent);
m_pFormEncoder->AddFieldL(KPostParamName, buf8);
// Create transaction
m_pUriParser.Parse(aUri);//解析
m_Transaction = m_Session.OpenTransactionL(m_pUriParser, *this,
m_Session.StringPool().StringF(HTTP::EPOST, RHTTPSession::GetTable()));
// Set transaction headers
RHTTPHeaders headers = m_Transaction.Request().GetHeaderCollection();
AddHeaderL(headers, HTTP::EUserAgent, KUserAgent);
AddHeaderL(headers, HTTP::EAccept, KAccept);
AddHeaderL(headers, HTTP::EContentType, KPostContentType);
// Set the form encoder as the data supplier
m_Transaction.Request().SetBody(*m_pFormEncoder);
// Submit the request
m_Transaction.SubmitL();
}
// Cancel an outstanding transaction
void CHTTPEngine::Cancel()
{
m_Transaction.Cancel();
}
void CHTTPEngine::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
m_Observer.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 (!m_pResponseBuffer)
{
m_pResponseBuffer = buf->AllocL();
}
else
{
m_pResponseBuffer = m_pResponseBuffer->ReAllocL(m_pResponseBuffer->Length() + buf->Length());
m_pResponseBuffer->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
m_Observer.ResponseReceivedL(*m_pResponseBuffer);
}
break;
}
}
TInt CHTTPEngine::MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
{
return aError;
}
void CHTTPEngine::ParseUriL(const TDesC& aUri)
{
// Convert the URI to an 8-bit descriptor
// then set iUriParser to point at it
delete m_pUri;
m_pUri = NULL;
m_pUri = HBufC8::NewL(aUri.Length());
m_pUri->Des().Copy(aUri);
User::LeaveIfError(m_pUriParser.Parse(*m_pUri));
}
void CHTTPEngine::AddHeaderL(RHTTPHeaders aHeaders, TInt aHeaderField, const TDesC8& aHeaderValue)
{
RStringPool stringPool = m_Session.StringPool();
RStringF valStr = stringPool.OpenFStringL(aHeaderValue);
THTTPHdrVal headerVal(valStr);
aHeaders.SetFieldL(stringPool.StringF(aHeaderField, RHTTPSession::GetTable()), headerVal);
valStr.Close();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -