本文共 3207 字,大约阅读时间需要 10 分钟。
object | content |
---|---|
Header | Wininet.h |
Library | Wininet.lib |
DLL | Wininet.dll |
BOOL bSuccess ;HINTERNET hIntSession, hFtpSession, hFind ;HANDLE hLocalFind ;WIN32_FIND_DATA finddata ;
hIntSession = InternetOpen (CTelProxyApp::szTitle, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0) ;if(hIntSession == NULL){ DWORD dwError = GetLastError(); ErrLog(<<"InternetOpen error "<
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 "<
//根目录为"/"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(<<<"Cannot set directory to "<
}
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 "<
}
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 "<
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.
源文档
否则遇到错误,调用GetLastError查看错误码返回0.我之前没注意这个问题,日志输出放在了GetlastError前面,结果ftp函数返回FALSE,查看GetLastError的错误码为0,颇是束手无策。
• <> WinInet和FTP• < > FTP协议的实现• WinINet Functions
转载地址:http://ewppi.baihongyu.com/