IWinHttpRequest::Option 属性
Option 属性设置或检索 Microsoft Windows HTTP Services (WinHTTP) 选项值。
此属性是可读写的。
语法
HRESULT put_Option(
[in] WinHttpRequestOption Option,
[in] VARIANT Value
);
HRESULT get_Option(
[in] WinHttpRequestOption Option,
[out, retval] VARIANT *Value
);
vtOption = WinHttpRequest.Option
WinHttpRequest.Option = vtOption
属性值
包含选项值的 Variant 值。
Option 参数指定要设置或检索的 WinHttpRequestOption。
错误代码
返回值在成功 时S_OK ,否则返回值为错误值。
备注
注意
对于 Windows XP 和 Windows 2000,请参阅 WinHTTP 起始页的 运行时要求 部分。
示例
以下示例演示如何设置和查看 用户代理 字符串选项以及查看其他各种选项。
#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}
};
int main()
{
// Variable for return value
HRESULT hr;
// Initialize COM
hr = CoInitialize( NULL );
IWinHttpRequest * pIWinHttpRequest = NULL;
BSTR bstrResponse = NULL;
VARIANT varFalse;
VARIANT varEmpty;
VARIANT varUserAgent;
CLSID clsid;
VariantInit(&varFalse);
V_VT(&varFalse) = VT_BOOL;
V_BOOL(&varFalse) = VARIANT_FALSE;
VariantInit(&varEmpty);
V_VT(&varEmpty) = VT_ERROR;
varUserAgent.vt = VT_BSTR;
varUserAgent.bstrVal = NULL;
hr = CLSIDFromProgID(L"WinHttp.WinHttpRequest.5.1", &clsid);
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(clsid,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWinHttpRequest,
(void **)&pIWinHttpRequest);
}
if (SUCCEEDED(hr))
{
// Open WinHttpRequest.
BSTR bstrMethod = SysAllocString(L"GET");
BSTR bstrUrl = SysAllocString(L"https://microsoft.com");
hr = pIWinHttpRequest->Open(bstrMethod,
bstrUrl,
varFalse);
SysFreeString(bstrMethod);
SysFreeString(bstrUrl);
}
if (SUCCEEDED(hr))
{
// Specify the user agent.
varUserAgent.vt = VT_BSTR;
varUserAgent.bstrVal =
SysAllocString(
L"A WinHttpRequest Example Program");
//varUserAgent is L"A WinHttpRequest Example Program");
hr = pIWinHttpRequest->put_Option(
WinHttpRequestOption_UserAgentString,
varUserAgent);
}
if (SUCCEEDED(hr))
{ // Send Request.
hr = pIWinHttpRequest->Send(varEmpty);
}
if (SUCCEEDED(hr))
{
// Get user agent string.
hr = pIWinHttpRequest->get_Option(
WinHttpRequestOption_UserAgentString,
&varUserAgent);
// Print response to console.
wprintf(L"%s\n\n", varUserAgent.bstrVal);
// Get URL.
hr = pIWinHttpRequest->get_Option(WinHttpRequestOption_URL,
&varUserAgent);
// Print response to console.
wprintf(L"%s\n\n", varUserAgent.bstrVal);
// Get URL Code Page.
hr = pIWinHttpRequest->get_Option(
WinHttpRequestOption_URLCodePage,
&varUserAgent);
// Print response to console.
wprintf(L"%u\n\n", varUserAgent.lVal);
// Convert percent symbols to escape sequences.
hr = pIWinHttpRequest->get_Option(
WinHttpRequestOption_EscapePercentInURL,
&varUserAgent);
// Print response to console.
wprintf(L"%s\n", varUserAgent.boolVal?L"True":L"False");
}
// Release memory.
if (pIWinHttpRequest)
pIWinHttpRequest->Release();
if (bstrResponse)
SysFreeString(bstrResponse);
if (varUserAgent.bstrVal)
SysFreeString(varUserAgent.bstrVal);
CoUninitialize();
return 0;
}
以下脚本示例演示如何设置和查看选项。
// Define the constants used by the option property.
WinHttpRequestOption_UserAgentString = 0; // Name of the user agent
WinHttpRequestOption_URL = 1; // Current URL
WinHttpRequestOption_URLCodePage = 2; // Code page
WinHttpRequestOption_EscapePercentInURL = 3; // Convert percents
// in the URL
// Instantiate a WinHttpRequest object.
var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
// Initialize an HTTP request.
WinHttpReq.Open("GET", "https://www.microsoft.com", false);
// Send the HTTP request.
WinHttpReq.Send();
// Display the WinHTTP option values.
WScript.Echo( 'User agent: '+
WinHttpReq.Option(WinHttpRequestOption_UserAgentString));
WScript.Echo( 'URL: '+
WinHttpReq.Option(WinHttpRequestOption_URL));
WScript.Echo( 'Code page: '+
WinHttpReq.Option(WinHttpRequestOption_URLCodePage));
WScript.Echo( 'Escape percents: '+
WinHttpReq.Option(WinHttpRequestOption_EscapePercentInURL));
要求
要求 | 值 |
---|---|
最低受支持的客户端 |
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 |
|