Condividi tramite


Metodo IHttpRequest::SetUrl

Modifica l'URL della richiesta.

Sintassi

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;  

Parametri

pszUrl
[IN] Puntatore a una stringa contenente l'URL da impostare.

cchUrl
[IN] Lunghezza, in caratteri, dell'URL specificato da pszUrl.

fResetQueryString
[IN] true per reimpostare la stringa di query esistente; in caso contrario, false.

Valore restituito

Oggetto HRESULT. I valori possibili includono, ma non sono limitati a, quelli indicati nella tabella seguente.

Valore Descrizione
S_OK Indica che l'operazione è riuscita.
ERROR_INVALID_PARAMETER Indica che il parametro specificato non è valido, ad esempio l'URL specificato è troppo lungo.
ERROR_NOT_ENOUGH_MEMORY Indica che memoria insufficiente per eseguire l'operazione.

Commenti

Il SetUrl metodo modifica l'URL per la richiesta corrente. Esistono due versioni di overload del SetUrl metodo . Uno consente di specificare l'intestazione usando un puntatore a una stringa. L'altro overload usa un puntatore a una stringa wide.

Le successive operazioni di elaborazione delle richieste e registrazione elaborano il nuovo URL come se il client avesse richiesto l'URL. Pertanto, tutte le condizioni di errore causate dalla modifica dell'URL verranno restituite al client. Ad esempio, se il nuovo URL non esiste, il server Web restituirà un errore HTTP 404.

Avviso

Il SetUrl metodo viene chiamato dopo la raccolta dei parametri iniziali per la richiesta, pertanto l'elaborazione successiva della richiesta potrebbe non essere a conoscenza dell'URL modificato. Ad esempio, il recupero della variabile del server URL rifletterà la richiesta originale, non l'URL modificato. Gli implementatori devono chiamare il metodo IHttpContext::ExecuteRequest anziché SetUrl per eseguire la richiesta tramite la pipeline completa. Il SetUrl metodo non deve essere usato per la riscrittura dell'URL.

Nota

A differenza del metodo IHttpResponse::Redirect , il SetUrl metodo non reindirizza un client a un nuovo URL.

Nota

È necessario chiamare il SetUrl metodo prima del primo evento nella pipeline di elaborazione delle richieste integrata HTTP. La chiamata al SetUrl metodo da un gestore OnPostBeginRequest comporta un comportamento indeterminato.

Esempio

Nell'esempio di codice seguente viene illustrato come usare il SetUrl metodo per modificare un URL richiesto in un altro 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
    );
}

Requisiti

Tipo Descrizione
Client - IIS 7.0 in Windows Vista
- IIS 7.5 in Windows 7
- IIS 8.0 in Windows 8
- IIS 10.0 in Windows 10
Server - IIS 7.0 in Windows Server 2008
- IIS 7.5 in Windows Server 2008 R2
- IIS 8.0 in Windows Server 2012
- IIS 8.5 in Windows Server 2012 R2
- IIS 10.0 in Windows Server 2016
Prodotto - 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
Intestazione Httpserv.h

Vedere anche

Interfaccia IHttpRequest
Metodo IHttpRequest::GetUrlChanged