共用方式為


ISendResponseProvider::GetLogData 方法

擷取目前回應的記錄資訊。

語法

virtual VOID* GetLogData(  
   VOID  
) const = 0;  

參數

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

傳回值

VOID 的指標。

備註

方法 GetLogData 會擷 VOID 取指標,您轉換成 HTTP_LOG_FIELDS_DATA 結構來處理記錄資訊。

注意

結構 HTTP_LOG_FIELDS_DATA 定義于 Http.h 標頭檔中。

重要

您的 HTTP 模組必須將註冊優先順序設定為 PRIORITY_ALIAS_FIRST 或 PRIORITY_ALIAS_HIGH,否則 GetLogData 一律會傳回 Null。 如需設定註冊優先順序的詳細資訊,請參閱 IHttpModuleRegistrationInfo::SetPriorityForRequestNotification 方法。

呼叫 GetLogData 之前,您必須呼叫 ISendResponseProvider::GetReadyToLogData 方法,以確認 IIS 已準備好記錄資訊。 傳回 trueGetReadyToLogData ,您的模組可以使用 GetLogData 方法來擷取記錄資訊。

範例

下列程式碼範例示範如何建立 HTTP 模組,以使用 GetReadyToLogData 方法來判斷 IIS 是否準備好記錄資訊。 此課程模組會完成下列步驟:

  1. GetLogData使用 方法來擷取 HTTP_LOG_FIELDS_DATA 結構。

  2. 使用此結構從記錄資訊擷取伺服器名稱。

  3. 修改記錄專案中的伺服器埠。

  4. 使用 SetLogData 方法,將修改過的記錄資訊提交至 IIS。

  5. 將伺服器名稱傳回至 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
    )
    {
        
        // Retrieve log status.
        if (TRUE == pProvider->GetReadyToLogData())
        {
            // Retrieve log information.
            PHTTP_LOG_FIELDS_DATA pLogData =
                (PHTTP_LOG_FIELDS_DATA) pProvider->GetLogData();
            // Test for an error.
            if (NULL != pLogData)
            {
                // 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);
                // Return the server name.
                WriteResponseMessage(pHttpContext,
                    "Server name: ",pLogData->ServerName);
                // Modify the port number.
                pLogData->ServerPort = 8080;
                // Set the log data.
                HRESULT hr = pProvider->SetLogData(pLogData);
                // 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;
    }

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 );

    // Create an HRESULT to receive return values from methods.
    HRESULT hr;

    // Set the request priority.
    hr = pModuleInfo->SetPriorityForRequestNotification(
        RQ_SEND_RESPONSE,PRIORITY_ALIAS_HIGH);

    // Test for an error and exit if necessary.
    if (FAILED(hr))
    {
        return hr;
    }

    // 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::GetReadyToLogData 方法
ISendResponseProvider::SetLogData 方法