怎么用VC写一个从http上下载一文件,谢谢, 最近公司要做一个版本自动升级的东西,所以要到网站上下载最新版本文件那在VC下怎么写,不用特殊的控件,,谢谢!!!!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 try{ CInternetSession session(_T("xxx")); session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,32000); session.SetOption(INTERNET_OPTION_CONNECT_RETRIES,10); session.SetOption(INTERNET_OPTION_CONNECT_BACKOFF,1000); CHttpConnection *pConn = session.GetHttpConnection("www.xxx.com"); CHttpFile *hFile = NULL; hFile = pConn->OpenRequest("GET", "filename"); if(hFile) { hFile->AddRequestHeaders(L"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, */*"); hFile->AddRequestHeaders(L"Accept-Language: zh-cn"); hFile->AddRequestHeaders(L"Content-Type: application/x-www-form-urlencoded"); hFile->AddRequestHeaders(L"Accept-Encoding: gzip, deflate"); hFile->AddRequestHeaders(L"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"); hFile->AddRequestHeaders(L"Connection: Keep-Alive"); hFile->AddRequestHeaders(L"Cache-Control: no-cache"); hFile->AddRequestHeaders(L"Content-Type: application/x-www-form-urlencoded"); hFile->SendRequest(); char m_pBuffer[MAX_LENGTH]; // m_pBuffer中保存下载文件的内容 memset (m_pBuffer, 0, MAX_LENGTH); int m_iBuffer = 0; int iBytes = 0; while((iBytes = hFile->Read(m_pBuffer+m_iBuffer, MAX_LENGTH -m_iBuffer)) ) { m_iBuffer += iBytes; } hFile->Close(); pConn->Close(); session.Close(); } else { return FALSE; } } catch(CInternetException *pEx) { return FALSE; } 但是还是不行,,m_pBuffer里面好象没内容啊 不用这么复杂吧!unsigned char myData[10000]; CInternetSession mySession(NULL,0); CHttpFile* myHttpFile=NULL; //myHttpFile=(CHttpFile*)mySession.OpenURL(urlstr,1,INTERNET_FLAG_TRANSFER_BINARY); try { myHttpFile=(CHttpFile*)mySession.OpenURL("www.sina.com.cn",1,INTERNET_FLAG_TRANSFER_BINARY); } catch(CInternetException *e) { AfxMessageBox("不能打开指定网页"); mySession.Close(); return ; } unsigned long code; myHttpFile->QueryInfoStatusCode(code); long length = myHttpFile->GetLength(); CString title; CString name; title = myHttpFile->GetFileTitle(); name = myHttpFile->GetFileName(); CFile f; CFileException e; char* pFileName = "test.txt"; if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, &e ) ) { return ; } UINT nReturnCount; while(nReturnCount=myHttpFile->Read(myData,10000)) { f.Write(myData,nReturnCount); } myHttpFile->Close(); mySession.Close(); f.Close(); AfxMessageBox("读取网页完成"); 可以用一个很简单的函数!URLDownloadToFile就可以了!!!!!!!!! long length = myHttpFile->GetLength(); VC中实现在线版本检测http://www.xiaozhou.net/ReadNews.asp?NewsID=242 在本地比较文件的时间繁琐了一些,因为服务器上文件只有一个,如何更方便一点比较,升级?可否直接在http请求头中加入:if-Modified-Since这项然后根据服务器返回响应:200或(304)来判断 服务器上的文件 已经(没有)修改,从而(不)下载? 网赚是真的还是假的?有点玄乎 为什么贴的BMP图片会错位? 有关头文件的 多线程才能用select? 新手求救!(网络数据传输) ——————————COM接口函数声明中[in][out]的问题,来的朋友都有分!—————————— CListView中列表控件的拖放问题? 要做一个可以覆盖在CView上的窗口? 怎么做,各路高手请进... 刚才听了一些,句柄于ID,好象明白了,我的问题是.. 请问怎样得到另一个进程的句柄? 在VC++中如何调用Linux下的shell程序 高分求算法:用VC实现AES、RSA、ECC加密算法
CInternetSession session(_T("xxx"));
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,32000);
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES,10);
session.SetOption(INTERNET_OPTION_CONNECT_BACKOFF,1000);
CHttpConnection *pConn = session.GetHttpConnection("www.xxx.com");
CHttpFile *hFile = NULL;
hFile = pConn->OpenRequest("GET", "filename"); if(hFile)
{
hFile->AddRequestHeaders(L"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, */*");
hFile->AddRequestHeaders(L"Accept-Language: zh-cn");
hFile->AddRequestHeaders(L"Content-Type: application/x-www-form-urlencoded");
hFile->AddRequestHeaders(L"Accept-Encoding: gzip, deflate");
hFile->AddRequestHeaders(L"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)");
hFile->AddRequestHeaders(L"Connection: Keep-Alive");
hFile->AddRequestHeaders(L"Cache-Control: no-cache");
hFile->AddRequestHeaders(L"Content-Type: application/x-www-form-urlencoded"); hFile->SendRequest();
char m_pBuffer[MAX_LENGTH]; // m_pBuffer中保存下载文件的内容
memset (m_pBuffer, 0, MAX_LENGTH);
int m_iBuffer = 0;
int iBytes = 0;
while((iBytes = hFile->Read(m_pBuffer+m_iBuffer, MAX_LENGTH -m_iBuffer)) )
{
m_iBuffer += iBytes;
}
hFile->Close();
pConn->Close();
session.Close();
}
else
{
return FALSE;
}
}
catch(CInternetException *pEx)
{
return FALSE;
}
unsigned char myData[10000];
CInternetSession mySession(NULL,0);
CHttpFile* myHttpFile=NULL; //myHttpFile=(CHttpFile*)mySession.OpenURL(urlstr,1,INTERNET_FLAG_TRANSFER_BINARY);
try
{
myHttpFile=(CHttpFile*)mySession.OpenURL("www.sina.com.cn",1,INTERNET_FLAG_TRANSFER_BINARY);
}
catch(CInternetException *e)
{
AfxMessageBox("不能打开指定网页");
mySession.Close();
return ;
}
unsigned long code;
myHttpFile->QueryInfoStatusCode(code);
long length = myHttpFile->GetLength();
CString title;
CString name;
title = myHttpFile->GetFileTitle();
name = myHttpFile->GetFileName();
CFile f;
CFileException e;
char* pFileName = "test.txt";
if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, &e ) )
{
return ;
} UINT nReturnCount;
while(nReturnCount=myHttpFile->Read(myData,10000))
{
f.Write(myData,nReturnCount);
}
myHttpFile->Close();
mySession.Close();
f.Close();
AfxMessageBox("读取网页完成");
http://www.xiaozhou.net/ReadNews.asp?NewsID=242
可否直接在http请求头中加入:if-Modified-Since这项
然后根据服务器返回响应:200或(304)来判断 服务器上的文件 已经(没有)修改,从而(不)下载?