讨论一下使用 WinInet.h 中的 API 来做一个 FTP 客户程序(VC)

CCED136 2002-05-08 03:17:49
现在,在 BCB 中作一个 FTP 的客户程序是一件很容易的事,因为 TNMFTP 这个
控件已经将很多的关于 FTP 的方法封装了,我们只需要简单的使用它就可以了。
问题是,使用 TNMFTP 做出来的程序体积较大。假如我要写一个 木马 程序,其中
会有从 FTP 服务器下载数据和文件的功能,这样大的体积肯定不行(编写出来的程
序就算使用压缩工具进行压缩,至少都还有 300K)。
因此,我想到了可以使用 API 进行编写,在 MSDN 中,介绍了使用 WinInet.h 中
的 API 来写 FTP 客户程序的方法,可惜没有例子。
希望有过这方面的经验的兄弟指点一二,没有写过的兄弟也来讨论讨论。
如果 100 分不够,我就在开贴加分。( BCB 或 VC 都行)

...全文
138 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
CCED136 2002-05-09
  • 打赏
  • 举报
回复
兄弟们,上3
CCED136 2002-05-08
  • 打赏
  • 举报
回复
问(tianlinyi(笨蛋) ):假如我的这个程序是一个木马,我并不知道中了该

木马的机器是否使用了代理,有没有一种方法可以让我们不关心代理的问题而正

常执行 FTP 的功能。以 WIN2000 的连接共享为例,能不能实现下面描述的这种

功能:不管用户是否通过代理,我们的程序都可以正常进行 FTP ?
CCED136 2002-05-08
  • 打赏
  • 举报
回复
问( tianlinyi(笨蛋) ): 我觉得,假如我的这个程序是个木马的话,我并不知道

中了我的木马的机器是否通过了代理上网。能不能有一种方法:不关心用户是否使

用了代理,都能正常完成 FTP 功能?
tianlinyi 2002-05-08
  • 打赏
  • 举报
回复
lpszProxyBypass
Long pointer to a null-terminated string that contains an optional list of host names or IP addresses, or both, that should not be routed through the proxy. The list can contain wildcards. Do not use an empty string, because InternetOpen will use it as the proxy bypass list. If this parameter specifies the "<local>" macro as the only entry, the function bypasses any host name that does not contain a period. If this parameter is NULL, the function reads the bypass list from the registry.
tianlinyi 2002-05-08
  • 打赏
  • 举报
回复
HINTERNET WINAPI InternetOpen(
LPCTSTR lpszAgent,
DWORD dwAccessType,
LPCTSTR lpszProxy,
LPCTSTR lpszProxyBypass,
DWORD dwFlags);
lpszProxy参数就是指的是代理。。。
如果你不指明的话,他会从你的注册表中读取代理服务器的信息,下面是具体说明。。。
lpszProxy
Long pointer to a null-terminated string that contains the name of the proxy server (or servers) to use if proxy access was specified. If this parameter is NULL, the function reads proxy information from the registry. Do not use an empty string, because InternetOpen will use it as the proxy name. The Win32 Internet functions only recognize CERN type proxies (HTTP only) and the TIS FTP gateway (FTP only). If Microsoft Internet Explorer is installed, the Win32 Internet functions also support SOCKS proxies. FTP requests can be made through a CERN type proxy either by changing them to an HTTP request or by using InternetOpenUrl.
CCED136 2002-05-08
  • 打赏
  • 举报
回复
问: 此时,您的代码中,并没有涉及到关于代理的问题,假如需要通过代理的

话(以 win2000 中的连接共享为例),需要注意些什么呢?
tianlinyi 2002-05-08
  • 打赏
  • 举报
回复
To access an FTP server with WinInet

Call InternetOpen to initialize an Internet handle.
InternetOpen creates the root HINTERNET handle that is used to establish the FTP session. The HINTERNET Internet handle is used by all subsequent functions.

Call InternetConnect to create an FTP session.
When calling InternetConnect specify INTERNET_DEFAULT_FTP_PORT for the nServerPort parameter and INTERNET_SERVICE_FTP for the dwService parameter.

This function uses the handle returned by InternetOpen to create a specific FTP session. InternetConnect initializes an FTP session for the specified site, using the arguments passed to it and creates HINTERNET that is a branch off the root handle. In the case of an FTP session, InternetConnect attempts to establish a connection to the specified site.

Call FtpGetFile or FtpFindFirstFile.
InternetConnect returns a handle that subsequent functions can use, such as FtpGetFile or FtpFindFirstFile.

Use the InternetFindNextFile function with FtpFindFirstFile to find the next file in a file search, using the search parameters and HINTERNET handle from FtpFindFirstFile.

To complete a file search, continue to call InternetFindNextFile using the HINTERNET handle returned by FtpFindFirstFile until function fails with the extended error message ERROR_NO_MORE_FILES. To get the extended error data, call the GetLastError function.

Call InternetCloseHandle to close the FTP session created by calling InternetConnect.
Call InternetCloseHandle to close the handle created by calling InternetOpen.
Note Applications must specify a directory relative to the current directory or include the full directory path.

tianlinyi 2002-05-08
  • 打赏
  • 举报
回复
我上面的代码是我做的用wininet,http协议实现断点续传,多线程下载的软件里面的小部分代码。。。。

HINTERNET hConnect=::InternetConnect(hSession,//当前internet会话句柄
m_strRemoteServer,//server name
INTERNET_INVALID_PORT_NUMBER,
NULL,//"",//user name
"",//password
INTERNET_SERVICE_HTTP,//Type of service to access
0,
0);

