使用WinInet函数简单实现FTP客户端

#include <WinInet.h>
#pragma comment(lib, "WinInet.lib")

bool FTPUpload(const string& strFtpHost, int nPort, const string &strUser, const string &strPwd,
   const string& strLocalFile, const string& strFtpFile, string &strMsg)
{
BOOL dRes,pRes;
HINTERNET hInternet;
HINTERNET hConnect;

const char *pFtpHost = strFtpHost.c_str();
if (nPort==0)
{
nPort = INTERNET_DEFAULT_FTP_PORT;
}
const char *pFtpUser = strUser.c_str();
const char *pFtpPwd = strPwd.c_str();
const char *pLocalFile = strLocalFile.c_str();
const char *pFtpFile = strFtpFile.c_str();

hInternet = InternetOpen("DataClient", INTERNET_OPEN_TYPE_DIRECT,NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE);
if ( NULL == hInternet )
//printf("InternetOpen Error:%d ", GetLastError() );
strMsg = "InternetOpen Error";
return false;
}

hConnect  = InternetConnect(hInternet, pFtpHost, nPort, pFtpUser, pFtpPwd, INTERNET_SERVICE_FTP, 
INTERNET_FLAG_EXISTING_CONNECT || INTERNET_FLAG_PASSIVE,0 );
if ( NULL == hInternet )
{
//printf( "InternetConnect Error:%d ", GetLastError() );
strMsg = "InternetConnect Error";
InternetCloseHandle(hInternet);
return false;
}

//dRes = FtpGetFile(hConnect, "./usera/123.txt", "D:\\ftp123.txt", FALSE, FILE_ATTRIBUTE_ARCHIVE, FTP_TRANSFER_TYPE_UNKNOWN, 0);
//if ( dRes == 0 )
//{
// printf( "FtpGetFile Error: ", GetLastError() );
//}

pRes = FtpPutFile(hConnect,pLocalFile,pFtpFile,FTP_TRANSFER_TYPE_BINARY,0);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);

if(pRes==0)
{
//printf("上传文件失败! ");
strMsg = "FtpPutFile Error";
return false;
}

strMsg="上次成功";
return true;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值