共用方式為


IHttpResponse::Flush 方法

將回應緩衝區中的現有內容傳送至用戶端。

語法

virtual HRESULT Flush(  
   IN BOOL fAsync,  
   IN BOOL fMoreData,  
   OUT DWORD* pcbSent,  
   OUT BOOL* pfCompletionExpected = NULL  
) = 0;  

參數

fAsync
[IN] true 以非同步方式完成作業;否則為 false

fMoreData
[IN] true 表示在此回應中傳送更多資料;否則為 false

pcbSent
[OUT]的 DWORD 指標,接收傳送給用戶端的位元組數目。

pfCompletionExpected
[OUT]布林值的指標,接收這個呼叫的非同步完成是否擱置中。

傳回值

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

描述
S_OK 表示作業成功。
ERROR_INVALID_DATA 表示資料無效。
ERROR_NOT_ENOUGH_MEMORY 表示記憶體不足,無法執行作業。

備註

方法 Flush 會將目前可用的回應傳送給用戶端。 回應至少包含狀態標頭,但也會包含呼叫 方法時存在的任何回應緩衝區。

fMoreData如果在呼叫 Flush 方法之後會傳回更多資料,請將 參數設定為 true ,或在沒有剩餘的資料時設定 fMoreDatafalse

方法 Flush 支援同步和非同步作業。 如果作業是非同步,請將 參數 true 設定 fAsync 為 ,或當作業為同步時設定為 , fAsyncfalse 以指定作業模式。

注意

如果您要以非同步方式呼叫此方法,則必須在呼叫之後立即傳回 。

範例

下列程式碼範例示範如何使用 Flush 方法來將目前的回應傳送給用戶端。 Flush因為方法已將回應標頭傳送給用戶端,所以對 Clear方法的後續呼叫不會有任何作用。

#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;
        // Buffer to store the byte count.
        DWORD cbSent = 0;
        // Buffer to store if asyncronous completion is pending.
        BOOL fCompletionExpected = false;

        // Retrieve a pointer to the response.
        IHttpResponse * pHttpResponse = pHttpContext->GetResponse();
        
        // Test for an error.
        if (pHttpResponse != NULL)
        {
            // Flush the response to the client.
            hr = pHttpResponse->Flush(false,true,&cbSent,&fCompletionExpected);
            // Test for an error.
            if (FAILED(hr))
            {
                // Set the error status.
                pProvider->SetErrorStatus( hr );
                // End additional processing.
                return RQ_NOTIFICATION_FINISH_REQUEST;
            }
            // Clear the response.
            pHttpResponse->Clear();
        }

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