你把上面的INTERNET_SERVICE_HTTP改为INTERNET_SERVICE_FTP就可以利用ftp服务了,然后利用ftp的几个函数进行ftp连接,ftp下载
tianlinyi 2002-05-08
  • 打赏
  • 举报
回复
HINTERNET hSession=::InternetOpen("Mozilla 4.0",INTERNET_OPEN_TYPE_PRECONFIG ,
"",INTERNET_INVALID_PORT_NUMBER,0);

if(hSession == NULL){
AfxMessageBox("Session Initialization failed!",MB_OK,0);
VERIFY(::InternetCloseHandle(hSession));
return FALSE;
}

HINTERNET hConnect=::InternetConnect(hSession,//当前internet会话句柄
m_strRemoteServer,//server name
INTERNET_INVALID_PORT_NUMBER,
NULL,//"",//user name
"",//password
INTERNET_SERVICE_HTTP,//Type of service to access
0,
0);

if(hConnect == NULL){
AfxMessageBox("Connect Initialization failed!",MB_OK,0);
VERIFY(::InternetCloseHandle(hSession));
}

LPCTSTR sType = "*/*";
LPCTSTR *pFileType = &sType;
HINTERNET hHttpFile=::HttpOpenRequest(hConnect,
"GET",
m_strRemotePath,
"HTTP/1.1",
NULL,
pFileType,
INTERNET_FLAG_DONT_CACHE,
0);

if(hHttpFile == NULL){
AfxMessageBox("HttpFile Initialization failed!",MB_OK,0);
VERIFY(::InternetCloseHandle(hConnect));
VERIFY(::InternetCloseHandle(hSession));
}

BOOL bSendRequest = ::HttpSendRequest(hHttpFile,
NULL,
0,
0,
0);

if(bSendRequest)
{
char chQueryBuf[1024];

DWORD dwQueryBufLen = sizeof(chQueryBuf);

BOOL bQuery;
bQuery = ::HttpQueryInfo(hHttpFile,
HTTP_QUERY_RAW_HEADERS_CRLF,
(LPVOID)chQueryBuf,
&dwQueryBufLen,
NULL);

/* if (GetLastError()==ERROR_HTTP_HEADER_NOT_FOUND)
{
// Code to handle the case where the header isn't available.
AfxMessageBox("ERROR_HTTP_HEADER_NOT_FOUND");
}
else
{
// Check for an insufficient buffer.
if (GetLastError()==ERROR_INSUFFICIENT_BUFFER)
{
AfxMessageBox("ERROR_INSUFFICIENT_BUFFER");
}
} */


DWORD dwStatusCode; //response status code

if(!bQuery)
AfxMessageBox("发送请求失败!");
else
{

bQuery = ::HttpQueryInfo(hHttpFile,
HTTP_QUERY_CONTENT_LENGTH,
chQueryBuf,
&dwQueryBufLen,
NULL);
if(bQuery)
{
m_dwFileSize = (DWORD)atol(chQueryBuf);
}
else
{
m_dwFileSize = 10*1024;
}
bQuery = ::HttpQueryInfo(hHttpFile,
HTTP_QUERY_STATUS_CODE,
(LPVOID)chQueryBuf,
&dwQueryBufLen,
NULL);
dwStatusCode = (DWORD)atol(chQueryBuf);
if(dwStatusCode > 400 && dwStatusCode <499) //400-499:Request Error
AfxMessageBox("发送请求失败!");
else if(dwStatusCode > 500 && dwStatusCode < 599) //500-599:Server Error
AfxMessageBox("服务器错误!");
else
{
CString strRange;
DWORD dwHeaderLength;

hHttpFile=::HttpOpenRequest(hConnect,
"GET",
m_strRemotePath,
"HTTP/1.1",
NULL,
pFileType,
INTERNET_FLAG_DONT_CACHE,
0);
strRange.Format("Range: bytes=%d-%d\r\n", 0, 10);
dwHeaderLength=(DWORD)strRange.GetLength();
BOOL bSendRequest = ::HttpSendRequest(hHttpFile,
(LPCTSTR)strRange,
dwHeaderLength,
0,
0);
bQuery = ::HttpQueryInfo(hHttpFile,
HTTP_QUERY_RAW_HEADERS_CRLF,
(LPVOID)chQueryBuf,
&dwQueryBufLen,
NULL);
::HttpQueryInfo(hHttpFile,
HTTP_QUERY_STATUS_CODE,
(LPVOID)chQueryBuf,
&dwQueryBufLen,
NULL);
dwStatusCode = (DWORD)atol(chQueryBuf);
if(dwStatusCode == 206) //支持断点续传
m_bSupportResume = TRUE;
m_strLocalFile += m_strFileName;
m_strTempFile = m_strLocalFile;
m_strTempFile += ".bot";
}
}
}

VERIFY(::InternetCloseHandle(hConnect));
VERIFY(::InternetCloseHandle(hSession));
hSession = NULL;
hConnect = NULL;
hHttpFile = NULL;

return TRUE;
CCED136 2002-05-08
  • 打赏
  • 举报
回复
I know SOCKET programming. I know of the RFC !

but I don't know how to use the API to make FTP program.
tianlinyi 2002-05-08
  • 打赏
  • 举报
回复
wininet我用过,我作的是http协议下载文件的
支持多线程,支持断点续传
就是那个httpQueryInfo函数特别奇怪
tianlinyi 2002-05-08
  • 打赏
  • 举报
回复
wininet我用过,我作的是http协议下载文件的
支持多线程,支持断点续传
就是那个httpQueryInfo函数特别奇怪
ndy_w 2002-05-08
  • 打赏
  • 举报
回复
the information u do need is in the rfc. and u must know socket programming(stream).

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