急! 使用wininet API 实现Http下载

unicornhx 2009-08-16 02:49:21
刚刚接触Wince 6.0 现在要实现一个使用url链接下载的功能 ,参考了网上一些程序编写如下代码

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++]

}
[/code]
编译通过了 在Debug时 发现调用OpenURL函数时出现“the server name or address could not be resolved”
跟踪到inet.cpp 发现是由于

hOpener = InternetOpenUrl(m_hSession, pstrURL, pstrHeaders,dwHeadersLength, dwFlags, dwContext); //返回为0 造成的
一开始以为是我使用调用CString 类型的url造成的, 后来把类型转换成LPCSTR 还是不行
是不是还要把http数据包头补上?? 求各位高手指点~~
...全文
58 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
egmkang 2009-08-20
  • 打赏
  • 举报
回复
换吧
前几天用wininet的FTP那些API,愣是没找到原因...

19,502

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 嵌入开发(WinCE)
社区管理员
  • 嵌入开发(WinCE)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