Common HTTP and FTP WinInet APIs

依赖专家资源的传统机构与依赖数据算法的现代平台,在转化效率上谁更胜一筹?.docx 依赖专家资源的传统机构与依赖数据算法的现代平台,在转化效率上谁更胜一筹? 立即下载
 

This chapter covers most used WinInet APIs with sample code. I don't see any use of Gopher protocol these days, so I will skip that part for now.

WinInet HTTP APIs

Here is WinInet HTTP APIs hierarchy. To use an Http API, you have to go through the heirarchy. 



Before using any HTTP functions, you must be aware of InternetConnect funtion. Here is syntax for InternetConnect.

HINTERNET InternetConnect(IN HINTERNET hInternetSession, IN LPCSTR lpszServerName, IN INTERNET_PORT nServerPort, IN LPCSTR lpszUsername, IN LPCSTR lpszPassword, IN DWORD dwService, IN DWORD dwFlags, IN DWORD dwContext );

Where hInternetSession is handle retured by InternetOpen. Parameter lpszServerName is name of the server (a host name or host IP). The next parameter, nServerPort allows you to specify a port number. Here are default values for this parameter:

nServerPortValue Description
INTERNET_DEFAULT_FTP_PORTUses the default port for FTP server, port 21.
INTERNET_DEFAULT_GOPHER_PORTUses the default port for Gopher server, port 70.
INTERNET_DEFAULT_HTTP_PORTUses the default port for HTTP server, port 80.
INTERNET_DEFAULT_HTTPS_PORTUses the default port for FTP server or HTTPS, port 443.
INTERNET_DEFAULT_SOCKS_PORTUses the default port for Socks firewall servers, port 1080.
INTERNET_INVALID_PORT_NUMBERUses the default port for the service specified by dwService

Parameter lpszUsername and lpszPassword are UserID and passwords. Next parameter, dwService has three values:

dwServiceValue Description
INTERNET_SERVICE_FTPFTP service.
INTERNET_SERVICE_GOPHERGopher service.
INTERNET_SERVICE_HTTPHTTP service.

Here is an example:

