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;
}
查看全文
相关阅读:
十一. 图形、图像与多媒体8.多媒体基础
十一. 图形、图像与多媒体7.图像缓冲技术
十一. 图形、图像与多媒体6.图像处理基础
十一. 图形、图像与多媒体5.Graphics2D类的绘图方法
十一. 图形、图像与多媒体4.Graphics类的绘图方法
十一. 图形、图像与多媒体3.绘图模式
十一. 图形、图像与多媒体2.设置字型和颜色
十一. 图形、图像与多媒体1.绘图基础
十. 图形界面(GUI)设计14.键盘事件
linux 安装python3.6.10
原文地址:https://www.cnblogs.com/wubiyu/p/1255186.html
最新文章
移动适配参考
解析URL参数
javascript设计模式学习笔记四
javascript设计模式学习笔记三
使用git完成本地项目和远程仓库建立关系(Mac)
[Anglar]-使用ComponentFactoryResolver动态产生Component
javascript设计模式学习笔记二
js-闭包
javascript设计模式学习笔记一
HTML + CSS短标题(二,三,四文字长度)两端对齐的方式
热门文章
vue-cli创建uni-app项目 (一)
vue项目在ie下空白
跨域原理
uni-app使用vuex
新手配置阿里云云服务
mac更新后webstrom提示can't start git:usr/bin/git解决办法
git 操作解决办法
深入学习总结
angular5使用echart渐变色功能
十二. 网络与数据库编程1.IP地址和InetAddress类
Copyright © 2011-2022 走看看