📄 downloadfile.cpp
字号:
/*
* downloadfile.cpp
*
* Created on: 2009-4-27
* Author: Administrator
*/
#ifndef DOWNLOADFILE_H_
#define DOWNLOADFILE_H_
#include "downloadfile.h"
void CDownLoadEngine::GetRequestL(const TDesC8& aUri)
{
ParseUriL(aUri);//对URL地址进行分析
// Create the transaction
iTransaction = iSession.OpenTransactionL(iUriParser, *this,
iSession.StringPool().StringF(HTTP::EGET, RHTTPSession::GetTable()));
RHTTPHeaders headers = iTransaction.Request().GetHeaderCollection();
//添加HTTP请求消息头
AddHeaderL(headers, HTTP::EUserAgent, KUserAgent);
AddHeaderL(headers, HTTP::EAccept, KAccept);
// Set transaction headers
if (0 != m_recv_bytes)
{
TBuf8 <20> aBufLength;
aBufLength.SetLength(0);
aBufLength.Append(KRange);
aBufLength.AppendNum(m_recv_bytes);
aBufLength.Append(_L("-"));//这里一定要注意了。如果是断点下载一定要加上这个,我可被这个害死了
AddHeaderL(headers, HTTP::ERange,aBufLength);
}
// Submit the request
iTransaction.SubmitL();
}
/**---------------------------------------------------------------------------
* 文件名 : DemoClass.cpp
* 功 能 : 实现URL连接
* 作 者 : 洪文彬
* 日 期 : 2009/4/27
**---------------------------------------------------------------------------
*/
void CDownLoadEngine::ParseUriL(const TDesC8& aUri)
{
// Convert the URI to an 8-bit descriptor
// then set iUriParser to point at it
delete iUri;
iUri = NULL;
iUri = HBufC8::NewL(aUri.Length());
iUri->Des().Copy(aUri);
User::LeaveIfError(iUriParser.Parse(*iUri));
}
//这个函数时处理从internet返回的消息
void CHTTPExampleEngine::AddHeaderL(RHTTPHeaders aHeaders, TInt aHeaderField, const TDesC8& aHeaderValue)
{
RStringPool stringPool = iSession.StringPool();
RStringF valStr = stringPool.OpenFStringL(aHeaderValue);
THTTPHdrVal headerVal(valStr);
aHeaders.SetFieldL(stringPool.StringF(aHeaderField, RHTTPSession::GetTable()), headerVal);
valStr.Close();
}
void CDownLoadEngine::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
{
switch (aEvent.iStatus)
{
case THTTPEvent::EGotResponseHeaders://返回的是消息头
//这个样说明一下之前那个基于RSock的话需要自己去判断那里是消息头那里是Boday
//在这个例子中框架可以给你去判断
{
THTTPHdrVal aHeaderValue;
RHTTPResponse resp = aTransaction.Response();
RHTTPHeaders aHeaders = resp.GetHeaderCollection();
TRAPD(error,GetHeaderL (aHeaders, _L8("Content-Length"), aHeaderValue ));
m_total_bytes = aHeaderValue.Int();
m_total_bytes += m_recv_bytes;
}
break;
case THTTPEvent::EGotResponseBodyData:
//获取消息Body后要将其写入本地文件中
{
MHTTPDataSupplier* dataSupplier = aTransaction.Response().Body();
TPtrC8 ptr;
dataSupplier->GetNextDataPart(ptr);
HBufC* buf = HBufC::NewLC(ptr.Length());
buf->Des().Copy(ptr);
HBufC8* aBufTmp;
// Append to iResponseBuffer
if (!iResponseBuffer)
{
iResponseBuffer = buf->AllocL();
aBufTmp = HBufC8::NewL(iResponseBuffer->Des().Length());
aBufTmp ->Des().Copy(iResponseBuffer->Des());
RecvNotify(*aBufTmp);
}
else
{
// iResponseBuffer = iResponseBuffer->ReAllocL(iResponseBuffer->Length() + buf->Length());
// iResponseBuffer->Des().Append(*buf);
aBufTmp = HBufC8::NewL(buf->Des().Length());
aBufTmp ->Des().Copy(buf->Des());
RecvNotify(*aBufTmp);
}
CleanupStack::PopAndDestroy(buf);
//CleanupStack::PopAndDestroy(aBufTmp);
dataSupplier->ReleaseData();
}
break;
case THTTPEvent::EResponseComplete:
//下载完毕
{
TInt aIndexofTmp;
aIndexofTmp = iFileName.Find(_L(".tmp"));
iFileName.Delete(aIndexofTmp,4);
iFile.Rename(iFileName);
}
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -