📄 httpclientengine.cpp
字号:
/*
* Copyright (c) 2007,苏州丰达
* All rights reserved.
* 版权所有(C)2007-2008 苏州丰达
* 公司地址:中国,江苏省苏州市
* 网址:http://www.
*
* 文件名称:CHTTPClientEngine.cpp
* 摘 要:
*
* 产品版本:彩视 1.0
*
* 作 者:xxxx
* 创建日期:xxxxx
* 负责人:xxxxx
*
* 修改者:司治国
* 修改日期:2008.1.17
*
* 编译器或环境等描述:uiqsdk2.1
* 适用于VC2003+symbian 7.x的环境开发。
*
**/
// System includes
#include <httpstringconstants.h>
#include <http\rhttpheaders.h>
#include <EscapeUtils.h>
#include <s32file.h>
// User includes
#include "M3uiq2.hrh" // EMaxNameLength
#include "HttpClientEngine.h"
#include "RLog.h"
// CONSTANTS
_LIT8(KUserAgent, "Acmcc_uiq2_10000");//头信息
_LIT8(KUserAgentold, "M3upload 1.00"); // Name of this client app
_LIT8(KAccept, "text/*"); // Accept any (but only) text content
_LIT8(KPostContentType, "text/plain"); // Content type sent in a POST request
_LIT8(KPROXY, "10.0.0.172:80");
_LIT(KServerUrl, "http://m3push.m3go.com/ups/");
// ================= MEMBER FUNCTIONS =======================
// Schemes for given uris
_LIT(KHttpPrefix, "http://");
_LIT8(KHttpPrefix8, "http://");
// HTTPS schemes
_LIT(KHttpsPrefix, "https://");
_LIT8(KHttpsPrefix8, "https://");
/**
* 二段构造
* @param aObserver http引擎的观察器
* @Return http引擎的指针
*/
CHTTPClientEngine * CHTTPClientEngine::NewL(MHTTPClientEngineObserver &aObserver)
{
CHTTPClientEngine *self = new (ELeave) CHTTPClientEngine(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
/**
* 默认构造
* @param aObserver http引擎的观察器
*/
CHTTPClientEngine::CHTTPClientEngine(MHTTPClientEngineObserver &aObserver) : iObserver(aObserver), iPostData(NULL)
{
}
/**
* 两段构造
*/
void CHTTPClientEngine::ConstructL()
{
// 打开 RHTTPSession
TRAPD(err, iSession.OpenL());
iSession.SetSessionEventCallback(this);
if(err != KErrNone)
{
// Most common error; no access point configured, and session creation
// leaves with KErrNotFound.
_LIT(KErrMsg,
"Cannot create session. Is internet access point configured?");
_LIT(KExitingApp, "Exiting app.");
iObserver.ResponseStatusL(-1,_L("error"));
User::Leave(err);
}
iFormEncoder = CHTTPFormEncoder::NewL();
}
/**
* 析构函数,释放内存
*/
CHTTPClientEngine::~CHTTPClientEngine()
{
// Close session
iSession.Close(); // Will also close any open transactions
if (iUri)
{
delete iUri;
}
if (iPostData)
{
delete iPostData;
iPostData = NULL;
}
if (iFormEncoder)
{
delete iFormEncoder;
iFormEncoder = NULL;
}
ResetReceiveData();
}
/**
* Override of pure virtual method in MHTTPTransactionCallback.
* Called to report progress by a currently outstanding transaction.
*
* @param aTransaction The transaction reporting progress
* @param aEvent The event being notified
*/
void CHTTPClientEngine::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());
//用于写log
TBuf<32> temp;
temp.Append(_L("http EGotResponseHeaders:"));
RLog::Log(temp);
RLog::Log(statusBuf->Des());
// 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);
//用于写log
TBuf<32> temp;
temp.Append(_L("http EGotResponseBodyData1:"));
RLog::Log(temp);
HBufC* h = HBufC::NewL(ptr.Length());
h->Des().Copy(ptr);
RLog::Log(h->Des());
delete h;
h = NULL;
// Convert to 16-bit descriptor
HBufC8 *buf = HBufC8::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);
//用于写log
TBuf<32> temp;
temp.Append(_L("http EGotResponseBodyData2:"));
RLog::Log(temp);
HBufC* h = HBufC::NewL(iResponseBuffer->Length());
h->Des().Copy(iResponseBuffer->Des());
RLog::Log(h->Des());
}
iObserver.ResponseStatusL(THTTPEvent::EGotResponseBodyData, _L(""));
// Release buf
CleanupStack::PopAndDestroy(buf);
// Release the body data
dataSupplier->ReleaseData();
}
break;
case THTTPEvent::EResponseComplete:
{
if (iAct == EDown)
{
iObserver.ResponseReceivedDownL(iResponseBuffer);
//用于写log
TBuf<32> temp;
temp.Append(_L("http EResponseComplete1:"));
RLog::Log(temp);
HBufC* h = HBufC::NewL(iResponseBuffer->Length());
h->Des().Copy(iResponseBuffer->Des());
RLog::Log(h->Des());
}
else
{
HBufC16 *bufc16;
bufc16 = EscapeUtils::ConvertToUnicodeFromUtf8L(*iResponseBuffer);
//用于写log
TBuf<32> temp;
temp.Append(_L("http EResponseComplete2:"));
RLog::Log(temp);
HBufC* h = HBufC::NewL(iResponseBuffer->Length());
h->Des().Copy(iResponseBuffer->Des());
RLog::Log(h->Des());
delete h;
h = NULL;
iObserver.ResponseReceivedL(*bufc16);
ResetReceiveData();
if (bufc16)
{
delete bufc16;
bufc16 = NULL;
}
}
//记录用户返回的
}
break;
case THTTPEvent::EFailed:
{
iObserver.ResponseStatusL(-1,_L("The transaction has failed"));
}
break;
default:
{
if (aEvent.iStatus < 0)
{
iObserver.ResponseStatusL(-1,_L("error"));
}
break;
}
}
}
/**
* 错误处理
* @param aError 错误类型
* @param aTransaction
* @param aEvent
* @Return 错误类型
*/
TInt CHTTPClientEngine::MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
{
iObserver.ResponseStatusL(-1,_L("error"));
return aError;
}
/**
* 初始化一个get请求
* @param aUri URI地址
*/
void CHTTPClientEngine::GetRequestL(const TDesC &aUri)
{
ResetReceiveData();
// Parse the URI
ParseUriL(aUri);
//TBuf8<20> aProxy8(_L8("10.0.0.172:80"));
//RStringF iPrxAddr = iSession.StringPool().OpenFStringL(aProxy8);
//CleanupClosePushL(iPrxAddr);
//THTTPHdrVal iPrxUsage(iSession.StringPool().StringF(HTTP::EUseProxy,RHTTPSession::GetTable()));
//iSession.ConnectionInfo().SetPropertyL(iSession.StringPool().StringF(HTTP::EProxyUsage,RHTTPSession::GetTable()), iPrxUsage);
//iSession.ConnectionInfo().SetPropertyL(iSession.StringPool().StringF(HTTP::EProxyAddress,RHTTPSession::GetTable()), iPrxAddr);
//CleanupStack::PopAndDestroy();
// Get request method string for HTTP GET
RStringF method = iSession.StringPool().StringF(HTTP::EGET, RHTTPSession::GetTable());
// Create the transaction
iTransaction = iSession.OpenTransactionL(iUriParser, *this, method);
// Set transaction headers
RHTTPHeaders headers = iTransaction.Request().GetHeaderCollection();
AddHeaderL(headers, HTTP::EUserAgent, KUserAgent);
AddHeaderL(headers, HTTP::EAccept, KAccept);
// Submit the request
iTransaction.SubmitL();
}
/**
* 重置接受的数据
**/
void CHTTPClientEngine::ResetReceiveData()
{
if (iResponseBuffer)
{
delete iResponseBuffer;
iResponseBuffer = NULL;
}
}
/**
* 初始化一个post请求
* @param aText URI地址
* @param aPostContent post内容
* @param aAct
*/
void CHTTPClientEngine::PostRequestL(const TDesC &aText, const TDesC *aPostContent, TActionOps aAct)
{
ResetReceiveData();
if (iPostData)
{
delete iPostData;
}
TBuf<1024> url;
if (aPostContent)
{
iPostData = EscapeUtils::ConvertFromUnicodeToUtf8L(*aPostContent);
TInt iSize = iPostData->Length();
//log
RLog::Log(aText);
//处理url
_LIT(KAUPFILE,"upfile?%d@");//isize,url
url.Format(KAUPFILE, iSize);
url.Append(aText);
//log
TBuf<256> temp;
temp.Append(_L("all url :"));
temp.Append(url);
RLog::Log(temp);
}
//TODO
iAct = aAct;
if (iAct == EUp)
{
//todo 分段上传,断点上传
RFile file;
RFs fs;
User::LeaveIfError(fs.Connect());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -