ホーム › Networking.WinHttp › WinHttpSetCredentials
WinHttpSetCredentials
関数HTTP要求に認証用の資格情報を設定する。
シグネチャ
// WINHTTP.dll
#include <windows.h>
BOOL WinHttpSetCredentials(
void* hRequest,
DWORD AuthTargets,
DWORD AuthScheme,
LPCWSTR pwszUserName,
LPCWSTR pwszPassword,
void* pAuthParams
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| hRequest | void* | inout | WinHttpOpenRequest が返した有効な HINTERNET ハンドル。 | ||||||||||||
| AuthTargets | DWORD | in | 認証ターゲットを含むフラグを指定する符号なし整数です。次の表の値のいずれかを指定できます。
| ||||||||||||
| AuthScheme | DWORD | in | 認証スキームを含むフラグを指定する符号なし整数です。 WinHttpQueryAuthSchemes から返される、サポートされている認証スキームのいずれかである必要があります。次の表に指定可能な値を示します。
| ||||||||||||
| pwszUserName | LPCWSTR | in | 有効なユーザー名を含む文字列へのポインター。 | ||||||||||||
| pwszPassword | LPCWSTR | in | 有効なパスワードを含む文字列へのポインター。パスワードは空でもかまいません。 | ||||||||||||
| pAuthParams | void* | inout | このパラメーターは予約されており、NULL である必要があります。 |
戻り値の型: BOOL
公式ドキュメント
WinHttpSetCredentials 関数は、必要な認可資格情報をサーバーに渡します。
戻り値
成功した場合は TRUE、それ以外の場合は FALSE を返します。拡張エラー情報を取得するには、 GetLastError を呼び出します。次の表に返されるエラーコードを示します。
| エラーコード | 説明 |
|---|---|
| 指定されたハンドルが正しい状態にないため、要求された操作を実行できません。 | |
| 指定されたハンドルの種類が、この操作には正しくありません。 | |
| 内部エラーが発生しました。 | |
| 要求された操作を完了するのに十分なメモリがありませんでした (Windows エラーコード)。 |
解説(Remarks)
WinHTTP を非同期モードで使用している場合 (すなわち WinHttpOpen で WINHTTP_FLAG_ASYNC が設定されている場合) でも、この関数は同期的に動作します。戻り値は成功または失敗を示します。拡張エラー情報を取得するには、 GetLastError を呼び出します。
WinHttpSetCredentials で設定された資格情報は、単一の要求に対してのみ使用されます。WinHTTP はこれらの資格情報を後続の要求で使用するためにキャッシュしません。そのため、アプリケーションは複数のチャレンジに応答できるように記述する必要があります。認証済みの接続が再利用される場合、後続の要求はチャレンジされないことがありますが、コードは任意の時点でチャレンジに応答できるようにしておく必要があります。
WinHttpSetCredentials の使用方法を示すサンプルコードについては、Authentication in WinHTTP を参照してください。
注 Passport 認証を使用していて 407 状態コードに応答する場合、WinHTTP アプリケーションはプロキシ資格情報を提供するために WinHttpSetCredentials ではなく WinHttpSetOption を使用する必要があります。これは Passport 認証を使用する場合にのみ当てはまります。それ以外のすべての状況では、WinHttpSetOption は安全性が低いため、WinHttpSetCredentials を使用してください。
注 Windows XP および Windows 2000 については、WinHttp スタートページの Run-Time Requirements セクションを参照してください。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// WINHTTP.dll
#include <windows.h>
BOOL WinHttpSetCredentials(
void* hRequest,
DWORD AuthTargets,
DWORD AuthScheme,
LPCWSTR pwszUserName,
LPCWSTR pwszPassword,
void* pAuthParams
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WINHTTP.dll", SetLastError = true, ExactSpelling = true)]
static extern bool WinHttpSetCredentials(
IntPtr hRequest, // void* in/out
uint AuthTargets, // DWORD
uint AuthScheme, // DWORD
[MarshalAs(UnmanagedType.LPWStr)] string pwszUserName, // LPCWSTR
[MarshalAs(UnmanagedType.LPWStr)] string pwszPassword, // LPCWSTR
IntPtr pAuthParams // void* in/out
);<DllImport("WINHTTP.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WinHttpSetCredentials(
hRequest As IntPtr, ' void* in/out
AuthTargets As UInteger, ' DWORD
AuthScheme As UInteger, ' DWORD
<MarshalAs(UnmanagedType.LPWStr)> pwszUserName As String, ' LPCWSTR
<MarshalAs(UnmanagedType.LPWStr)> pwszPassword As String, ' LPCWSTR
pAuthParams As IntPtr ' void* in/out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' hRequest : void* in/out
' AuthTargets : DWORD
' AuthScheme : DWORD
' pwszUserName : LPCWSTR
' pwszPassword : LPCWSTR
' pAuthParams : void* in/out
Declare PtrSafe Function WinHttpSetCredentials Lib "winhttp" ( _
ByVal hRequest As LongPtr, _
ByVal AuthTargets As Long, _
ByVal AuthScheme As Long, _
ByVal pwszUserName As LongPtr, _
ByVal pwszPassword As LongPtr, _
ByVal pAuthParams As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
WinHttpSetCredentials = ctypes.windll.winhttp.WinHttpSetCredentials
WinHttpSetCredentials.restype = wintypes.BOOL
WinHttpSetCredentials.argtypes = [
ctypes.POINTER(None), # hRequest : void* in/out
wintypes.DWORD, # AuthTargets : DWORD
wintypes.DWORD, # AuthScheme : DWORD
wintypes.LPCWSTR, # pwszUserName : LPCWSTR
wintypes.LPCWSTR, # pwszPassword : LPCWSTR
ctypes.POINTER(None), # pAuthParams : void* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('WINHTTP.dll')
WinHttpSetCredentials = Fiddle::Function.new(
lib['WinHttpSetCredentials'],
[
Fiddle::TYPE_VOIDP, # hRequest : void* in/out
-Fiddle::TYPE_INT, # AuthTargets : DWORD
-Fiddle::TYPE_INT, # AuthScheme : DWORD
Fiddle::TYPE_VOIDP, # pwszUserName : LPCWSTR
Fiddle::TYPE_VOIDP, # pwszPassword : LPCWSTR
Fiddle::TYPE_VOIDP, # pAuthParams : void* in/out
],
Fiddle::TYPE_INT)#[link(name = "winhttp")]
extern "system" {
fn WinHttpSetCredentials(
hRequest: *mut (), // void* in/out
AuthTargets: u32, // DWORD
AuthScheme: u32, // DWORD
pwszUserName: *const u16, // LPCWSTR
pwszPassword: *const u16, // LPCWSTR
pAuthParams: *mut () // void* in/out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WINHTTP.dll", SetLastError = true)]
public static extern bool WinHttpSetCredentials(IntPtr hRequest, uint AuthTargets, uint AuthScheme, [MarshalAs(UnmanagedType.LPWStr)] string pwszUserName, [MarshalAs(UnmanagedType.LPWStr)] string pwszPassword, IntPtr pAuthParams);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WINHTTP_WinHttpSetCredentials' -Namespace Win32 -PassThru
# $api::WinHttpSetCredentials(hRequest, AuthTargets, AuthScheme, pwszUserName, pwszPassword, pAuthParams)#uselib "WINHTTP.dll"
#func global WinHttpSetCredentials "WinHttpSetCredentials" sptr, sptr, sptr, sptr, sptr, sptr
; WinHttpSetCredentials hRequest, AuthTargets, AuthScheme, pwszUserName, pwszPassword, pAuthParams ; 戻り値は stat
; hRequest : void* in/out -> "sptr"
; AuthTargets : DWORD -> "sptr"
; AuthScheme : DWORD -> "sptr"
; pwszUserName : LPCWSTR -> "sptr"
; pwszPassword : LPCWSTR -> "sptr"
; pAuthParams : void* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "WINHTTP.dll"
#cfunc global WinHttpSetCredentials "WinHttpSetCredentials" sptr, int, int, wstr, wstr, sptr
; res = WinHttpSetCredentials(hRequest, AuthTargets, AuthScheme, pwszUserName, pwszPassword, pAuthParams)
; hRequest : void* in/out -> "sptr"
; AuthTargets : DWORD -> "int"
; AuthScheme : DWORD -> "int"
; pwszUserName : LPCWSTR -> "wstr"
; pwszPassword : LPCWSTR -> "wstr"
; pAuthParams : void* in/out -> "sptr"; BOOL WinHttpSetCredentials(void* hRequest, DWORD AuthTargets, DWORD AuthScheme, LPCWSTR pwszUserName, LPCWSTR pwszPassword, void* pAuthParams)
#uselib "WINHTTP.dll"
#cfunc global WinHttpSetCredentials "WinHttpSetCredentials" intptr, int, int, wstr, wstr, intptr
; res = WinHttpSetCredentials(hRequest, AuthTargets, AuthScheme, pwszUserName, pwszPassword, pAuthParams)
; hRequest : void* in/out -> "intptr"
; AuthTargets : DWORD -> "int"
; AuthScheme : DWORD -> "int"
; pwszUserName : LPCWSTR -> "wstr"
; pwszPassword : LPCWSTR -> "wstr"
; pAuthParams : void* in/out -> "intptr"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
winhttp = windows.NewLazySystemDLL("WINHTTP.dll")
procWinHttpSetCredentials = winhttp.NewProc("WinHttpSetCredentials")
)
// hRequest (void* in/out), AuthTargets (DWORD), AuthScheme (DWORD), pwszUserName (LPCWSTR), pwszPassword (LPCWSTR), pAuthParams (void* in/out)
r1, _, err := procWinHttpSetCredentials.Call(
uintptr(hRequest),
uintptr(AuthTargets),
uintptr(AuthScheme),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pwszUserName))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pwszPassword))),
uintptr(pAuthParams),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction WinHttpSetCredentials(
hRequest: Pointer; // void* in/out
AuthTargets: DWORD; // DWORD
AuthScheme: DWORD; // DWORD
pwszUserName: PWideChar; // LPCWSTR
pwszPassword: PWideChar; // LPCWSTR
pAuthParams: Pointer // void* in/out
): BOOL; stdcall;
external 'WINHTTP.dll' name 'WinHttpSetCredentials';result := DllCall("WINHTTP\WinHttpSetCredentials"
, "Ptr", hRequest ; void* in/out
, "UInt", AuthTargets ; DWORD
, "UInt", AuthScheme ; DWORD
, "WStr", pwszUserName ; LPCWSTR
, "WStr", pwszPassword ; LPCWSTR
, "Ptr", pAuthParams ; void* in/out
, "Int") ; return: BOOL●WinHttpSetCredentials(hRequest, AuthTargets, AuthScheme, pwszUserName, pwszPassword, pAuthParams) = DLL("WINHTTP.dll", "bool WinHttpSetCredentials(void*, dword, dword, char*, char*, void*)")
# 呼び出し: WinHttpSetCredentials(hRequest, AuthTargets, AuthScheme, pwszUserName, pwszPassword, pAuthParams)
# hRequest : void* in/out -> "void*"
# AuthTargets : DWORD -> "dword"
# AuthScheme : DWORD -> "dword"
# pwszUserName : LPCWSTR -> "char*"
# pwszPassword : LPCWSTR -> "char*"
# pAuthParams : void* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "winhttp" fn WinHttpSetCredentials(
hRequest: ?*anyopaque, // void* in/out
AuthTargets: u32, // DWORD
AuthScheme: u32, // DWORD
pwszUserName: [*c]const u16, // LPCWSTR
pwszPassword: [*c]const u16, // LPCWSTR
pAuthParams: ?*anyopaque // void* in/out
) callconv(std.os.windows.WINAPI) i32;proc WinHttpSetCredentials(
hRequest: pointer, # void* in/out
AuthTargets: uint32, # DWORD
AuthScheme: uint32, # DWORD
pwszUserName: WideCString, # LPCWSTR
pwszPassword: WideCString, # LPCWSTR
pAuthParams: pointer # void* in/out
): int32 {.importc: "WinHttpSetCredentials", stdcall, dynlib: "WINHTTP.dll".}pragma(lib, "winhttp");
extern(Windows)
int WinHttpSetCredentials(
void* hRequest, // void* in/out
uint AuthTargets, // DWORD
uint AuthScheme, // DWORD
const(wchar)* pwszUserName, // LPCWSTR
const(wchar)* pwszPassword, // LPCWSTR
void* pAuthParams // void* in/out
);ccall((:WinHttpSetCredentials, "WINHTTP.dll"), stdcall, Int32,
(Ptr{Cvoid}, UInt32, UInt32, Cwstring, Cwstring, Ptr{Cvoid}),
hRequest, AuthTargets, AuthScheme, pwszUserName, pwszPassword, pAuthParams)
# hRequest : void* in/out -> Ptr{Cvoid}
# AuthTargets : DWORD -> UInt32
# AuthScheme : DWORD -> UInt32
# pwszUserName : LPCWSTR -> Cwstring
# pwszPassword : LPCWSTR -> Cwstring
# pAuthParams : void* in/out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t WinHttpSetCredentials(
void* hRequest,
uint32_t AuthTargets,
uint32_t AuthScheme,
const uint16_t* pwszUserName,
const uint16_t* pwszPassword,
void* pAuthParams);
]]
local winhttp = ffi.load("winhttp")
-- winhttp.WinHttpSetCredentials(hRequest, AuthTargets, AuthScheme, pwszUserName, pwszPassword, pAuthParams)
-- hRequest : void* in/out
-- AuthTargets : DWORD
-- AuthScheme : DWORD
-- pwszUserName : LPCWSTR
-- pwszPassword : LPCWSTR
-- pAuthParams : void* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('WINHTTP.dll');
const WinHttpSetCredentials = lib.func('__stdcall', 'WinHttpSetCredentials', 'int32_t', ['void *', 'uint32_t', 'uint32_t', 'str16', 'str16', 'void *']);
// WinHttpSetCredentials(hRequest, AuthTargets, AuthScheme, pwszUserName, pwszPassword, pAuthParams)
// hRequest : void* in/out -> 'void *'
// AuthTargets : DWORD -> 'uint32_t'
// AuthScheme : DWORD -> 'uint32_t'
// pwszUserName : LPCWSTR -> 'str16'
// pwszPassword : LPCWSTR -> 'str16'
// pAuthParams : void* in/out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("WINHTTP.dll", {
WinHttpSetCredentials: { parameters: ["pointer", "u32", "u32", "buffer", "buffer", "pointer"], result: "i32" },
});
// lib.symbols.WinHttpSetCredentials(hRequest, AuthTargets, AuthScheme, pwszUserName, pwszPassword, pAuthParams)
// hRequest : void* in/out -> "pointer"
// AuthTargets : DWORD -> "u32"
// AuthScheme : DWORD -> "u32"
// pwszUserName : LPCWSTR -> "buffer"
// pwszPassword : LPCWSTR -> "buffer"
// pAuthParams : void* in/out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t WinHttpSetCredentials(
void* hRequest,
uint32_t AuthTargets,
uint32_t AuthScheme,
const uint16_t* pwszUserName,
const uint16_t* pwszPassword,
void* pAuthParams);
C, "WINHTTP.dll");
// $ffi->WinHttpSetCredentials(hRequest, AuthTargets, AuthScheme, pwszUserName, pwszPassword, pAuthParams);
// hRequest : void* in/out
// AuthTargets : DWORD
// AuthScheme : DWORD
// pwszUserName : LPCWSTR
// pwszPassword : LPCWSTR
// pAuthParams : void* in/out
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Winhttp extends StdCallLibrary {
Winhttp INSTANCE = Native.load("winhttp", Winhttp.class);
boolean WinHttpSetCredentials(
Pointer hRequest, // void* in/out
int AuthTargets, // DWORD
int AuthScheme, // DWORD
WString pwszUserName, // LPCWSTR
WString pwszPassword, // LPCWSTR
Pointer pAuthParams // void* in/out
);
}@[Link("winhttp")]
lib LibWINHTTP
fun WinHttpSetCredentials = WinHttpSetCredentials(
hRequest : Void*, # void* in/out
AuthTargets : UInt32, # DWORD
AuthScheme : UInt32, # DWORD
pwszUserName : UInt16*, # LPCWSTR
pwszPassword : UInt16*, # LPCWSTR
pAuthParams : Void* # void* in/out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef WinHttpSetCredentialsNative = Int32 Function(Pointer<Void>, Uint32, Uint32, Pointer<Utf16>, Pointer<Utf16>, Pointer<Void>);
typedef WinHttpSetCredentialsDart = int Function(Pointer<Void>, int, int, Pointer<Utf16>, Pointer<Utf16>, Pointer<Void>);
final WinHttpSetCredentials = DynamicLibrary.open('WINHTTP.dll')
.lookupFunction<WinHttpSetCredentialsNative, WinHttpSetCredentialsDart>('WinHttpSetCredentials');
// hRequest : void* in/out -> Pointer<Void>
// AuthTargets : DWORD -> Uint32
// AuthScheme : DWORD -> Uint32
// pwszUserName : LPCWSTR -> Pointer<Utf16>
// pwszPassword : LPCWSTR -> Pointer<Utf16>
// pAuthParams : void* in/out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function WinHttpSetCredentials(
hRequest: Pointer; // void* in/out
AuthTargets: DWORD; // DWORD
AuthScheme: DWORD; // DWORD
pwszUserName: PWideChar; // LPCWSTR
pwszPassword: PWideChar; // LPCWSTR
pAuthParams: Pointer // void* in/out
): BOOL; stdcall;
external 'WINHTTP.dll' name 'WinHttpSetCredentials';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "WinHttpSetCredentials"
c_WinHttpSetCredentials :: Ptr () -> Word32 -> Word32 -> CWString -> CWString -> Ptr () -> IO CInt
-- hRequest : void* in/out -> Ptr ()
-- AuthTargets : DWORD -> Word32
-- AuthScheme : DWORD -> Word32
-- pwszUserName : LPCWSTR -> CWString
-- pwszPassword : LPCWSTR -> CWString
-- pAuthParams : void* in/out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let winhttpsetcredentials =
foreign "WinHttpSetCredentials"
((ptr void) @-> uint32_t @-> uint32_t @-> (ptr uint16_t) @-> (ptr uint16_t) @-> (ptr void) @-> returning int32_t)
(* hRequest : void* in/out -> (ptr void) *)
(* AuthTargets : DWORD -> uint32_t *)
(* AuthScheme : DWORD -> uint32_t *)
(* pwszUserName : LPCWSTR -> (ptr uint16_t) *)
(* pwszPassword : LPCWSTR -> (ptr uint16_t) *)
(* pAuthParams : void* in/out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library winhttp (t "WINHTTP.dll"))
(cffi:use-foreign-library winhttp)
(cffi:defcfun ("WinHttpSetCredentials" win-http-set-credentials :convention :stdcall) :int32
(h-request :pointer) ; void* in/out
(auth-targets :uint32) ; DWORD
(auth-scheme :uint32) ; DWORD
(pwsz-user-name (:string :encoding :utf-16le)) ; LPCWSTR
(pwsz-password (:string :encoding :utf-16le)) ; LPCWSTR
(p-auth-params :pointer)) ; void* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $WinHttpSetCredentials = Win32::API::More->new('WINHTTP',
'BOOL WinHttpSetCredentials(LPVOID hRequest, DWORD AuthTargets, DWORD AuthScheme, LPCWSTR pwszUserName, LPCWSTR pwszPassword, LPVOID pAuthParams)');
# my $ret = $WinHttpSetCredentials->Call($hRequest, $AuthTargets, $AuthScheme, $pwszUserName, $pwszPassword, $pAuthParams);
# hRequest : void* in/out -> LPVOID
# AuthTargets : DWORD -> DWORD
# AuthScheme : DWORD -> DWORD
# pwszUserName : LPCWSTR -> LPCWSTR
# pwszPassword : LPCWSTR -> LPCWSTR
# pAuthParams : void* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
公式の関連項目
- f WinHttpCloseHandle — WinHTTPハンドルを閉じてリソースを解放する。
- f WinHttpConnect — 指定サーバーへの接続を確立し接続ハンドルを返す。
- f WinHttpOpen — WinHTTPセッションを初期化しセッションハンドルを返す。
- f WinHttpOpenRequest — HTTP要求ハンドルを作成する。
- f WinHttpQueryAuthSchemes — サーバーが提示する認証方式を問い合わせる。