HINTERNET hConnection = InternetConnect( hSession, "
www.dotnetheaven.com", INTERNET_DEFAULT_HTTP_POST, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0 );

Here is a list of HTTP APIs:

HTTP APIsDescription
HttpOpenRequest Opens an HTTP request. You need to pass Internet connection as an input parameter to this function.
HTTPQueryInfo Queries information about a request.
InternetErrorDlg Displays predefined dialog for common Internet errors.
HttpAddRequestHeaders Adds HTTP request headers to the HTTP request handle.
HttpSendRequestSends actual request to the server. This request may be GET or POST.

HttpOpenRequest

This is the first HTTP function, which should be called after InternetConnect. To open an HTTP connection, InternetConnect must pass its third parameter value as INTERNET_DEFAULT_HTTP_PORT. This function sends a request to get or retrieve data depends on the method. Here is syntax:

HINTERNET HttpOpenRequest(IN HINTERNET hHttpSession,IN LPCSTR lpszVerb,    IN LPCSTR lpszObjectName,IN LPCSTR lpszVersion,IN LPCSTR lpszReferer,    IN LPCSTR FAR * lpszAcceptTypes,IN DWORD dwFlags,IN DWORD dwContext);

First line of every HTTP request is a combination of three elements: Method, URI, and Protocol Version. Three parameters lpszVerb, lpszObjectName, and lpszVersion of HttpOpenRequest make first line of HTTP request.

Parameters Description
hHttpSession Handle to the HTTP session returned by InternetConnect.
lpszVerb Address of string containing HTTP method. If you pass NULL, default method is GET.
lpszObjectName URI
lpszVersion Protocol version. If you pass NULL, default is HTTP/1.0.
lpszReferer 
lpszAcceptTypes 
dwFlags 
dwContext HTTP service.

Here is how to use this function:

HINTERNET hRequest = HttpOpenRequest( hConnection, "GET", "", NULL, NULL, INTERNET_FLAG_RELOAD, 0  );

We will see all the APIs in out example. For more details, you can see MSDN.

HttpSendRequest

Another important HTTP function is HttpSendRequest. This function actually sends a request to Http server. This request may be to post or to get data from the server. It depends on the HttpOpenRequest handle.

Syntax:

BOOL HttpSendRequest( IN HINTERNET hHttpRequest, IN LPCSTR lpszHeaders,    IN DWORD dwHeadersLength, IN LPVOID lpOptional, DWORD dwOptionalLength);

Parameters Description
hHttpRequest Handle of HttpOpenReqest
lpszHeaders NULL
dwHeaderLength 0
lpOptional NULL
dwOptionLength 0

Here is how to use this function:

HttpSendRequest( hData, NULL, 0, NULL, 0);

Example: Now let's see how to use all these functions in our example. This example shows how to download contents of a page using HTTP API functions.

HINTERNET hINet, hConnection, hData;
CHAR buffer[2048] ;
CString m_strContents ;
DWORD dwRead, dwFlags, dwStatus ;
hINet = InternetOpen("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
if ( !hINet )
{
AfxMessageBox("InternetOpen Failed");
return;
}
try
{
hConnection = InternetConnect( hINet, "
www.dotnetheaven.com", 80, " "," ", INTERNET_SERVICE_HTTP, 0, 0 );
if ( !hConnection )
{
InternetCloseHandle(hINet);
return;
}
// Get datahData = HttpOpenRequest( hConnection, "GET", "/uicsa/index.htm", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0 );
if ( !hData )
{
InternetCloseHandle(hConnection);
InternetCloseHandle(hINet);
return;
}
HttpSendRequest( hData, NULL, 0, NULL, 0);
while( InternetReadFile( hData, buffer, 255, &dwRead ) )
{
if ( dwRead == 0 )
return;
buffer[dwRead] = 0;
m_strContents += buffer;
}
}
catch( CInternetException* e)
{
e->ReportError();
e->Delete();
}
InternetCloseHandle(hConnection);InternetCloseHandle(hINet);
InternetCloseHandle(hData);

HttpAddRequestHeaders

You can add, replace or remove headers to your HTTP request by using this function. Add, replace, or remove depends on dwModifiers parameter. Ok, here is the syntax:

BOOL HttpAddRequestHeaders( IN HINTERNET hHttpRequest, IN LPCSTR lpszHeaders,    IN DWORD dwHeadersLength,  IN DWORD dwModifiers);

Parameters Description
hHttpRequest Handle of http request.
lpszHeaders Header you want to add.
dwHeadersLengthLength of the header.
dwModifierswhat kind of request is it? Add, replace or remove.

Ok, here is an example:

char* chHead = "Accept: image/*/r/n" ; HttpAddRequestHeaders( hHttpHandle, chHead, -1, HTTP_ADDREQ_FLAG_ADD);

HttpQueryInfo

This function allows you to retrieve information about a give HTTP request. Here is the syntax:

BOOL HttpQueryInfo(IN HINTERNET hHttpRequest, IN DWORD dwInfoLevel,    IN LPVOID lpvBuffer,  IN LPDWORD lpdwBufferLength,IN OUT LPDWORD lpdwIndex);

ParametersDescription
hHttpRequest Handle of http request.
dwInfoLevelType of information you are interested in.
lpvBufferBuffer.
lpdwBufferLengthBuffer size
lpdwIndexIndex

Ok, here is an example:

CHAR chBuff[1024];DWORD dwLen 1024;BOOL bRet;bRet = HttpQueryInfo( hRequest, HTTP_QUERY_CUSTOM, chBuff, &dwLen, NULL);
 
WinInet FTP APIs

Here is a list of ftp functions. All FTP functions are easy to understand and use. There is nothing to explain. I have sample examples for most of the functions.

Function Description
FtpCreateDirectory Creates a new directory.
FtpDeleteFileDeletes a file.
FtpFindFirstFileSearches the specified directory of the given FTP session. File and directory entries are returned to the application in the WIN32_FIND_DATA structure.
FtpGetCurrentDirectory Retrieves the current directory for the specified FTP session.
FtpGetFileRetrieves a file from the FTP server and stores it under the specified file name, creating a new local file in the process.
FtpGetFileSizeRetrieves the file size of the requested FTP resource.
FtpOpenFileInitiates access to a remote file on an FTP server for reading or writing.
FtpPutFileStores a file on the FTP server.
FtpRemoveDirectoryRemoves the specified directory.
FtpRenameFileRenames a file.
FtpSetCurrentDirectoryChanges to a different working directory.

FtpCreateDirectory

This function creates a directory on the FTP server.

Syntax:

BOOL FtpCreateDirectory(IN HINTERNET hConnect, IN LPCTSTR lpszDirectory );

Sample:

if (!FtpCreateDirectory(g_hConnection, "NewDir"))
DoSomething();

FtpDeleteFile

This function deletes a file from the FTP server. Nothing to explain here :). Here is the syntax:

BOOL FtpDeleteFile( IN HINTERNET hConnect, IN LPCTSTR lpszFileName);

Sample:

if (!FtpDeleteFile(g_hConnection, strFileName))
DoSomething();

FtpFindFirstFile

This function finds a file on the FTP server and help InternetFindNextFile to find files on the server.

Syntax:

HINTERNET FtpFindFirstFile(IN HINTERNET hConnect,IN LPCTSTR lpszSearchFile,    OUT LPWIN32_FIND_DATA lpFindFileData, IN DWORD dwFlags, IN DWORD_PTR dwContext );

Sample:

CString strFileName = "mcb.asp";
WIN32_FIND_DATA FindFileData;
HINTERNET hFindFile=NULL;
m_RemoteList.ResetContent();
if (hFindFile)
InternetCloseHandle(hFindFile);
hFindFile = FtpFindFirstFile(g_hConnection, szFileName, &FindFileData, INTERNET_FLAG_RELOAD, 0);
if (hFindFile)
{
if ( (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY )
strFileName.Format("%s <DIR>", FindFileData.cFileName);
else
strFileName = FindFileData.cFileName;
m_RemoteList.AddString(strFileName);
while(InternetFindNextFile(hFindFile, &FindFileData))
{
if ( (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY )
strFileName.Format("%s <DIR>", FindFileData.cFileName);
else
strFileName = FindFileData.cFileName;
m_RemoteList.AddString(strFileName);
}
InternetCloseHandle(hFindFile);
}

FtpGetCurrentDirectory

This function returns the current active directory of the FTP server. All FTP function applies to current active directory. So this function is plays an important role.

Syntax:

BOOL FtpGetCurrentDirectory(IN HINTERNET hConnect,OUT LPTSTR lpszCurrentDirectory,    IN OUT LPDWORD lpdwCurrentDirectory);

Sample:

char szDir[255];
DWORD dwLen = 255;
//get the current remote directory
if (!FtpGetCurrentDirectory(g_hConnection, szDir, &dwLen))
DoSomething();

FtpGetFile

This function retrieves a file from the FTP server and stores on the local system. Here is the syntax:

BOOL FtpGetFile(IN HINTERNET hConnect, IN LPCTSTR lpszRemoteFile, IN LPCTSTR                lpszNewFile, IN BOOL fFailIfExists,IN DWORD dwFlagsAndAttributes,IN DWORD dwFlags,IN DWORD_PTR dwContext);
 
FtpGetFileSize

Retrieves the file size of the requested FTP resource.

Syntax:

DWORD FtpGetFileSize(IN HINTERNET hFile, OUT LPDWORD lpdwFileSizeHigh);
 
FtpOpenFile

Initiates access to a remote file on an FTP server for reading or writing.

Syntax:

HINTERNET FtpOpenFile (IN HINTERNET hConnect,IN LPCTSTR lpszFileName,IN DWORD dwAccess, IN DWORD dwFlags,IN DWORD_PTR dwContext);

FtpPutFile

Send a local file on the FTP server.

Syntax:

BOOL FtpPutFile(IN HINTERNET hConnect,IN LPCTSTR lpszLocalFile,IN LPCTSTR lpszNewRemoteFile, IN DWORD dwFlags,IN DWORD_PTR dwContext);
DWORD dwFlags;
if (m_lMode == MODE_ASCII) 
dwFlags = FTP_TRANSFER_TYPE_ASCII;
else  
dwFlags = FTP_TRANSFER_TYPE_BINARY;
BOOL bRet = FtpPutFile(g_hConnection, szLocalFile, szRemoteFile, dwFlags, 0);

FtpRemoveDirectory

Removes the specified directory on the FTP server.

Syntax:

BOOL FtpRemoveDirectory(IN HINTERNET hConnect,IN LPCTSTR lpszDirectory)
 
FtpRenameFile

Renames a file stored on the FTP server.

Syntax:

BOOL FtpRenameFile(IN HINTERNET hConnect,IN LPCTSTR lpszExisting,IN LPCTSTR lpszNew);

FtpSetCurrentDirectory Function

Change to a different working directory on the FTP server.

Syntax:

BOOL FtpSetCurrentDirectory(IN HINTERNET hConnect,IN LPCTSTR lpszDirectory);

Other chapters of this tutorial

ABAP动态填充Word模板:基于cl_docx_document的XML替换方案 本文详细介绍了ABAP中基于cl_docx_document类的动态Word模板填充方案。该方案通过直接操作DOCX文件的XML核心,完美解决了传统OLE/DOI方法在格式保真、性能和后台运行方面的痛点,是实现复杂、批量Word文档生成的优雅解决方案。 阅读详情

相关推荐

STM32F103多通道PWM输出避坑指南:TIM1_CH1异常输出的解决方案

本文深入解析STM32F103多通道PWM输出中的TIM1_CH1异常问题,提供从时钟配置到GPIO复用的系统化解决方案。通过黄金法则配置流程和高级调试技巧,帮助开发者快速定位并解决PWM输出不稳定、占空比异常等问题,同时拓展多定时器协同和互补PWM等高级应用场景。

weixin_29194817的博客 120

FtpFindFirstFile()及InternetFindNextFile()对汉字编码的支持问题

首先说明情况。 ftp服务器是同事写的,运行在linux下,字符采用utf-8编码。 我写客户端,在windows下,自己的开发电脑是win74 64位简体中文版,ide采用vs2010,工程为ocx项目,支持MFC,unicode支持。 附加信息: 由于用到了字符编码的转换,为了简单,直接从网上找了相关的函数,参考网址: http://blog.csdn.net/feisy/a

jszj的专栏 3256

硬件工程师八大细分岗位详解:从芯片到整机的技术分工与职业选择

硬件工程作为电子系统设计的核心领域,涉及从芯片级到整机级的完整技术链条。其基本原理是通过电路设计、信号处理和功率管理等技术,将电子元件集成为功能完备的硬件系统。随着产品复杂度提升,硬件工程师岗位出现高度专业化分工,这种细分不仅提升了技术深度,更优化了团队协作效率。在工程实践中,信号完整性和电源完整性成为高速数字设计的两个关键技术挑战,直接影响系统稳定性。本文基于行业热词"硬件工程师"和"信号完整性",深入解析八大细分岗位的技术差异,帮助从业者明确职业发展方向,构建系统化的硬件设计思维。

weixin_33811539的博客 460

用WinINet Api 开发FTP客户端 (二)

FTP WinApi 函数详解FTP 各函数间的关系 各FTP函数工作时需要用到InternetConnect创建的FTP会话句柄。而InternetConnect函数需要用到由InternetOpen创建的HINTERNET句柄。下面这个图可以表示他们间的关系。  而对那些在线操作的一些命令来说,他们的关系可以用下面这个图表示。例如用于枚举当前目录下文件的两个命令FtpFindFirstFile

洪虎专栏 1万+

Power BI应用案例:淘宝用户行为分析实战

一、案例背景 基于千万级的淘宝用户的使用记录,对淘宝用户的行为进行分析,具体指标包括:日PV、日UV、小时PV、小时UV、不同用户行为的PV、用户购买次数分布、用户购买频次分布、活跃用户消费情况等。通过本案例,可以基本看看Power BI都能分析达到怎么样的效果。 二、数据来源及结构 阿里天池官方发布的1万用户在2014年双十二期间的行为记录,数据量达1200万+。 数据下载地址:...

灰哥数据智能的专栏 1万+

数字通信:信源编码、信道编码、调制、物理信道、解调、信道解码、信源解码

在通信系统中,信息的传输通常需要经过一系列的处理步骤,确保数据可以在信道中高效且可靠地传输。以下是信源编码、信道编码、调制、物理信道、解调、信道解码、信源解码的具体含义及其常见技术的解释。

weixin_44012667的博客 4819

HINTERNET 句柄

这篇文章的内容能够简单总结例如以下: WinINet函数创建和使用类型为HINTERNET的句柄 HINTERNET句柄不能与其它类型的句柄互换使用 用InternetCloseHandle关闭HINTERNET句柄 有多种类型的HINTERNET句柄,它们形成一个树结构,例如以下图所看到的:   图中带阴影的方框创建的HINTERNET...

weixin_33671935的博客 207

Redhawk:toggle rate从哪来?

redhwak中toggle rate的相关设置

m0_61544122的博客 8137

DELPHI 获取错误信息

try ....(需要执行的代码) except on e:exception do application.MessageBox(pchar(e.Message), '错误提示', 0); end;    转载于:https://www.cnblogs.com/Bung/archive/2011/08/15/2139384.html...

weixin_30384031的博客 329

华为od统一考试B卷【宜居星球改造计划】Java&Go 实现

XXX年,人类通过对火星的大气进行宜居改造分析,使得火星已在理论上具备人类宜居的条件,由于技术原因,无法一次性将火星大气全部改造,只能通过局部处理形式,假设将火星待改造的区域为row * column的网格每个网格有3个值,宜居区、可改造区、死亡区,使用YES、NO、NA代替,YES表示该网格已经完成大气改造,NO表示该网格未进行改造,后期可进行改造,NA表示死亡区,不作为判断是否改造完成的宜居,无法穿过 初始化下,该区域可能存在多个宜居区,并且每个宜居区能同时在每个太阳日单位向上下左右四个方向的相邻格子进

MISAYAONE的博客 1万+

Sigrity Power SI Model Extraction模式如何提取电源网络的S参数和阻抗操作指导(一)

34. 默认格式是BNP format格式,会被Sigrity识别的S参数文件,在仿真完成后,默认会在文件夹中生成bnp和ckt(MCP)文件,无需这步操作。由于port设置的ref Z是50ohm,实际上电源网络的阻抗是没有这么高的,所以S参数回损曲线是基本上全反射的一个状态。Sigrity PowerSI是频域电磁场仿真工具,以下图为例介绍如果用它观测电源的网络的S参数以及阻抗的频域曲线.42. Spice格式的加载进来图标是橙色的,BNP格式的图标是灰色的。

weixin_54787054的博客 1288

获得ftp服务器上文件名和长度,怎么获得FTP服务器上目录下的所有文件名和子目录...

当前位置:我的异常网» Delphi»怎么获得FTP服务器上目录下的所有文件名和子目录怎么获得FTP服务器上目录下的所有文件名和子目录www.myexceptions.net网友分享于:2015-08-26浏览:560次如何获得FTP服务器上目录下的所有文件名和子目录我用的是indy 10 的tidftp控件,请问如何获得FTP服务器上目录下的所有文件名和子目录,先谢谢了---...

weixin_29745609的博客 785

RT-DETR模型应用过程中的报错处理及疑问解答,涉及环境搭建、模型训练、模块改进、论文写作

本文记录了RT-DETR模型的这一系列过程中可能发生的问题,以及大家非常关心的问题,这里统一做了一个记录和解答。

Limiiiing的博客 1774
上一篇: 修改EXE文件图标及资源的方法
下一篇: 感觉比较好的hips+av配置方案
iiprogram
博客等级 码龄25年 1068粉丝 1515原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址