IWinHttpRequest::SetCredentials 方法
SetCredentials 方法设置要与 HTTP 服务器一起使用的凭据,无论是代理服务器还是原始服务器。
语法
HRESULT SetCredentials(
[in] BSTR UserName,
[in] BSTR Password,
[in] HTTPREQUEST_SETCREDENTIALS_FLAGS Flags
);
参数
-
UserName [in]
-
指定用于身份验证的用户名。
-
密码 [in]
-
指定用于身份验证的密码。 如果 bstrUserName 为 NULL 或缺失,则忽略此参数。
-
Flags [in]
-
指定 IWinHttpRequest 何时使用凭据。 可以是以下值之一。
值 含义 - HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
凭据将传递到服务器。 - HTTPREQUEST_SETCREDENTIALS_FOR_PROXY
凭据将传递给代理。
返回值
返回值在成功 时S_OK ,否则返回值为错误值。
备注
如果对 Open 的调用未成功完成,此方法将返回错误值。 假定用户必须先与代理服务器或源服务器进行某种交互,然后用户才能设置会话的凭据。 此外,除非用户知道支持哪个身份验证方案 () ,否则他们无法设置凭据的格式。
注意
对于 Windows XP 和 Windows 2000,请参阅 WinHTTP 起始页的 运行时要求 部分。
若要使用服务器和代理进行身份验证,应用程序必须调用 SetCredentials 两次;首先将 Flags 参数设置为 HTTPREQUEST_SETCREDENTIALS_FOR_SERVER,第二个将 Flags 参数设置为 HTTPREQUEST_SETCREDENTIALS_FOR_PROXY。
示例
以下示例演示如何打开 HTTP 连接、设置服务器的凭据、发送 HTTP 请求以及读取响应文本。 必须从命令提示符运行此示例。
#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;
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);
}
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))
{ // Set Credentials.
BSTR bstrUserName = SysAllocString(L"User Name");
BSTR bstrPassword = SysAllocString(L"Password");
hr = pIWinHttpRequest->SetCredentials(
bstrUserName,
bstrPassword,
HTTPREQUEST_SETCREDENTIALS_FOR_SERVER);
SysFreeString(bstrUserName);
SysFreeString(bstrPassword);
}
if (SUCCEEDED(hr))
{ // Send Request.
hr = pIWinHttpRequest->Send(varEmpty);
}
if (SUCCEEDED(hr))
{ // Get Response text.
hr = pIWinHttpRequest->get_ResponseText(&bstrResponse);
}
if (SUCCEEDED(hr))
{ // Print response to console.
wprintf(L"%.256s",bstrResponse);
}
// Release memory.
if (pIWinHttpRequest)
pIWinHttpRequest->Release();
if (bstrResponse)
SysFreeString(bstrResponse);
CoUninitialize();
return 0;
}
以下脚本示例演示如何打开 HTTP 连接、设置服务器的凭据、设置代理的凭据(如果使用),如何发送 HTTP 请求以及读取响应文本。
// HttpRequest SetCredentials flags
HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0;
HTTPREQUEST_SETCREDENTIALS_FOR_PROXY = 1;
// Instantiate a WinHttpRequest object.
var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
// Specify the target resource.
var targURL = "https://msdn.microsoft.com/downloads/samples/"+
"internet/winhttp/auth/authenticate.asp";
WinHttpReq.open("GET", targURL, false);
var Done = false;
var LastStatus=0;
do {
// Send a request to the server and wait for a response.
WinHttpReq.send();
// Obtain the status code from the response.
var Status = WinHttpReq.Status;
switch (Status){
// A 200 status indicates that the resource was retrieved.
case 200:
Done = true;
break;
// A 401 status indicates that the server
// requires authentication.
case 401:
WScript.Echo("Requires Server UserName and Password.");
// Specify the target resource.
WinHttpReq.open("GET", targURL, false);
// Set credentials for the server.
WinHttpReq.SetCredentials("User Name", "Password",
HTTPREQUEST_SETCREDENTIALS_FOR_SERVER);
// If the same credentials are requested twice, abort
// the request. For simplicity, this sample does not
// check for a repeated sequence of status codes.
if (LastStatus==401)
Done = true;
break;
// A 407 status indicates that the proxy
// requires authentication.
case 407:
WScript.Echo("Requires Proxy UserName and Password.");
// Specify the target resource.
WinHttpReq.open("GET", targURL, false);
// Set credentials for the proxy.
WinHttpReq.SetCredentials("User Name", "Password",
HTTPREQUEST_SETCREDENTIALS_FOR_PROXY);
// If the same credentials are requested twice, abort
// the request. For simplicity, this sample does not
// check for a repeated sequence of status codes.
if (LastStatus==407)
Done = true;
break;
// Any other status is unexpected.
default:
WScript.Echo("Unexpected Status: "+Status);
Done = true;
break;
}
// Keep track of the last status code.
LastStatus = Status;
} while (!Done);
// Display the results of the request.
WScript.Echo(WinHttpReq.Status + " " + WinHttpReq.StatusText);
WScript.Echo(WinHttpReq.GetAllResponseHeaders());
要求
要求 | 值 |
---|---|
最低受支持的客户端 |
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 |
|