Bagikan melalui


Mengambil Balasan dari Pekerjaan Pengunggahan Balasan

Pekerjaan BITS Upload-Reply, selain mengunggah file ke server, juga akan memeriksa URL balasan yang dikirim sebagai bagian dari balasan server dan kemudian secara otomatis mengikuti URL balasan dan mengunduh respons darinya. Lihat dokumentasi Ack for Fragment untuk detail selengkapnya tentang nilai header BITS-Reply-URL.

Atur jenis pekerjaan sebagai BG_JOB_TYPE_UPLOAD_REPLY untuk membuat pekerjaan jenis Upload-Reply. Data balasan tersedia untuk klien setelah pekerjaan memasuki status BG_JOB_STATE_TRANSFERRED. Untuk mengambil balasan, panggil salah satu metode berikut:

Panggil metode ini di metode IBackgroundCopyCallback::JobTransferred Anda hanya jika balasannya kecil dan dapat diproses dengan cepat agar tidak memblokir utas panggilan balik. Jika Anda menggunakan pemberitahuan baris perintah alih-alih panggilan balik, teruskan pengidentifikasi pekerjaan ke file yang dapat dieksekusi. File yang dapat dieksekusi menggunakan pengidentifikasi pekerjaan untuk memanggil metode Lengkap untuk membuat file balasan tersedia.

Contoh berikut menunjukkan cara menggunakan setiap metode untuk mengambil data balasan.

Menggunakan GetReplyData

Contoh berikut menunjukkan cara mengambil data balasan menggunakan metode IBackgroundCopyJob2::GetReplyData. Contoh mengasumsikan penunjuk antarmuka IBackgroundCopyJob valid, jenis pekerjaan adalah balasan unggahan, dan status pekerjaan BG_JOB_STATE_TRANSFERRED.

HRESULT hr;
IBackgroundCopyJob* pJob;
IBackgroundCopyJob2* pJob2 = NULL;
BYTE* pReply = NULL;
UINT64 ReplySize;

//Need to query the IBackgroundCopyJob interface for an IBackgroundCopyJob2
//interface pointer. The IBackgroundCopyJob2 interface contains the GetReplyData method.
hr = pJob->QueryInterface(__uuidof(IBackgroundCopyJob2), (void**)&pJob2);
if (SUCCEEDED(hr))
{
    hr = pJob2->GetReplyData(&pReply, &ReplySize);
    if (S_OK == hr))
    {
        if (pReply)
        {
            //Do something with the data.
            CoTaskMemFree(pReply);
        }
        else
        {
            //The server application did not return a reply.
        }
    }
    else if (BG_E_TOO_LARGE == hr)
    {
        //The reply exceeds 1 MB. To retrieve the reply, get the reply file name,
        //complete the job, open the reply file, and read the reply.
    }
    else
    {
        //Handle the error
    }

    pJob2->Release(); //When done, release the interface.
}
else
{
    //Handle error. QueryInterface will return E_NOINTERFACE if the version of BITS
    //running on the computer is less than BITS 1.5.
}

Menggunakan GetReplyFileName

Contoh berikut menunjukkan cara mengambil data balasan menggunakan metode IBackgroundCopyJob2::GetReplyFileName. Contohnya mengasumsikan penunjuk antarmuka IBackgroundCopyJob valid, jenis pekerjaan adalah balasan unggahan, dan status pekerjaan BG_JOB_STATE_TRANSFERRED.

HRESULT hr;
IBackgroundCopyJob* pJob;
IBackgroundCopyJob2* pJob2 = NULL;
WCHAR* pszFileName = NULL;

//Need to query the IBackgroundCopyJob interface for an IBackgroundCopyJob2
//interface pointer. The IBackgroundCopyJob2 interface contains the GetReplyFileName method.
hr = pJob->QueryInterface(__uuidof(IBackgroundCopyJob2), (void**)&pJob2);
if (SUCCEEDED(hr))
{
    hr = pJob2->GetReplyFileName(&pszFileName);
    if (SUCCEEDED(hr))
    {
        //Calling the Complete method removes the job from the queue, 
        //so make sure you maintain an interface pointer to this job 
        //or retrieve any job related information that you require 
        //when processing the reply.
        hr = pJob->Complete();

        //Open, read the file, and do something with the data.
        CoTaskMemFree(pszFileName);
    }

    pJob2->Release(); //When done, release the interface.
}
else
{
    //Handle error. QueryInterface will return E_NOINTERFACE if the version of BITS
    //running on the computer is less than BITS 1.5.
}