19,502
社区成员
发帖
与我相关
我的任务
分享
void CHttpGet::Download(CString url)
{
try
{
CStdioFile* pSFile=NULL;
CInternetSession* m_pInetSession=new CInternetSession(NULL,1,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
//打开URL文件,返回CStdioFile
LPCTSTR curl=(LPCTSTR)url.GetBuffer(url.GetLength());
pSFile = m_pInetSession->OpenURL(L"http://file1.top100.cn/200908161441/21CC5B00B81E9E38CDEBAF6D712FC7FC/Special_28186/%E6%B0%B4%E6%99%B6%E8%9C%BB%E8%9C%93.mp3");
//缓冲区
BYTE pBuf[1024] = {NULL};
CFile pCFile = NULL;
//创建本地文件
pCFile.Open(_T("c:\\"),CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
//从缓冲区循环读取文数据
while (int bytesread = pSFile->Read(pBuf,1024))
{
//指针移到文件结尾
pCFile.SeekToEnd();
//将读取的缓冲区数据写入本地文件
pCFile.Write(pBuf,bytesread);
}
//关闭本地文件
pCFile.Close();
//关闭CStdioFile
pSFile->Close();
delete m_pInetSession;
AfxMessageBox(L"Finish! the directory is C:\\",0,0);
}
catch(CInternetException* lpEx)
{
lpEx->ReportError();
lpEx->Delete();
AfxMessageBox(L"failed !",0,0);[code=C/C++]
hOpener = InternetOpenUrl(m_hSession, pstrURL, pstrHeaders,dwHeadersLength, dwFlags, dwContext); //返回为0 造成的
一开始以为是我使用调用CString 类型的url造成的, 后来把类型转换成LPCSTR 还是不行