Bagikan melalui


Metode IHttpResponse::WriteEntityChunkByReference

Menyisipkan atau menambahkan struktur HTTP_DATA_CHUNK ke dalam isi respons.

Sintaks

HRESULT WriteEntityChunkByReference(  
   IN HTTP_DATA_CHUNK* pDataChunk,  
   IN LONG lInsertPosition = -1  
)  

Parameter

pDataChunk
[IN] Penunjuk ke HTTP_DATA_CHUNK struktur.

lInsertPosition
[IN] Nilai LONG yang menentukan apakah akan menyisipkan atau menambahkan gugus.

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, HTTP_DATA_CHUNK penunjuk diatur ke NULL).
ERROR_NOT_ENOUGH_MEMORY Menunjukkan bahwa memori tidak cukup untuk melakukan operasi.
ERROR_ARITHMETIC_OVERFLOW Menunjukkan bahwa lebih dari 65535 gugus telah ditambahkan ke respons.

Keterangan

Metode WriteEntityChunkByReference menyisipkan atau menambahkan HTTP_DATA_CHUNK struktur ke dalam buffer respons tergantung pada nilai lInsertPosition parameter.

  • Jika lInsertPosition adalah 0, data akan disisipkan sebelum data respons yang ada.

  • Jika lInsertPosition adalah -1, data akan ditambahkan setelah potongan terakhir dari data respons yang ada.

    Metode WriteEntityChunkByReference ini menyisipkan referensi ke potongan data asli, bukan salinan, ke dalam buffer respons. Oleh karena itu, memori yang dialokasikan untuk pDataChunk->FromMemory.pBuffer harus bertahan selama pemrosesan respons Anda. Menggunakan memori lokal atau tumpukan menghasilkan perilaku yang tidak ditentukan.

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

Contoh

Contoh berikut menunjukkan cara menggunakan WriteEntityChunkByReference metode untuk menyisipkan data ke dalam respons. Ini juga menunjukkan cara menggunakan lInsertPosition parameter untuk menyisipkan atau menambahkan potongan data.

//  Insert data from ostringstream into the response
//  On error, Provider error status set here
//  ostringstream  buffer cleared for next call 

HRESULT  WECbyRefChunk( std::ostringstream  &os, IHttpContext *pHttpContext, 
                       IHttpEventProvider *pProvider, LONG InsertPosition= -1)
{

    HRESULT hr = S_OK;

    // create convenience string from ostringstream  
    std::string str(os.str());

    HTTP_DATA_CHUNK dc;
    dc.DataChunkType = HttpDataChunkFromMemory;
    dc.FromMemory.BufferLength = static_cast<DWORD>(str.size());
    dc.FromMemory.pBuffer = pHttpContext->AllocateRequestMemory( static_cast<DWORD>( str.size()+1) );

    if(!dc.FromMemory.pBuffer){
        hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
        LOG_ERR_HR(hr,"AllocateRequestMemory");
        pProvider->SetErrorStatus(hr);
        return hr;
    }

    //  use char pointer p for convenience
    char *p = static_cast<char *>(dc.FromMemory.pBuffer);
    strcpy_s(p, str.size()+1, str.c_str());

    hr = pHttpContext->GetResponse()->WriteEntityChunkByReference( &dc, InsertPosition );

    if (FAILED(hr)){
        LOG_ERR_HR(hr,"AllocateRequestMemory");
        pProvider->SetErrorStatus( hr );
    }

    os.str("");                // clear the ostringstream for next call

    return hr;
}  
REQUEST_NOTIFICATION_STATUS
CMyHttpModule::OnBeginRequest(
                              IHttpContext*       pHttpContext,
                              IHttpEventProvider* pProvider
                              )
{
    HRESULT hr;

    static long cnt;               
    InterlockedIncrement (&cnt);  // keep track of how many times we are called
    cnt++;

    IHttpRequest *pRequest = pHttpContext->GetRequest();

    PCWSTR url = pRequest->GetRawHttpRequest()->CookedUrl.pAbsPath;
    OutputDebugStringW( url  );

    // return unless requesting a HTML file

    if( !wcsstr(url, L".htm"))
        return RQ_NOTIFICATION_CONTINUE;

    IHttpResponse * pHttpResponse = pHttpContext->GetResponse();

    // Return most times so we can still view content
    if( (cnt%5) || pHttpResponse == NULL)
        return RQ_NOTIFICATION_CONTINUE;

    TRC_MSG_FULL("HTML  cnt = " << cnt  );

    static int insertPosCnt;
    int insertPos = ++insertPosCnt%2 -1;    // toggle between 0 and -1

    // Use ostringstream to create some dynamic content
    std::ostringstream os; 

    os << "<p /> first chunk  callback count = " << cnt 
        << " insertPos = " << insertPos << "<br />";

    // 
    // WECbyRefChunk does all the work of inserting data into the response
    //

    hr = WECbyRefChunk( os, pHttpContext, pProvider, insertPos);
    if (FAILED(hr))
        return RQ_NOTIFICATION_FINISH_REQUEST;       

    os << "<br /> <b> Adding 2nd chunk in Bold </b> File insertPos = " << insertPos ;
    hr = WECbyRefChunk( os, pHttpContext, pProvider,insertPos);
    if (FAILED(hr))
        return RQ_NOTIFICATION_FINISH_REQUEST;       

    os << " <p /> Last (3rd) Chunk added with default append chunk  GetCurrentThreadId = " 
        << GetCurrentThreadId();
    
    // any errors will be logged/handled in  WECbyRefChunk
    WECbyRefChunk( os, pHttpContext, pProvider);

    // End additional processing, not because of error, but so another request
    // doesn't wipe out our WriteEntityChunkByReference

    return RQ_NOTIFICATION_FINISH_REQUEST;       
}

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::WriteEntityChunks