Bagikan melalui


Metode IHttpResponse::WriteEntityChunks

Menambahkan satu atau beberapa struktur HTTP_DATA_CHUNK ke isi respons.

Sintaks

virtual HRESULT WriteEntityChunks(  
   IN HTTP_DATA_CHUNK* pDataChunks,  
   IN DWORD nChunks,  
   IN BOOL fAsync,  
   IN BOOL fMoreData,  
   OUT DWORD* pcbSent,  
   OUT BOOL* pfCompletionExpected = NULL  
) = 0;  

Parameter

pDataChunks
[IN] Penunjuk ke satu atau beberapa HTTP_DATA_CHUNK struktur.

nChunks
[IN] yang DWORD berisi jumlah gugus yang ditujukkan oleh pDataChunks.

fAsync
[IN] true jika metode harus selesai secara asinkron; jika tidak, false.

fMoreData
[IN] true jika lebih banyak data akan dikirim dalam respons; false jika ini adalah data terakhir.

pcbSent
[OUT] Jumlah byte yang dikirim ke klien jika panggilan selesai secara sinkron.

pfCompletionExpected
[OUT] true jika penyelesaian asinkron tertunda untuk panggilan ini; jika tidak, false.

Tampilkan Nilai

Sebuah HRESULT. Nilai yang mungkin termasuk, tetapi tidak terbatas pada, yang ada dalam tabel berikut.

Nilai Deskripsi
S_OK Menunjukkan bahwa operasi berhasil.
ERROR_INVALID_PARAMETER Menunjukkan bahwa parameter tidak valid (misalnya, pDataChunks penunjuk diatur ke NULL).
ERROR_NOT_ENOUGH_MEMORY Menunjukkan bahwa memori tidak cukup untuk melakukan operasi.
ERROR_ARITHMETIC_OVERFLOW Lebih dari 65535 potongan telah ditambahkan ke respons.

Keterangan

Pengembang dapat menggunakan WriteEntityChunks metode untuk memasukkan satu HTTP_DATA_CHUNK struktur atau array HTTP_DATA_CHUNK struktur ke dalam isi respons. Jika operasi telah selesai secara sinkron, pcbSent parameter akan menerima jumlah byte yang dimasukkan ke dalam respons.

Jika buffering diaktifkan, WriteEntityChunks metode akan membuat salinan struktur apa pun HTTP_DATA_CHUNK , sehingga menduplikasi data yang mendasarinya sehingga tidak perlu dipertahankan. Jika buffering dimatikan, atau batas buffer respons tercapai, WriteEntityChunks metode juga akan menghapus respons. Jika buffering nonaktif dan fAsync parameternya adalah true, memori harus dipertahankan hingga permintaan selesai.

Anda dapat mengonfigurasi WriteEntityChunks operasi untuk menyelesaikan secara asinkron dengan mengatur parameter ke truefAsync . Dalam situasi ini, WriteEntityChunks metode akan segera kembali ke pemanggil, dan pcbSent parameter tidak akan menerima jumlah byte yang dimasukkan ke dalam respons. Jika buffering dinonaktifkan dan fAsync parameternya adalah true, memori untuk pDataChunks parameter harus dipertahankan hingga panggilan asinkron selesai.

Maksimum 65535 gugus (64 KB dikurangi 1) dapat ditulis ke permintaan.

Contoh

Contoh berikut menunjukkan cara menggunakan WriteEntityChunks metode untuk membuat modul HTTP yang menyisipkan array dari dua gugus data ke dalam respons. Contoh kemudian mengembalikan respons ke klien Web.

#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;
        
        // Create an array of data chunks.
        HTTP_DATA_CHUNK dataChunk[2];

        // Buffer for bytes written of data chunk.
        DWORD cbSent;
        
        // Create string buffers.
        PCSTR pszOne = "First chunk data\n";
        PCSTR pszTwo = "Second chunk data\n";

        // Retrieve a pointer to the response.
        IHttpResponse * pHttpResponse = pHttpContext->GetResponse();

        // Test for an error.
        if (pHttpResponse != NULL)
        {
            // Clear the existing response.
            pHttpResponse->Clear();
            // Set the MIME type to plain text.
            pHttpResponse->SetHeader(
                HttpHeaderContentType,"text/plain",
                (USHORT)strlen("text/plain"),TRUE);
            
            // Set the chunk to a chunk in memory.
            dataChunk[0].DataChunkType = HttpDataChunkFromMemory;
            // Set the chunk to the first buffer.
            dataChunk[0].FromMemory.pBuffer =
                (PVOID) pszOne;
            // Set the chunk size to the first buffer size.
            dataChunk[0].FromMemory.BufferLength =
                (USHORT) strlen(pszOne);

            // Set the chunk to a chunk in memory.
            dataChunk[1].DataChunkType = HttpDataChunkFromMemory;
            // Set the chunk to the second buffer.
            dataChunk[1].FromMemory.pBuffer =
                (PVOID) pszTwo;
            // Set the chunk size to the second buffer size.
            dataChunk[1].FromMemory.BufferLength =
                (USHORT) strlen(pszTwo);

            // Insert the data chunks into the response.
            hr = pHttpResponse->WriteEntityChunks(
                dataChunk,2,FALSE,TRUE,&cbSent);

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

Modul Anda harus mengekspor fungsi RegisterModule . Anda dapat mengekspor fungsi ini dengan membuat file definisi modul (.def) untuk proyek Anda, atau Anda dapat mengkompilasi modul dengan menggunakan sakelar /EXPORT:RegisterModule . Untuk informasi selengkapnya, lihat Panduan: Membuat Modul HTTP Request-Level Dengan Menggunakan Kode Asli.

Anda dapat secara opsional mengkompilasi kode dengan menggunakan __stdcall (/Gz) konvensi panggilan alih-alih secara eksplisit mendeklarasikan konvensi panggilan untuk setiap fungsi.

Persyaratan

Jenis Deskripsi
Klien - IIS 7.0 di Windows Vista
- IIS 7.5 di Windows 7
- IIS 8.0 di Windows 8
- IIS 10.0 pada Windows 10
Server - IIS 7.0 di Windows Server 2008
- IIS 7.5 di Windows Server 2008 R2
- IIS 8.0 di Windows Server 2012
- IIS 8.5 di Windows Server 2012 R2
- IIS 10.0 di Windows Server 2016
Produk - 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

Lihat juga

Antarmuka IHttpResponse
Metode IHttpResponse::WriteEntityChunkByReference