IHttpRequest::InsertEntityBody 方法
将新的实体正文插入到现有的 HTTP 请求正文中。
语法
virtual HRESULT InsertEntityBody(
IN VOID* pvBuffer,
IN DWORD cbBuffer
) = 0;
parameters
pvBuffer
[IN]指向包含请求实体正文的缓冲区的指针 VOID
。
cbBuffer
[IN]一个 DWORD
,它包含缓冲区的大小(以字节为单位)。
返回值
HRESULT
。 可能的值包括(但并不限于)下表中的项。
值 | 说明 |
---|---|
S_OK | 指示操作成功。 |
备注
InsertEntityBody 方法将 参数指向pvBuffer
的实体正文插入 HTTP 请求实体正文。
注意
新实体正文的插入点位于以后可从 Web 客户端检索的任何剩余未读实体正文之前。
重要
IIS 不会复制实体正文缓冲区。 模块的实体缓冲区必须保留到请求结束。
此方法不更新 Content-Length 标头值。 使用此方法的模块必须单独更新 Content-Length。
示例
下面的代码示例演示如何创建调用 IHttpContext::AllocateRequestMemory 方法以分配 1 KB 缓冲区的 HTTP 模块。 然后,该模块将字符串“Hello World”复制到缓冲区中。 最后,模块调用 InsertEntityBody
方法,将预加载的 HTTP 请求实体正文替换为缓冲区,然后退出。
#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
OnBeginRequest(
IN IHttpContext * pHttpContext,
IN IHttpEventProvider * pProvider
)
{
// Create an HRESULT to receive return values from methods.
HRESULT hr;
// Allocate a 1K buffer.
DWORD cbBuffer = 1024;
void * pvBuffer = pHttpContext->AllocateRequestMemory(cbBuffer);
// Test for an error.
if (NULL == pvBuffer)
{
// Set the error status.
pProvider->SetErrorStatus(
HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY));
// End additional processing.
return RQ_NOTIFICATION_FINISH_REQUEST;
}
// Copy a string into the buffer.
strcpy_s((char*)pvBuffer,cbBuffer,"Hello world!");
// Insert the entity body into the buffer.
hr = pHttpContext->GetRequest()->InsertEntityBody(
pvBuffer,(DWORD)strlen((char*)pvBuffer));
// Test for an error.
if (FAILED(hr))
{
// Set the error status.
pProvider->SetErrorStatus( hr );
// End additional processing.
return RQ_NOTIFICATION_FINISH_REQUEST;
}
// Return processing to the pipeline.
return RQ_NOTIFICATION_CONTINUE;
}
};
// 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_BEGIN_REQUEST,
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 |