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

3 篇文章 0 订阅

需要

objectcontent
HeaderWininet.h
LibraryWininet.lib
DLLWininet.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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值