IReadEntityProvider::GetEntity 方法
检索请求实体。
语法
virtual VOID GetEntity(
PVOID* ppBuffer,
DWORD* pcbData,
DWORD* pcbBuffer
) = 0;
parameters
ppBuffer
指向包含请求实体的 void 缓冲区的指针。
pcbData
指向 的指针 DWORD
,该指针包含 中 ppBuffer
数据的大小。
pcbBuffer
指向 DWORD
包含缓冲区大小的 的指针,该大小 ppBuffer
应大于或等于 pcbData
。
返回值
VOID
.
注解
调用 GetEntity
方法后, ppBuffer
参数将指定请求实体; pcbData
参数将指定中返回 ppBuffer;
的请求实体中的数据的大小(以字节为单位),参数 pcbBuffer
将指定 指向 ppBuffer
的请求实体缓冲区的大小(以字节为单位)。
注意
pcbBuffer
应始终大于或等于 pcbData
。
示例
下面的代码示例演示如何创建执行以下任务的 HTTP 模块:
使用
GetEntity
方法检索请求实体。创建包含请求实体数据大小和缓冲区大小的字符串数组。
写入包含请求实体信息的事件查看器日志条目,然后退出。
#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <httpserv.h>
// NOTE - Data needs to be passed to this module, e.g. a POST request, or it will not appear to return anything.
// Create the module class.
class MyHttpModule : public CHttpModule
{
public:
REQUEST_NOTIFICATION_STATUS
OnReadEntity(
IN IHttpContext * pHttpContext,
IN IReadEntityProvider * pProvider
)
{
UNREFERENCED_PARAMETER( pHttpContext );
// Create buffers for the request entity information.
PVOID pBuffer = NULL;
DWORD cbData = 0;
DWORD cbBuffer = 0;
// Retrieve the request entity.
pProvider->GetEntity(&pBuffer,&cbData,&cbBuffer);
// Create string buffers for the return messages.
char szData[30];
char szBuffer[30];
// Format the return messages.
sprintf_s(szData,29,"Data Size: %u",cbData);
sprintf_s(szBuffer,29,"Buffer Size: %u",cbBuffer);
// Create an array of strings for the event viewer log.
LPCSTR szReturn[3] = {"Request Entity Information",szData,szBuffer};
// Write the strings to the Event Viewer.
WriteEventViewerLog(szReturn,3);
// Return processing to the pipeline.
return RQ_NOTIFICATION_CONTINUE;
}
MyHttpModule()
{
// Open a handle to the Event Viewer.
m_hEventLog = RegisterEventSource( NULL,"IISADMIN" );
}
~MyHttpModule()
{
// Test whether the handle for the Event Viewer is open.
if (NULL != m_hEventLog)
{
// Close the handle to the Event Viewer.
DeregisterEventSource( m_hEventLog );
m_hEventLog = NULL;
}
}
private:
// Create a handle for the event viewer.
HANDLE m_hEventLog;
// Define a method that writes to the Event Viewer.
BOOL WriteEventViewerLog(LPCSTR szBuffer[], WORD wNumStrings)
{
// Test whether the handle for the Event Viewer is open.
if (NULL != m_hEventLog)
{
// Write any strings to the Event Viewer and return.
return ReportEvent(
m_hEventLog,
EVENTLOG_INFORMATION_TYPE,
0, 0, NULL, wNumStrings,
0, szBuffer, NULL );
}
return FALSE;
}
};
// Create the module's class factory.
class MyHttpModuleFactory : public IHttpModuleFactory
{
public:
HRESULT
GetHttpModule(
OUT CHttpModule ** ppModule,
IN IModuleAllocator * pAllocator
)
{
UNREFERENCED_PARAMETER( pAllocator );
// Create a new instance.
MyHttpModule * pModule = new MyHttpModule;
// Test for an error.
if (!pModule)
{
// Return an error if the factory cannot create the instance.
return HRESULT_FROM_WIN32( ERROR_NOT_ENOUGH_MEMORY );
}
else
{
// Return a pointer to the module.
*ppModule = pModule;
pModule = NULL;
// Return a success status.
return S_OK;
}
}
void Terminate()
{
// Remove the class from memory.
delete this;
}
};
// Create the module's exported registration function.
HRESULT
__stdcall
RegisterModule(
DWORD dwServerVersion,
IHttpModuleRegistrationInfo * pModuleInfo,
IHttpServer * pGlobalInfo
)
{
UNREFERENCED_PARAMETER( dwServerVersion );
UNREFERENCED_PARAMETER( pGlobalInfo );
// Set the request notifications and exit.
return pModuleInfo->SetRequestNotifications(
new MyHttpModuleFactory,
RQ_READ_ENTITY,
0
);
}
模块必须导出 RegisterModule 函数。 可以通过为项目创建模块定义 (.def) 文件导出此函数,也可以使用 开关编译模块 /EXPORT:RegisterModule
。 有关详细信息,请参阅 演练:使用本机代码创建Request-Level HTTP 模块。
可以选择使用调用约定编译代码, __stdcall (/Gz)
而不是为每个函数显式声明调用约定。
要求
类型 | 说明 |
---|---|
客户端 | - Windows Vista 上的 IIS 7.0 - Windows 7 上的 IIS 7.5 - Windows 8 上的 IIS 8.0 - Windows 10 上的 IIS 10.0 |
服务器 | - Windows Server 2008 上的 IIS 7.0 - Windows Server 2008 R2 上的 IIS 7.5 - Windows Server 2012 上的 IIS 8.0 - Windows Server 2012 R2 上的 IIS 8.5 - Windows Server 2016 上的 IIS 10.0 |
产品 | - IIS 7.0、IIS 7.5、IIS 8.0、IIS 8.5、IIS 10.0 - IIS Express 7.5、IIS Express 8.0、IIS Express 10.0 |
Header | Httpserv.h |
另请参阅
IReadEntityProvider 接口
IReadEntityProvider::SetEntity 方法
IHttpRequest::ReadEntityBody 方法
IHttpRequest::InsertEntityBody 方法