需要
object | content |
---|---|
Header | Wininet.h |
Library | Wininet.lib |
DLL | Wininet.dll |
变量声明
BOOL bSuccess ;
HINTERNET hIntSession, hFtpSession, hFind ;
HANDLE hLocalFind ;
WIN32_FIND_DATA finddata ;
Open an internet session
hIntSession = InternetOpen (CTelProxyApp::szTitle, INTERNET_OPEN_TYPE_DIRECT,
NULL, NULL, 0) ;
if(hIntSession == NULL)
{
DWORD dwError = GetLastError();
ErrLog(<<"InternetOpen error "<<dwError);
return;
}
Open an FTP session
hFtpSession = InternetConnect(hIntSession, m_szFTPServer,
INTERNET_DEFAULT_FTP_PORT,
m_szUser, m_szPassword, INTERNET_SERVICE_FTP, 0, 0);
if(hFtpSession == NULL)
{
DWORD dwError = GetLastError();
ErrLog(<<"InternetConnect error "<<dwError);
InternetCloseHandle (hIntSession) ;
return;
}
ftp远程路径设置
//根目录为"/"
bSuccess = FtpSetCurrentDirectory (hFtpSession, m_szDirectory) ;
if(!bSuccess)
{
DWORD dwLastError = GetLastError();
if (dwLastError == ERROR_INTERNET_EXTENDED_ERROR) {
char szLastResponse[1024] = { 0 };
DWORD lastErrorResponse = 0;
DWORD bufSize = sizeof(szLastResponse);
if (InternetGetLastResponseInfo(&lastErrorResponse, szLastResponse, &bufSize))
{
//如果错误类似于550 /0: No such file or directory.
if(memcmp(szLastResponse,"550",3) == 0)
{
bSuccess = FtpCreateDirectory (hFtpSession, m_szDirectory) ;
bSuccess = FtpSetCurrentDirectory (hFtpSession, m_szDirectory) ;
if(!bSuccess)
{
ErrLog(<<szLastResponse<<"Cannot set directory to "<<m_szDirectory);
InternetCloseHandle (hFtpSession) ;
InternetCloseHandle (hIntSession) ;
return;
}
}else{
ErrLog(<<"Cannot set directory to "<<m_szDirectory);
InternetCloseHandle (hFtpSession) ;
InternetCloseHandle (hIntSession) ;
return;
}
}
else //if InternetGetLastResponseInfo() failed
{
ErrLog(<<"Cannot set directory to "<<m_szDirectory);
InternetCloseHandle (hFtpSession) ;
InternetCloseHandle (hIntSession) ;
return;
}
}
}
获得ftp远程路径
char szCurrentDirectory[MAX_PATH] = {0};
DWORD dwLen = MAX_PATH;
bSuccess = FtpGetCurrentDirectory(hFtpSession, szCurrentDirectory, &dwLen);
if(!bSuccess)
{
DWORD dwLastError = GetLastError(); //set dwLastError to GetLastError()
ErrLog(<<"Cannot set directory to "<<m_szDirectory);
InternetCloseHandle (hFtpSession) ;
InternetCloseHandle (hIntSession) ;
return;
}
本地路径遍历上传
hLocalFind = ::FindFirstFile(CApp::getRecordFileTemplate().c_str(),&finddata);
if(hLocalFind == INVALID_HANDLE_VALUE)
{
InternetCloseHandle (hFtpSession) ;
InternetCloseHandle (hIntSession) ;
return;
}
do
{
bSuccess = FtpPutFile(hFtpSession,(CApp::getRecordFileDirectory()+=finddata.cFileName).c_str(),
finddata.cFileName,FTP_TRANSFER_TYPE_BINARY, 0);
if(!bSuccess)
{
DWORD dwErr = GetLastError();
ErrLog(<<"upload file to ftp server fail.error "<<dwErr);
InternetCloseHandle (hFtpSession) ;
InternetCloseHandle (hIntSession) ;
return;
}
DebugLog(<<"upload "<<finddata.cFileName);
}while(FindNextFile(hLocalFind,&finddata));
远程路径遍历下载
hFind = FtpFindFirstFile (hFtpSession, TEMPLATE,
&finddata, 0, 0) ;
if (hFind == NULL)
{
InternetCloseHandle (hFtpSession) ;
InternetCloseHandle (hIntSession) ;
return;
}
do
{
FtpGetFile (hFtpSession,
finddata.cFileName, finddata.cFileName, TRUE,
FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0) ;
}
while (InternetFindNextFile (hFind, &finddata)) ;
完成销毁对象
InternetCloseHandle (hFind) ;
InternetCloseHandle (hFtpSession) ;
InternetCloseHandle (hIntSession) ;
注意
You should call theGetLastError function immediately when a function’s return value indicates that such a call will return useful data.
源文档 https://msdn.microsoft.com/en-us/library/windows/desktop/ms679360(v=vs.85).aspx
否则遇到错误,调用GetLastError查看错误码返回0.我之前没注意这个问题,日志输出放在了GetlastError前面,结果ftp函数返回FALSE,查看GetLastError的错误码为0,颇是束手无策。
参考
? <<Windows程序设计>> WinInet和FTP
? <<Visual C++ 网络高级编程>> FTP协议的实现
? WinINet Functions
时间: 2024-10-16 19:52:46