Properti IWinHttpRequest::ResponseStream
Properti ResponseStream mengambil isi entitas respons sebagai IStream.
Properti ini bersifat hanya baca.
Sintaks
HRESULT get_ResponseStream(
[out, retval] VARIANT *Body
);
vtResponseStream = WinHttpRequest.ResponseStream
Nilai properti
Varian yang menerima pointer ke antarmuka IUnknown yang dapat dikueri untuk antarmuka IStream. Aliran ini mengembalikan data mentah seperti yang diterima langsung dari server.
Kode kesalahan
Nilai yang dikembalikan S_OK pada keberhasilan atau nilai kesalahan jika tidak.
Ini akan E_PENDING jika operasi Kirim sebelumnya tidak selesai.
Keterangan
Panggil QueryInterface pada penunjuk yang dikembalikan untuk mendapatkan penunjuk ke antarmuka IStream . Properti ini mengembalikan data respons sebagai IStream. Properti ini hanya dapat dipanggil setelah metode Kirim dipanggil.
Catatan
Untuk Windows XP dan Windows 2000, lihat bagian Persyaratan Run-Time di Halaman Mulai WinHTTP.
Contoh
Contoh berikut menunjukkan cara membuka koneksi HTTP, mengirim permintaan HTTP, dan membaca respons sebagai IStream. Data dari IStream ditulis ke file Temp1.gif.
#include <windows.h>
#include <stdio.h>
#include <objbase.h>
#include "httprequest.h"
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")
// IID for IWinHttpRequest.
const IID IID_IWinHttpRequest =
{
0x06f29373,
0x5c5a,
0x4b54,
{0xb0, 0x25, 0x6e, 0xf1, 0xbf, 0x8a, 0xbf, 0x0e}
};
#define Trace(_s) fprintf(stderr, _s)
#define TraceErr(_s, _hr) fprintf(stderr, _s, _hr)
#define Check(_hr,_s) if (FAILED(_hr)) { fprintf(stderr, _s ## " 0x%08lx\n", _hr); return -1; }
int main(int argc, char* argv[])
{
UNREFERENCED_PARAMETER(argc);
UNREFERENCED_PARAMETER(argv);
// Variable for return value.
HRESULT hr;
// Initialize COM.
hr = CoInitialize( NULL );
IWinHttpRequest * pIWinHttpRequest = NULL;
BSTR bstrResponse = NULL;
VARIANT varFalse;
VARIANT varEmpty;
VARIANT varResponse;
VariantInit(&varResponse);
CLSID clsid;
VariantInit(&varFalse);
V_VT(&varFalse) = VT_BOOL;
V_BOOL(&varFalse) = VARIANT_FALSE;
VariantInit(&varEmpty);
V_VT(&varEmpty) = VT_ERROR;
hr = CLSIDFromProgID(L"WinHttp.WinHttpRequest.5.1", &clsid);
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(clsid, NULL,
CLSCTX_INPROC_SERVER,
IID_IWinHttpRequest,
(void **)&pIWinHttpRequest);
}
// ==== Get binary (.gif) file and write it to disk. =========
if (SUCCEEDED(hr))
{ // Open WinHttpRequest for synchronous access.
BSTR bstrMethod = SysAllocString(L"GET");
BSTR bstrUrl = SysAllocString(
L"https://www.microsoft.com/library/homepage/images/ms-banner.gif");
hr = pIWinHttpRequest->Open(bstrMethod, bstrUrl, varFalse);
SysFreeString(bstrMethod);
SysFreeString(bstrUrl);
}
if (SUCCEEDED(hr))
{ // Send Request.
hr = pIWinHttpRequest->Send(varEmpty);
}
if (SUCCEEDED(hr))
{ // Get response body.
hr = pIWinHttpRequest->get_ResponseBody(&varResponse);
}
if (SUCCEEDED(hr))
{ // Print response to file temp1.gif.
long UpperBounds;
long LowerBounds;
unsigned char* buff;
// Verify that varResponse contains array of unsigned bytes.
if (varResponse.vt == (VT_ARRAY | VT_UI1)) {
long Dims = SafeArrayGetDim(varResponse.parray);
// The array should only have 1 dimension.
if (Dims == 1) {
// Get upper and lower array bounds.
SafeArrayGetLBound(varResponse.parray, 1,
&LowerBounds);
SafeArrayGetUBound(varResponse.parray, 1,
&UpperBounds);
UpperBounds++;
// Lock SAFEARRAY for access.
SafeArrayAccessData (varResponse.parray,
(void**)&buff);
// Output array to file temp.gif.
HANDLE hFile;
DWORD dwBytesWritten;
// Create file.
hFile = CreateFile(TEXT("Temp1.gif"),
GENERIC_WRITE, // Open for writing.
0, // Do not share.
NULL, // No security.
CREATE_ALWAYS, // Overwrite existing.
FILE_ATTRIBUTE_NORMAL, // Normal file.
NULL); // No attribute template.
if (hFile == INVALID_HANDLE_VALUE)
{
wprintf(L"Could not open file."); // Process error.
}
else
{
WriteFile(hFile, buff, UpperBounds - LowerBounds,
&dwBytesWritten, NULL);
}
CloseHandle(hFile);
SafeArrayUnaccessData (varResponse.parray);
}
}
}
// ======== Get binary (.gif) file and write
// it to disk using IStream. ================
if (SUCCEEDED(hr))
{ // Open WinHttpRequest for synchronous access.
BSTR bstrMethod = SysAllocString(L"GET");
BSTR bstrUrl = SysAllocString(
L"https://www.microsoft.com/library/homepage/images/ms-banner.gif");
hr = pIWinHttpRequest->Open(bstrMethod, bstrUrl, varFalse);
SysFreeString(bstrMethod);
SysFreeString(bstrUrl);
}
if (SUCCEEDED(hr))
{ // Send Request.
hr = pIWinHttpRequest->Send(varEmpty);
}
if (SUCCEEDED(hr))
{ // Get Response as an IStream.
hr = pIWinHttpRequest->get_ResponseStream(&varResponse);
}
if (SUCCEEDED(hr))
{ // Print response to file temp1.gif.
IStream* pStream = NULL;
BYTE bBuffer[8192];
DWORD cb, cbRead, cbWritten;
// Check that an IStream was received.
if (VT_UNKNOWN == V_VT(&varResponse) ||
VT_STREAM == V_VT(&varResponse))
{
// Get IStream interface pStream.
hr = V_UNKNOWN(&varResponse)->QueryInterface(IID_IStream,
reinterpret_cast<void**>(&pStream));
Check(hr, "QI for IStream");
}
else
{
wprintf(L"Unexpected vartype for Response\n");
return -1;
}
HANDLE hFile;
// Open file Temp2.gif for output.
hFile = CreateFile(TEXT("Temp2.gif"),
GENERIC_WRITE, // Open for writing.
0, // Do not share.
NULL, // No security.
CREATE_ALWAYS, // Overwrite existing.
FILE_ATTRIBUTE_NORMAL, // Normal file.
NULL); // No attribute template.
if (hFile == INVALID_HANDLE_VALUE)
{
wprintf(L"Could not open file."); // Process error.
}
else
{
// Copy data from the response stream to file.
cb = sizeof(bBuffer);
hr = pStream->Read(bBuffer, cb, &cbRead);
while (SUCCEEDED(hr) && 0 != cbRead)
{
if (!WriteFile(hFile, bBuffer,
cbRead, &cbWritten, NULL))
{
TraceErr("WriteFile fails with 0x%08lx\n",
HRESULT_FROM_WIN32(GetLastError()));
return -1;
}
hr = pStream->Read(bBuffer, cb, &cbRead);
}
}
CloseHandle(hFile);
pStream->Release();
VariantClear(&varResponse);
}
// Release memory.
if (pIWinHttpRequest)
pIWinHttpRequest->Release();
if (bstrResponse)
SysFreeString(bstrResponse);
CoUninitialize();
return 0;
}
Persyaratan
Persyaratan | Nilai |
---|---|
Klien minimum yang didukung |
Windows XP, Windows 2000 Professional dengan SP3 [hanya aplikasi desktop] |
Server minimum yang didukung |
Windows Server 2003, Windows 2000 Server dengan SP3 [hanya aplikasi desktop] |
Redistribusi |
WinHTTP 5.0 dan Internet Explorer 5.01 atau yang lebih baru pada Windows XP dan Windows 2000. |
IDL |
|
Pustaka |
|
DLL |
|