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. poi管道流的导入导出

    /** * 导入学生信息 * * @param classid * @param uploadFilePath * @return */ public boolean uploadStudentFil ...

  2. redis pipeline

    redis pipeline 简而言之就是把多个redis命令打包,一起发送给redis server,并且一起返回结果,减少客户端和服务器之间的多次“折返跑”

  3. Oracle 截取、查找字符函数(持续更新)

    整理一些常用的截取.查找字符函数: 1.查找某一个字符串中某一个字符(串)出现的次数 SELECT LENGTH(REGEXP_REPLACE(REPLACE('anne<br>lily& ...

  4. python 2.X

    from BaseHTTPServer import BaseHTTPRequestHandler

  5. [[4], [5, 6, 7]](Python)list的方法

    现在我们要学习一些Python的数据结构了,本节将主要学习列表(list)的用法 1.list的方法 list.append(x) 在list的末尾添加一个元素 >>> a=[1,2 ...

  6. getline函数的用法

    函数声明 bool getline(istream &in, string &s) 功能说明: 从输入流读入一行到变量string s,及时是空格也可以读入. –直到出现以下情况为止: ...

  7. Nginx + tornado + supervisor部署

    参考链接:supervisor + Tornado + Nginx 使用详解, 用tornado ,Supervisord ,nginx架网站, tornado官方文档 项目文档树: . ├── ch ...

  8. 【区间dp】codevs1966 乘法游戏

    f(i,j)=min{f(i,k)+f(k,j)+a[i]*a[k]*a[j]}(1<=i<=j<=n,i<k<j) #include<cstdio> #in ...

  9. 『TCP/IP详解——卷一:协议』读书笔记——04

    2013-08-18 16:31:17 第2章 链路层 2.1 引言 链路层主要有三个目的: 为IP模块发送和接受IP数据报 为ARP模块发送ARP请求和接受ARP应答 为RARP发送RARP请求和接 ...

  10. kafka go producer 启动基本配置

    1.官网上下载kafka安装包:http://kafka.apache.org/downloads.html 2.执行命令运行zookeeper 实例(单点): bin/zookeeper-serve ...