zoukankan
html css js c++ java
[转]使用MFC提供的Http类下载和上传文件
使用MFC提供的Http类下载和上传文件
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
=
2000
;
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut);
//
重试之间的等待延时
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES,
1
);
//
重试次数
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[
1000
];
//
返回消息
DWORD dwFileSize
=
0
;
//
文件长度
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
=
1024
*
10
;
pszBuffer
=
new
char
[BUFFER_LENGTH];
//
读取文件的缓冲
DWORD dwWrite, dwTotalWrite;
dwWrite
=
dwTotalWrite
=
0
;
UINT nRead
=
pHttpFile
->
Read(pszBuffer, BUFFER_LENGTH);
//
读取服务器上数据
while
(nRead
>
0
)
{
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
=
0
;
CString strServer;
CString strObject;
INTERNET_PORT wPort
=
0
;
DWORD dwFileLength
=
0
;
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
<=
0
)
return
FALSE;
pFileBuff
=
new
char
[dwFileLength];
memset(pFileBuff,
0
,
sizeof
(
char
)
*
dwFileLength);
file.Read(pFileBuff, dwFileLength);
const
int
nTimeOut
=
5000
;
cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut);
//
联接超时设置
cis.SetOption(INTERNET_OPTION_CONNECT_RETRIES,
1
);
//
重试1次
pHC
=
cis.GetHttpConnection(strServer, wPort);
//
取得一个Http联接
pHF
=
pHC
->
OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);
if
(
!
pHF
->
SendRequest(NULL,
0
, pFileBuff, dwFileLength))
{
delete[]pFileBuff;
pFileBuff
=
NULL;
pHF
->
Close();
pHC
->
Close();
cis.Close();
return
FALSE;
}
DWORD dwStateCode
=
0
;
pHF
->
QueryInfoStatusCode(dwStateCode);
if
(dwStateCode
==
HTTP_STATUS_OK)
bResult
=
TRUE;
}
catch
(CInternetException
*
pEx)
{
char
sz[
256
]
=
""
;
pEx
->
GetErrorMessage(sz,
25
);
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;
}
查看全文
相关阅读:
解释中国经济奇迹的5本书
《门口的野蛮人》三部曲,美国的宝万之争专业户
3星|《城市更新》:欧美东亚中国老城区改造的资料与案例汇编
3.5星|《哈佛商业评论》2018年第11期:机场的书店很危险,出差的高管可能会被商业新书主张吸引
3星|《好文案一句话就够了》:10年前一些日本经典广告文案
5星|林毅夫《解读中国经济》:完美解释中国经济奇迹和现存问题
iOS:Masonry 英文原档介绍
iOS:Masonry介绍与使用
iOS :学习新技术途径和sizeClasses屏幕适配
iOS:XMPP即时聊天知识
原文地址:https://www.cnblogs.com/wubiyu/p/1255186.html
最新文章
junit4 javaee 5.0 jpa SSH 单元测试问题集锦
ttttttttttt
win7 64位安装redis 及Redis Desktop Manager使用
maven Connection refused: connect
Eclipse用Tomcat插件部署Java Web项目
Memcached使用入门
Linux软连接和硬链接
LeetCode85 Maximal Rectangle java题解
sqlite数据库改动及升级
MySQL双主热备问题处理
热门文章
兔子--html,js,php,ASP,ASP.NET,JSP的关系
at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:)
辛星深入分析vim的自己主动补全功能以及vim的映射
HDU ACM 1073 Online Judge ->字符串水题
kettle中使用javascript步骤和fireToDB函数实现自己定义数据库查询
Eoeclient源代码分析---SlidingMenu的使用
4星|《环球科学》2018年11月号:许多高水平科研结果无法重复
2.5星|《后谷歌时代》:鼓吹区块链比特币必将战胜谷歌,旁征博引难掩逻辑与证据的不足
2星|《商业模式进化论》:封面上作者都不敢标注“著”只敢说“编著”,粗制滥造
3.5星|《魔球》:美国棒球穷队数据分析挑选球员战胜富队的故事
Copyright © 2011-2022 走看看