http文件下载的中文文件名问题????

wangxi1240 2006-09-04 07:41:43
使用CHttpConnection和CHttpFile下载文件,当要下载的文件是英文名字如a.wav正常,但是当文件名称为中文如财神.wav时,就下载不了,应该是中文文件名的问题,不知道哪位大侠知道怎么解决阿?
我的代码如下,多谢各位帮忙:
CHttpDownload::DownloadFile(CString sURL, CString sLocalPath)
{
CInternetSession session; //会话期对象)
CHttpConnection* pServer = NULL; // 指向服务器地址(URL)
CHttpFile * pHttpFile = NULL;//HTTP文件指针
CString strServerName; //服务器名
CString strObject; //查询对象名(http文件)
INTERNET_PORT nPort; //端口
DWORD dwServiceType; //服务类型
DWORD dwHttpRequestFlags = INTERNET_FLAG_NO_AUTO_REDIRECT;//请求标志
//INTERNET_FLAG_EXISTING_CONNECT;
const TCHAR szHeaders[]=_T("Accept: text/*,audio/*\r\nUser-Agent:HttpClient\r\n");
BOOL OK=AfxParseURL( //词法分析
sURL, //被分析URL串
dwServiceType, //服务类型,ftp,http等
strServerName, //服务器名
strObject, //URL中被查询对象
nPort ); //URL指定的端口,可能为空

OK=OK && (dwServiceType == INTERNET_SERVICE_HTTP);

if (!OK)
{
CLog::Trace(ER,"要下载的http地址%s无效",sURL);
return ErrHttpInvalidAddress; //无效地址
}
try
{
pServer = session.GetHttpConnection(strServerName, nPort); //获得服务器名
pHttpFile = pServer-> OpenRequest( CHttpConnection::HTTP_VERB_GET,strObject, NULL, 1, NULL, NULL,dwHttpRequestFlags);
//向服务器发送请求,建立http连接,
//建立本机上的http文件指针
pHttpFile->AddRequestHeaders(szHeaders);
pHttpFile->SendRequest(); //发送请求
CFile f; //输出文件对象
if( !f.Open(sLocalPath, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary ) ) //打开输出文件
{
CLog::Trace(ER,"下载http文件%s时打开本地文件出错",sLocalPath);
return ErrHttpInvalidLocalPath;//本地文件打开出错
}
//下面将检索结果保存到文件上

TCHAR szBuf[1024]; //缓存
int length=0;
long a=pHttpFile->GetLength();
while (length=pHttpFile->Read(szBuf, 1024))
{
f.Write(szBuf,length);
}
f.Close(); //善后工作
pHttpFile ->Close();
pServer ->Close();

if (pHttpFile != NULL)
delete pHttpFile;

if (pServer != NULL)
delete pServer;

session.Close();
}
catch(CInternetException ex)
{
CLog::Trace(ER,"下载http文件%s时,出现异常",sLocalPath);
return ErrInternetException;
}
catch(...)
{
CLog::Trace(ER,"下载http文件%s时,出现异常",sLocalPath);
return ErrOtherException;
}
return ErrSucc;
}
...全文
499 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
robinwjb 2006-09-06
  • 打赏
  • 举报
回复
要不给我个URL,我试试。
robinwjb 2006-09-06
  • 打赏
  • 举报
回复
错误处在哪一步?
HttpStatusCode 是多少?

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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