IHttpResponse::SetHeader 方法
设置或追加指定 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;
parameters
pszHeaderName
[IN]指向包含要设置的 HTTP 标头名称的字符串的指针。
ulHeaderIndex
[IN]要设置的 HTTP 标头的 ID。
pszHeaderValue
[IN]指向包含要设置的标头值的字符串的指针。
cchHeaderValue
[IN]标头值的长度(以字符为单位),不包括 \0 字符。
fReplace
[IN]指定是否应覆盖现有标头。
返回值
HRESULT
。 可能的值包括(但并不限于)下表中的项。
值 | 说明 |
---|---|
S_OK | 指示操作成功。 |
ERROR_INVALID_DATA | 指示数据无效 (例如,标头中的数据太长) 。 |
ERROR_INVALID_PARAMETER | 指示指定的参数无效 (例如, 参数设置为 NULL) 。 |
ERROR_NOT_ENOUGH_MEMORY | 指示内存不足,无法执行操作。 |
备注
方法 SetHeader
设置当前响应的 HTTP 标头的值。 方法有两个 SetHeader
重载版本。 第一个允许使用 参数中包含的 pszHeaderName
字符串指定 标头。 另一个重载使用 参数中包含的 ulHeaderIndex
无符号长整数。
注意
不应使用使用 ulHeaderIndex
参数的重载来设置标头的值 Server
,因为值将追加到现有标头值。 pszHeaderName
请改用 参数。
参数指定的 pszHeaderName
标头名称可以是自定义标头,也可以是在请求注释 (RFC) 1945,“超文本传输协议 -- HTTP/1.0”或 RFC 2616“超文本传输协议 -- HTTP/1.1”中定义的标头。
注意
参数 pszHeaderName
不能为 NULL。
参数 ulHeaderIndex
指定枚举中列出的 HTTP 标头的 HTTP_HEADER_ID
ID。
注意
HTTP_HEADER_ID
枚举在 Http.h 头文件中定义。
fReplace
如果 参数为 true
,则指定的标头值将替换现有标头值(如果标头存在)。 如果 fReplace
为 false
,则指定的标头值应追加到现有标头,并使用逗号与标头本身分隔。
注意
其他模块或处理程序可能会调用 SetHeader
方法来替换值,或将值追加到指定的值。
示例
下面的代码示例演示如何使用 方法的 SetHeader
两个重载将 HTTP Content-Type
和 Server
标头替换为自定义值,并将 HTTP Refresh
标头设置为特定的秒数。
#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
)
{
UNREFERENCED_PARAMETER( pProvider );
// Create an HRESULT to receive return values from methods.
HRESULT hr;
// Set the "Refresh" header name.
char szRefreshName[] = "Refresh";
// Set the "Refresh" header value.
char szRefreshValue[] = "30";
// Set the "Content-Type" header value.
char szContentType[] = "text/plain";
// Set the "Server" header value.
char szServerValue[] = "MyServer/7.0";
// Retrieve a pointer to the response.
IHttpResponse * pHttpResponse = pHttpContext->GetResponse();
// Test for an error.
if (pHttpResponse != NULL)
{
// Set the "Refresh" header.
hr = pHttpResponse->SetHeader(
szRefreshName,szRefreshValue,
(USHORT)strlen(szRefreshValue),FALSE);
// Test for an error.
if (FAILED(hr))
{
// Set the error status.
pProvider->SetErrorStatus( hr );
// End additional processing.
return RQ_NOTIFICATION_FINISH_REQUEST;
}
// Set the "Content-Type" header.
hr = pHttpResponse->SetHeader(
HttpHeaderContentType,szContentType,
(USHORT)strlen(szContentType),TRUE);
// Test for an error.
if (FAILED(hr))
{
// Set the error status.
pProvider->SetErrorStatus( hr );
// End additional processing.
return RQ_NOTIFICATION_FINISH_REQUEST;
}
// Set the "Server" header.
hr = pHttpResponse->SetHeader(
"Server",szServerValue,
(USHORT)strlen(szServerValue),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;
}
}
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)
而不是为每个函数显式声明调用约定。
要求
类型 | 说明 |
---|---|
客户端 | - 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 |
Header | Httpserv.h |
另请参阅
IHttpResponse 接口
IHttpResponse::D eleteHeader 方法
IHttpResponse::GetHeader 方法