共用方式為


ISendResponseProvider::GetFlags 方法

擷取目前 HTTP 回應的旗標。

語法

virtual DWORD GetFlags(  
   VOID  
) const = 0;  

參數

此方法不會採用任何參數。

傳回值

DWORD,包含回應旗標。

備註

您可以使用 GetFlags 方法來擷取目前回應的回應旗標。 這些旗標提供 IIS 7 HTTP 行為的額外控制權。 Http.h 標頭檔中定義了下列旗標。

名稱 描述
HTTP_SEND_RESPONSE_FLAG_DISCONNECT 0x00000001 在傳送回應之後立即中斷網路連線。
HTTP_SEND_RESPONSE_FLAG_MORE_DATA 0x00000002 在回應中傳送其他資料。
HTTP_SEND_RESPONSE_FLAG_BUFFER_DATA 0x00000004 先緩衝處理回應再傳送。
HTTP_SEND_RESPONSE_FLAG_ENABLE_NAGLING 0x00000008 讓 Nagle 演算法優化 TCP 回應封包。

範例

下列程式碼範例示範如何使用 SetFlagsGetFlags 方法來建立 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
    OnSendResponse(
        IN IHttpContext * pHttpContext,
        IN ISendResponseProvider * pProvider
    )
    {
        // Set the flags.
        pProvider->SetFlags( pProvider->GetFlags() | 
            HTTP_SEND_RESPONSE_FLAG_DISCONNECT |
            HTTP_SEND_RESPONSE_FLAG_BUFFER_DATA );

        // Retrieve the flags.
        DWORD dwFlags = pProvider->GetFlags();

        // Clear the existing response.
        pHttpContext->GetResponse()->Clear();
        // Set the MIME type to plain text.
        pHttpContext->GetResponse()->SetHeader(
            HttpHeaderContentType,"text/plain",
            (USHORT)strlen("text/plain"),TRUE);

        // Create a string buffer for the return message.
        char szBuffer[256];
        sprintf_s(szBuffer,255,"0x%x\n",dwFlags);

        // Test for the disconnect flag.
        if (dwFlags & HTTP_SEND_RESPONSE_FLAG_DISCONNECT)
            strcat_s(szBuffer,255,
            "\nHTTP_SEND_RESPONSE_FLAG_DISCONNECT");

        // Test for the more data flag.
        if (dwFlags & HTTP_SEND_RESPONSE_FLAG_MORE_DATA)
            strcat_s(szBuffer,255,
            "\nHTTP_SEND_RESPONSE_FLAG_MORE_DATA");

        // Test for the buffer data flag.
        if (dwFlags & HTTP_SEND_RESPONSE_FLAG_BUFFER_DATA)
            strcat_s(szBuffer,255,
            "\nHTTP_SEND_RESPONSE_FLAG_BUFFER_DATA");

        // Return the message to the Web client.
        WriteResponseMessage(pHttpContext,"\nFlags: ",szBuffer);

        // End additional processing.
        return RQ_NOTIFICATION_FINISH_REQUEST;
    }

private:

    // Create a utility method that inserts a name/value pair into the response.
    HRESULT WriteResponseMessage(
        IHttpContext * pHttpContext,
        PCSTR pszName,
        PCSTR pszValue
    )
    {
        // Create an HRESULT to receive return values from methods.
        HRESULT hr;
        
        // Create a data chunk.
        HTTP_DATA_CHUNK dataChunk;
        // Set the chunk to a chunk in memory.
        dataChunk.DataChunkType = HttpDataChunkFromMemory;
        // Buffer for bytes written of data chunk.
        DWORD cbSent;

        // Set the chunk to the first buffer.
        dataChunk.FromMemory.pBuffer =
            (PVOID) pszName;
        // Set the chunk size to the first buffer size.
        dataChunk.FromMemory.BufferLength =
            (USHORT) strlen(pszName);
        // Insert the data chunk into the response.
        hr = pHttpContext->GetResponse()->WriteEntityChunks(
            &dataChunk,1,FALSE,TRUE,&cbSent);
        // Test for an error.
        if (FAILED(hr))
        {
            // Return the error status.
            return hr;
        }

        // Set the chunk to the second buffer.
        dataChunk.FromMemory.pBuffer =
            (PVOID) pszValue;
        // Set the chunk size to the second buffer size.
        dataChunk.FromMemory.BufferLength =
            (USHORT) strlen(pszValue);
        // Insert the data chunk into the response.
        hr = pHttpContext->GetResponse()->WriteEntityChunks(
            &dataChunk,1,FALSE,TRUE,&cbSent);
        // Test for an error.
        if (FAILED(hr))
        {
            // Return the error status.
            return hr;
        }

        // Return a success status.
        return S_OK;
    }
};

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

另請參閱

ISendResponseProvider 介面
ISendResponseProvider::SetFlags 方法