//创建 CInternetSession对象,初始化WinInet,并连接服务器
m_pInetSession=new CInternetSession(NULL
,1
,INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD);
//设置参数
m_pInetSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,1000,1);
m_pInetSession->SetOption(INTERNET_OPTION_CONNECT_BACKOFF,1000);
m_pInetSession->SetOption(INTERNET_OPTION_CONNECT_RETRIES,1);
//打开URL文件,返回CStdioFile
try
{ pSFile = m_pInetSession->OpenURL(http://www.mywerb.com/tt.mp3);
//缓冲区
BYTE pBuf[1024] = {NULL};
CFile pCFile = NULL;
//创建本地文件
pCFile.Open("c:\\test.mp3",
CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
//从缓冲区循环读取文数据
int n;
while ( n = pSFile->Read(pBuf,1024))
{
//将读取的缓冲区数据写入本地文件
pCFile.Write(pBuf,n);
}
//关闭本地文件
pCFile.Close();
//关闭CStdioFile
pSFile->Close();
}
catch(CInternetException* lpEx)
{
lpEx->ReportError();
lpEx->Delete();
delete m_pInetSession;
}
AfxMessageBox("下载完成");
想在上面这段代码中,增加断点续传的功能,如何实现呢?
m_pInetSession=new CInternetSession(NULL
,1
,INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD);
//设置参数
m_pInetSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,1000,1);
m_pInetSession->SetOption(INTERNET_OPTION_CONNECT_BACKOFF,1000);
m_pInetSession->SetOption(INTERNET_OPTION_CONNECT_RETRIES,1);
//打开URL文件,返回CStdioFile
try
{ pSFile = m_pInetSession->OpenURL(http://www.mywerb.com/tt.mp3);
//缓冲区
BYTE pBuf[1024] = {NULL};
CFile pCFile = NULL;
//创建本地文件
pCFile.Open("c:\\test.mp3",
CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
//从缓冲区循环读取文数据
int n;
while ( n = pSFile->Read(pBuf,1024))
{
//将读取的缓冲区数据写入本地文件
pCFile.Write(pBuf,n);
}
//关闭本地文件
pCFile.Close();
//关闭CStdioFile
pSFile->Close();
}
catch(CInternetException* lpEx)
{
lpEx->ReportError();
lpEx->Delete();
delete m_pInetSession;
}
AfxMessageBox("下载完成");
想在上面这段代码中,增加断点续传的功能,如何实现呢?
2.sprintf(SendInfo,"Range:bytes=%u-",FilePointer);
这里FilePointer就是从文件哪里开始续传
3.HttpSendRequest()
4.HttpQueryInfo() 查看是否返回206这个值,如果是,证明是支持续传.
pSFile.seek(nDownload, nDownload);
pCFile.seek(nDownload, nDownload);
nRead = pSfile.read(pBuf, sizeof(pBuf));
pCFile.write(pBuf, nRead);
大概是这个意思,具体细节还要处理一下!