IHttpRequest::SetUrl 方法

修改请求 URL。

语法

virtual HRESULT SetUrl(  
   IN PCWSTR pszUrl,  
   IN DWORD cchUrl,  
   IN BOOL fResetQueryString  
) = 0;  
  
virtual HRESULT SetUrl(  
   IN PCSTR pszUrl,  
   IN DWORD cchUrl,  
   IN BOOL fResetQueryString  
) = 0;  

参数

pszUrl
[IN]指向包含要设置的 URL 的字符串的指针。

cchUrl
[IN]指定的 pszUrlURL 的长度(以字符为单位)。

fResetQueryString
[IN] true 重置现有查询字符串;否则为 false。

返回值

HRESULT。 可能的值包括(但并不限于)下表中的项。

值 说明
S_OK 指示操作成功。
ERROR_INVALID_PARAMETER 指示指定的参数无效 (例如,指定的 URL) 太长。
ERROR_NOT_ENOUGH_MEMORY 指示内存不足,无法执行操作。

注解

方法 SetUrl 修改当前请求的 URL。 方法有两个 SetUrl 重载版本。 一个允许你通过使用指向字符串的指针来指定标头。 另一个重载使用指向宽字符串的指针。

后续的请求处理函数和日志记录操作将处理新 URL,就像客户端请求 URL 一样。 因此,修改 URL 导致的任何错误条件都将返回到客户端。 例如,如果新 URL 不存在,Web 服务器将返回 HTTP 404 错误。

警告

在 SetUrl 收集请求的初始参数后调用 方法,因此后续请求处理可能不知道更改的 URL。 例如,检索 URL 服务器变量将反映原始请求,而不是更改的 URL。 实现者应调用 IHttpContext::ExecuteRequest 方法 而不是 SetUrl ,以便通过整个管道运行请求。 不应 SetUrl 使用 方法重写 URL。

注意

与 IHttpResponse::Redirect 方法不同, SetUrl 方法不会将客户端重定向到新的 URL。

注意

必须在 HTTP 集成请求处理管道中的第一个事件之前调用 SetUrl 方法。 SetUrl从 OnPostBeginRequest 处理程序调用 方法会导致行为不确定。

示例

下面的代码示例演示如何使用 SetUrl 方法将请求的 URL 更改为另一个 URL。

#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
    )
    {
        HRESULT hr;

        // Retrieve a pointer to the request.
        IHttpRequest * pHttpRequest = pHttpContext->GetRequest();

        // Test for an error.
        if (pHttpRequest != NULL)
        {
            // Create a buffer with an example URL.
            PCSTR pszBuffer = "/example/default.aspx";
            // Set the URL for the request.
            hr = pHttpRequest->SetUrl(
                pszBuffer,(DWORD)strlen(pszBuffer),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_BEGIN_REQUEST,
        0
    );
}

要求

类型 说明
客户端 - 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::GetUrlChanged 方法