1、下载文件

Download(const CString& strFileURLInServer, //待下载文件的URL
const CString & strFileLocalFullPath)//存放到本地的路径
{
ASSERT(strFileURLInServer != "");
ASSERT(strFileLocalFullPath != "");
CInternetSession session;
CHttpConnection* pHttpConnection = NULL;
CHttpFile* pHttpFile = NULL;
CString strServer, strObject;
INTERNET_PORT wPort; DWORD dwType;
const int nTimeOut = ;
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); //重试之间的等待延时
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, ); //重试次数
char* pszBuffer = NULL; try
{
AfxParseURL(strFileURLInServer, dwType, strServer, strObject, wPort);
pHttpConnection = session.GetHttpConnection(strServer, wPort);
pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject);
if(pHttpFile->SendRequest() == FALSE)
return false;
DWORD dwStateCode; pHttpFile->QueryInfoStatusCode(dwStateCode);
if(dwStateCode == HTTP_STATUS_OK)
{
HANDLE hFile = CreateFile(strFileLocalFullPath, GENERIC_WRITE,
FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
NULL); //创建本地文件
if(hFile == INVALID_HANDLE_VALUE)
{
pHttpFile->Close();
pHttpConnection->Close();
session.Close();
return false;
} char szInfoBuffer[]; //返回消息
DWORD dwFileSize = ; //文件长度
DWORD dwInfoBufferSize = sizeof(szInfoBuffer);
BOOL bResult = FALSE;
bResult = pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,
(void*)szInfoBuffer,&dwInfoBufferSize,NULL); dwFileSize = atoi(szInfoBuffer);
const int BUFFER_LENGTH = * ;
pszBuffer = new char[BUFFER_LENGTH]; //读取文件的缓冲
DWORD dwWrite, dwTotalWrite;
dwWrite = dwTotalWrite = ;
UINT nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH); //读取服务器上数据 while(nRead > )
{
WriteFile(hFile, pszBuffer, nRead, &dwWrite, NULL); //写到本地文件
dwTotalWrite += dwWrite;
nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH);
} delete[]pszBuffer;
pszBuffer = NULL;
CloseHandle(hFile);
}
else
{
delete[]pszBuffer;
pszBuffer = NULL;
if(pHttpFile != NULL)
{
pHttpFile->Close();
delete pHttpFile;
pHttpFile = NULL;
}
if(pHttpConnection != NULL)
{
pHttpConnection->Close();
delete pHttpConnection;
pHttpConnection = NULL;
}
session.Close();
return false;
}
}
catch(...)
{
delete[]pszBuffer;
pszBuffer = NULL;
if(pHttpFile != NULL)
{
pHttpFile->Close();
delete pHttpFile;
pHttpFile = NULL;
}
if(pHttpConnection != NULL)
{
pHttpConnection->Close();
delete pHttpConnection;
pHttpConnection = NULL;
}
session.Close();
return false;
} if(pHttpFile != NULL)
pHttpFile->Close();
if(pHttpConnection != NULL)
pHttpConnection->Close();
session.Close();
return true;
}

2、上传文件

UploadFile(LPCTSTR strURL, //负责接收上传操作的页面的URL
LPCTSTR strLocalFileName) //待上传的本地文件路径
{
ASSERT(strURL != NULL && strLocalFileName != NULL); BOOL bResult = FALSE;
DWORD dwType = ;
CString strServer;
CString strObject;
INTERNET_PORT wPort = ;
DWORD dwFileLength = ;
char * pFileBuff = NULL; CHttpConnection * pHC = NULL;
CHttpFile * pHF = NULL;
CInternetSession cis; bResult = AfxParseURL(strURL, dwType, strServer, strObject, wPort);
if(!bResult)
return FALSE;
CFile file;
try
{
if(!file.Open(strLocalFileName, CFile::shareDenyNone | CFile::modeRead))
return FALSE;
dwFileLength = file.GetLength();
if(dwFileLength <= )
return FALSE;
pFileBuff = new char[dwFileLength];
memset(pFileBuff, , sizeof(char) * dwFileLength);
file.Read(pFileBuff, dwFileLength); const int nTimeOut = ;
cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); //联接超时设置
cis.SetOption(INTERNET_OPTION_CONNECT_RETRIES, ); //重试1次
pHC = cis.GetHttpConnection(strServer, wPort); //取得一个Http联接 pHF = pHC->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);
if(!pHF->SendRequest(NULL, , pFileBuff, dwFileLength))
{
delete[]pFileBuff;
pFileBuff = NULL;
pHF->Close();
pHC->Close();
cis.Close();
return FALSE;
}
DWORD dwStateCode = ;
pHF->QueryInfoStatusCode(dwStateCode); if(dwStateCode == HTTP_STATUS_OK)
bResult = TRUE;
} catch(CInternetException * pEx)
{
char sz[] = "";
pEx->GetErrorMessage(sz, );
CString str;
str.Format("InternetException occur!\r\n%s", sz);
AfxMessageBox(str);
}
catch(CFileException& fe)
{
CString str;
str.Format("FileException occur!\r\n%d", fe.m_lOsError);
AfxMessageBox(str);
}
catch(...)
{
DWORD dwError = GetLastError();
CString str;
str.Format("Unknow Exception occur!\r\n%d", dwError);
AfxMessageBox(str);
} delete[]pFileBuff;
pFileBuff = NULL;
file.Close();
pHF->Close();
pHC->Close();
cis.Close();
return bResult;
}

