IWinHttpRequest::ResponseStream 属性
ResponseStream 属性将响应实体正文检索为 IStream。
此属性为只读。
语法
HRESULT get_ResponseStream(
[out, retval] VARIANT *Body
);
vtResponseStream = WinHttpRequest.ResponseStream
属性值
一个 Variant 类型,接收指向 IUnknown 接口的指针,该接口可查询 IStream 接口。 此流返回从服务器直接接收的原始数据。
错误代码
返回值在成功时 S_OK ,否则返回值为错误值。
如果上一个发送操作未完成,则会E_PENDING。
备注
对返回的指针调用 QueryInterface 以获取指向 IStream 接口的指针。 此属性以 IStream 的形式返回响应数据。 只能在调用 Send 方法后调用此属性。
注意
对于 Windows XP 和 Windows 2000,请参阅 WinHTTP 起始页的 运行时要求 部分。
示例
以下示例演示如何打开 HTTP 连接、发送 HTTP 请求,以及如何以 IStream 的形式读取响应。 IStream 中的数据将写入文件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;
}
要求
要求 | 值 |
---|---|
最低受支持的客户端 |
Windows XP、Windows 2000 Professional 和 SP3 [仅限桌面应用] |
最低受支持的服务器 |
Windows Server 2003、Windows 2000 Server SP3 [仅限桌面应用] |
可再发行组件 |
Windows XP 和 Windows 2000 上的 WinHTTP 5.0 和 Internet Explorer 5.01 或更高版本。 |
IDL |
|
库 |
|
DLL |
|