設定或附加指定 HTTP 要求標頭的值。
語法
virtual HRESULT SetHeader(
IN PCSTR pszHeaderName,
IN PCSTR pszHeaderValue,
IN USHORT cchHeaderValue,
IN BOOL fReplace
) = 0;
virtual HRESULT SetHeader(
IN HTTP_HEADER_ID ulHeaderIndex,
IN PCSTR pszHeaderValue,
IN USHORT cchHeaderValue,
IN BOOL fReplace
) = 0;
參數
pszHeaderName
[IN]字串的指標,其中包含要設定的 HTTP 標頭名稱。
ulHeaderIndex
[IN]要設定的 HTTP 標頭識別碼。
pszHeaderValue
[IN]字串的指標,其中包含要設定之標頭的值。
cchHeaderValue
[IN]標頭值的長度,以字元為單位,不包含 \0 字元。
fReplace
[IN] true 表示覆寫現有的標頭;否則為 false 。
傳回值
HRESULT。 可能的值包括 (但不限於) 下表中的這些值。
| 值 | 描述 |
|---|---|
| S_OK | 表示作業成功。 |
| ERROR_INVALID_DATA | 表示資料無效 (例如,標頭中的資料太長) 。 |
| ERROR_INVALID_PARAMETER | 表示指定的參數無效 (例如,參數為 Null) 。 |
| ERROR_NOT_ENOUGH_MEMORY | 表示記憶體不足,無法執行作業。 |
備註
方法 SetHeader 會設定目前要求的 HTTP 標頭值。 方法有兩個 SetHeader 多載版本。 其中一個可讓您使用 包含在 參數中的 pszHeaderName 字串來指定標頭。 另一個多載會使用 參數中包含的 ulHeaderIndex 不帶正負號長整數。
參數指定的 pszHeaderName 標頭名稱可以是自訂標頭或標頭,定義于要求批註 (RFC) 1945、「超文字傳輸通訊協定 -- HTTP/1.0」或 RFC 2616「超文字傳輸通訊協定 -- HTTP/1.1」中。
注意
參數 pszHeaderName 無法設定為 Null。
參數 ulHeaderIndex 會指定列舉中所列之 HTTP 標頭的 HTTP_HEADER_ID 識別碼。
注意
列舉 HTTP_HEADER_ID 定義于 Http.h 標頭檔中。
fReplace如果 參數為 true ,則指定的標頭值會在標頭存在時取代現有的標頭值。 如果 fReplace 為 false ,您應該將指定的標頭值附加至現有的標頭,並以逗號分隔值與標頭本身。
範例
下列程式碼範例示範如何使用 方法的 SetHeader 兩個多載來建立 HTTP 模組,以新的值取代 HTTP User-Agent 和 Accept-Language 標頭。
#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;
// Specify the "User-Agent" header name.
char szHeaderName[] = "User-Agent";
// Specify the "User-Agent" header value.
char szUserAgent[] = "Test Browser";
// Specify the "Accept-Language" header value.
char szAcceptLanguage[] = "en-ie";
// Retrieve a pointer to the request.
IHttpRequest * pHttpRequest = pHttpContext->GetRequest();
// Test for an error.
if (pHttpRequest != NULL)
{
// Replace the "User-Agent" header.
hr = pHttpRequest->SetHeader(
szHeaderName,szUserAgent,
(USHORT)strlen(szUserAgent),true);
// Test for an error.
if (FAILED(hr))
{
// Set the error status.
pProvider->SetErrorStatus( hr );
// End additional processing.
return RQ_NOTIFICATION_FINISH_REQUEST;
}
// Replace the "Accept-language" header.
hr = pHttpRequest->SetHeader(
HttpHeaderAcceptLanguage,szAcceptLanguage,
(USHORT)strlen(szAcceptLanguage),true);
// 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;
}
}
/* MERGEFORMAT 16 Aug 07 5:41 PM */
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 介面
IHttpRequest::D eleteHeader 方法
IHttpRequest::GetHeader 方法