网络编程,CInternetSession,OpenUrl,CStdion,exe 请问用CInternetSession.OpenUrl函数和CStdion类从网站上下载一个exe文件的方法 |
作者:不详 来源:本站整理 发布时间:2007-1-22 8:57:56 发布人:Admin |
问题
于 2006-09-15 00:18
如题:
<---->
我的代码如下: char szbuff[1024] ; szbuff[1023] = '/0' ; static char fileName[] = "c://1. exe" ; CFile file(fileName , CFile::modeCreate | CFile::modeWrite) ; CInternetSession session ; CStdioFile * pFile = NULL ; try { pFile = session.OpenURL(szUrl) ; while (pFile->Read(szbuff, 1023) != 0) { file.Write(szbuff, 1024) ; } } catch(CInternetException * e) { AfxMessageBox("error!") ; e->Delete(); } file.Cl ose() ; if(pFile) { delete pFile ; } session.Cl ose() ; 下载下来后,1. exe文件中为一个页面无法打开的htm页面,请教正确的下载方法。
响应者
1:
刚才看了下msdn,原来如此。
<---->
To use connection-specific (that is, protocol-specific) functions, such as writing to a file, you must open a session, then open a particular kind of connection, then use that connection to open a file in the desired mode. See CInternetConnection for more information about connection-specific functions.
响应者
2:
楼主有使用到
断点续传吗?小弟正在研究这个,如果有的话,希望不吝赐教
<---->
你所要下载的文件是ftp服务器上还是http服务器上呀? 我看了,如果你这个程序不改成 断点续传的说,基本上是通用的,但要是改成 断点续传的话,就不行了,如果你有更好的解决方法,希望能贴出来,小弟学习一下。
响应者
3:
CInternetSession mySession;
<----><---->
CHttpFile* myHttpFile; CStdioFile* csf=new CStdioFile; try { myHttpFile=(CHttpFile*)mySession.OpenURL(m_thistruct->fileurl); CString queryinfo; myHttpFile->QueryInfo(HTTP_QUERY_STATUS_CODE,queryinfo); if(queryinfo=="200") { myHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,queryinfo); m_thistruct->m_Size=(float)atof(queryinfo); char buf[4096]; int n; if(csf->Open(url,CFile::modeRead|CFile::modeWrite | CFile::typeBinary | CFile::shareDenyWrite)) { myHttpFile->Seek(csf->GetLength(),CHttpFile::begin); m_thistruct->m_DownSize=(float)csf->SeekToEnd(); while((n=myHttpFile->Read(buf,sizeof(buf)))>0)// { csf->Write(buf,n); } } else { if(csf->Open(m_thistruct->clentfilepath,CFile::modeCreate|CFile::modeWrite | CFile::typeBinary | CFile::shareDenyWrite)) { while((n=myHttpFile->Read(buf,sizeof(buf)))>0) { csf->Write(buf,n); } } } } }catch(CException *e) { e->Delete(); } if(csf) { csf->Cl ose(); } delete csf; if(myHttpFile) { myHttpFile->Cl ose(); } delete myHttpFile; mySession.Cl ose(); |
CInternetSession
最新推荐文章于 2016-11-24 16:53:32 发布