使用MFC提供的Http类下载和上传文件的更多相关文章

  1. 2.3 利用FTP服务器下载和上传文件

    二.利用FTP服务器的下载文件 from ftplib import FTP from os.path import exists def getfile(file,site,dir,user=(), ...

  2. 在XShell中使用sz和rz命令下载和上传文件

    借助XShell,使用linux命令sz可以很方便的将服务器上的文件下载到本地,使用rz命令则是把本地文件上传到服务器 工具/原料   XShell CentOS 6.5 使用sz下载文件   1 输 ...

  3. window系统使用tftp下载和上传文件

    安装tftp32服务器 首先需要安装tftp服务器:tftpd32 , 下载以后的目录如下: tftp使用帮助 命令提示符(cmd): 直接运行tftpd32.exe tftp命令的用法: 关于tft ...

  4. python+selenium下载和上传文件

    操作浏览器上传文件,先看代码 1 """ 2 * send_keys() 指定文件上传路径. 3 """ 4 from selenium i ...

  5. ansible 通过网络下载和上传文件

    1.通过http下载文件,并且不验证证书 - name: download files by https get_url: url: https://robin.org.cn/test.zip des ...

  6. 重新想象 Windows 8 Store Apps (66) - 后台任务: 下载和上传

    [源码下载] 重新想象 Windows 8 Store Apps (66) - 后台任务: 下载和上传 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后台任务 后台 ...

  7. 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性

    [源码下载] 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性 作者:webabcd 介 ...

  8. github下载和上传项目

    git下载和上传项目 下载: git clone +地址 上传: 1.git init 在当前项目的目录中生成本地的git管理(多一个.git文件夹,为隐藏文件) 2.git add .(注意最后面有 ...

  9. [实战]MVC5+EF6+MySql企业网盘实战(12)——新建文件夹和上传文件

    写在前面 之前的上传文件的功能,只能上传到根目录,前两篇文章实现了新建文件夹的功能,则这里对上传文件的功能进行适配. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战] ...

随机推荐

  1. mysql root 密码丢失问题

    root密码丢失,我们需要将mysql以不检查权限的方式重新启动. 在mysql的配置文件(/etc/my.cnf)中,[mysqld]下添加一句skip-grant-tables,然后重新启动服务, ...

  2. hadoop中master免登录slave

    hadoop集群免登录配置 在主机master上执行如下: 1. $cd ~/.ssh(如果没有此目录,可以手动创建) 2. $ssh-keygen -t rsa  ----------------- ...

  3. [R] 回归拟合

    如下示例 > fit <- lm(y~x, data = data01) > summary(fit) Call: lm(formula = data01$P ~ data01$M, ...

  4. PHP 使用CURL

    CURL是一个非常强大的开源库,支持很多协议,包括HTTP.FTP.TELNET等,我们使用它来发送HTTP请求.它给我 们带来的好处是可以通过灵活的选项设置不同的HTTP协议参数,并且支持HTTPS ...

  5. 【kd-tree】bzoj1941 [Sdoi2010]Hide and Seek

    枚举每个点,计算离他最近的和最远的点. #include<cstdio> #include<cmath> #include<algorithm> using nam ...

  6. Android性能优化方法(八)

    Android SDK tools目录下提供一个观察布局的工具,层级观察器(Hierarchy Viewer).Hierarchy Viewer工具是一个非常好的布局优化工具,同时,你也可以通过它学习 ...

  7. Java EE 参考文档及sample

    http://docs.oracle.com/javaee/6/tutorial/doc/ https://svn.java.net/svn/javaeetutorial~svn/ 检索: site: ...

  8. MVVM了解

    了解WPF要有两年,零零碎碎也做了几个项目,之前面试的时候面试官必问对MVVM的了解. 其实不太了解,只是做项目的时候一直采用这种模式,Model-View-ViewModel.以下是我在了解过程中的 ...

  9. Neither BindingResult nor plain target object for bean

    当你开发一个项目,如果你选择的是spring MVC 框架,而你在前台使用spring的标签时,那么你有可能出现在这个情况. javax.servlet.jsp.JspTagException: Ne ...

  10. linux tcp协议状态机

    截图来自百度文库 TCP状态-有限状态机