码迷,mamicode.com
首页 > Windows程序 > 详细

充满梦想的FTP探索之旅(二)WinInet和FTP

时间:2015-05-07 16:52:02      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:wininet   c++   ftp   

需要

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

充满梦想的FTP探索之旅(二)WinInet和FTP

标签:wininet   c++   ftp   

原文地址:http://blog.csdn.net/soliddream66/article/details/45561005

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!