共用方式為


IHttpRequest::InsertEntityBody 方法

將新的實體主體插入現有的 HTTP 要求本文。

語法

virtual HRESULT InsertEntityBody(  
   IN VOID* pvBuffer,  
   IN DWORD cbBuffer  
) = 0;  

參數

pvBuffer
[IN]包含要求實體主體之緩衝區的指標 VOID

cbBuffer
[IN], DWORD 包含緩衝區的大小,以位元組為單位。

傳回值

HRESULT。 可能的值包括 (但不限於) 下表中的這些值。

描述
S_OK 表示作業成功。

備註

InsertEntityBody方法會將 參數指向的實體主體 pvBuffer 插入 HTTP 要求實體主體。

注意

新實體主體的插入點位於任何後續可從 Web 用戶端擷取的剩餘未讀取實體主體之前。

重要

IIS 不會建立實體主體緩衝區的複本。 您必須保留模組的實體緩衝區,直到要求結束為止。

這個方法不會更新 Content-Length 標頭值。 使用此方法的模組必須個別更新 Content-Length。

範例

下列程式碼範例示範如何建立 HTTP 模組來呼叫 IHttpCoNtext::AllocateRequestMemory 方法來配置 1 KB 緩衝區。 模組接著會將字串 「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) 而不是明確宣告每個函式的呼叫慣例。

規格需求

類型 描述
Client - 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
標頭 Httpserv.h

另請參閱

IHttpRequest 介面