共用方式為


IHttpResponse::WriteEntityChunks 方法

將一或多個 HTTP_DATA_CHUNK 結構附加至回應本文。

語法

virtual HRESULT WriteEntityChunks(  
   IN HTTP_DATA_CHUNK* pDataChunks,  
   IN DWORD nChunks,  
   IN BOOL fAsync,  
   IN BOOL fMoreData,  
   OUT DWORD* pcbSent,  
   OUT BOOL* pfCompletionExpected = NULL  
) = 0;  

參數

pDataChunks
[IN]一或多個 HTTP_DATA_CHUNK 結構的指標。

nChunks
[IN], DWORD 其中包含 所 pDataChunks 指向的區塊數目。

fAsync
[IN] true 如果方法應該以非同步方式完成,則為 ;否則為 false

fMoreData
[IN] true 如果要在回應中傳送更多資料,則為 ; false 如果這是最後一個資料,則為 。

pcbSent
[OUT]如果呼叫同步完成,則傳送給用戶端的位元組數目。

pfCompletionExpected
[OUT] true 如果這個呼叫的非同步完成擱置中,則為 ;否則為 false

傳回值

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

描述
S_OK 表示作業成功。
ERROR_INVALID_PARAMETER 表示參數無效 (例如, pDataChunks 指標會設定為 Null) 。
ERROR_NOT_ENOUGH_MEMORY 表示記憶體不足,無法執行作業。
ERROR_ARITHMETIC_OVERFLOW 已將超過 65535 個區塊新增至回應。

備註

開發人員可以使用 WriteEntityChunks 方法,將單 HTTP_DATA_CHUNK 一結構或結構陣列 HTTP_DATA_CHUNK 插入回應主體中。 如果作業已同步完成, pcbSent 參數將會收到插入回應中的位元組數目。

如果開啟緩衝處理, WriteEntityChunks 此方法會建立任何 HTTP_DATA_CHUNK 結構的複本,藉此複製基礎資料,使其不需要保留。 如果緩衝已關閉,或達到回應緩衝區限制,方法 WriteEntityChunks 也會排清回應。 如果緩衝已關閉,且 fAsync 參數為 true ,則必須保留記憶體,直到要求完成為止。

您可以將 WriteEntityChunks 參數 true 設定 fAsync 為 ,以非同步方式完成作業。 在此情況下,方法 WriteEntityChunks 會立即傳回給呼叫端,而 pcbSent 參數將不會收到插入回應中的位元組數目。 如果緩衝已停用,且 fAsync 參數為 true ,則必須保存參數的 pDataChunks 記憶體,直到非同步呼叫完成為止。

最多 65535 個區塊 (64 KB 減 1) 可以寫入要求。

範例

下列範例示範如何使用 WriteEntityChunks 方法來建立 HTTP 模組,以將兩個數據區塊的陣列插入回應中。 此範例接著會傳回 Web 用戶端的回應。

#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <httpserv.h>

// Create the module class.
class MyHttpModule : public CHttpModule
{
public:
    REQUEST_NOTIFICATION_STATUS
    OnBeginRequest(
        IN IHttpContext * pHttpContext,
        IN IHttpEventProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );

        // Create an HRESULT to receive return values from methods.
        HRESULT hr;
        
        // Create an array of data chunks.
        HTTP_DATA_CHUNK dataChunk[2];

        // Buffer for bytes written of data chunk.
        DWORD cbSent;
        
        // Create string buffers.
        PCSTR pszOne = "First chunk data\n";
        PCSTR pszTwo = "Second chunk data\n";

        // Retrieve a pointer to the response.
        IHttpResponse * pHttpResponse = pHttpContext->GetResponse();

        // Test for an error.
        if (pHttpResponse != NULL)
        {
            // Clear the existing response.
            pHttpResponse->Clear();
            // Set the MIME type to plain text.
            pHttpResponse->SetHeader(
                HttpHeaderContentType,"text/plain",
                (USHORT)strlen("text/plain"),TRUE);
            
            // Set the chunk to a chunk in memory.
            dataChunk[0].DataChunkType = HttpDataChunkFromMemory;
            // Set the chunk to the first buffer.
            dataChunk[0].FromMemory.pBuffer =
                (PVOID) pszOne;
            // Set the chunk size to the first buffer size.
            dataChunk[0].FromMemory.BufferLength =
                (USHORT) strlen(pszOne);

            // Set the chunk to a chunk in memory.
            dataChunk[1].DataChunkType = HttpDataChunkFromMemory;
            // Set the chunk to the second buffer.
            dataChunk[1].FromMemory.pBuffer =
                (PVOID) pszTwo;
            // Set the chunk size to the second buffer size.
            dataChunk[1].FromMemory.BufferLength =
                (USHORT) strlen(pszTwo);

            // Insert the data chunks into the response.
            hr = pHttpResponse->WriteEntityChunks(
                dataChunk,2,FALSE,TRUE,&cbSent);

            // 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

另請參閱

IHttpResponse 介面
IHttpResponse::WriteEntityChunkByReference 方法