Share via


Recupero della risposta da un processo upload-reply

Un processo di caricamento-risposta BITS, oltre a caricare un file in un server, esamina anche un URL di risposta inviato come parte della risposta del server e quindi segue automaticamente l'URL di risposta e scarica una risposta da esso. Per altre informazioni sul valore dell'intestazione BITS-Reply-URL, vedere la documentazione di Ack for Fragment .

Impostare il tipo di processo come BG_JOB_TYPE_UPLOAD_REPLY per creare un processo di tipo Upload-Reply. I dati di risposta sono disponibili per il client dopo che il processo entra nello stato BG_JOB_STATE_TRANSFERRED. Per recuperare la risposta, chiamare uno dei metodi seguenti:

Chiamare questi metodi nel metodo IBackgroundCopyCallback::JobTransferred solo se la risposta è piccola e può essere elaborata rapidamente in modo da non bloccare il thread di callback. Se si usa la notifica della riga di comando anziché il callback, passare l'identificatore del processo al file eseguibile. Il file eseguibile usa l'identificatore del processo per chiamare il metodo Complete per rendere disponibile il file di risposta.

Negli esempi seguenti viene illustrato come usare ogni metodo per recuperare i dati di risposta.

Uso di GetReplyData

Nell'esempio seguente viene illustrato come recuperare i dati di risposta usando il metodo IBackgroundCopyJob2::GetReplyData. L'esempio presuppone che il puntatore all'interfaccia IBackgroundCopyJob sia valido, il tipo di processo sia upload-reply e lo stato del processo sia 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.
}

Uso di GetReplyFileName

Nell'esempio seguente viene illustrato come recuperare i dati di risposta usando il metodo IBackgroundCopyJob2::GetReplyFileName. Nell'esempio si presuppone che il puntatore all'interfaccia IBackgroundCopyJob sia valido, il tipo di processo sia upload-reply e lo stato del processo sia 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.
}