Win32 API 日本語リファレンス
ホームNetworking.WinHttp › WinHttpSendRequest

WinHttpSendRequest

関数
HTTP要求をサーバーへ送信する。
DLLWINHTTP.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// WINHTTP.dll
#include <windows.h>

BOOL WinHttpSendRequest(
    void* hRequest,
    LPCWSTR lpszHeaders,   // optional
    DWORD dwHeadersLength,
    void* lpOptional,   // optional
    DWORD dwOptionalLength,
    DWORD dwTotalLength,
    UINT_PTR dwContext
);

パラメーター

名前方向
hRequestvoid*inout
lpszHeadersLPCWSTRinoptional
dwHeadersLengthDWORDin
lpOptionalvoid*inoptional
dwOptionalLengthDWORDin
dwTotalLengthDWORDin
dwContextUINT_PTRin

戻り値の型: BOOL

各言語での呼び出し定義

// WINHTTP.dll
#include <windows.h>

BOOL WinHttpSendRequest(
    void* hRequest,
    LPCWSTR lpszHeaders,   // optional
    DWORD dwHeadersLength,
    void* lpOptional,   // optional
    DWORD dwOptionalLength,
    DWORD dwTotalLength,
    UINT_PTR dwContext
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WINHTTP.dll", SetLastError = true, ExactSpelling = true)]
static extern bool WinHttpSendRequest(
    IntPtr hRequest,   // void* in/out
    [MarshalAs(UnmanagedType.LPWStr)] string lpszHeaders,   // LPCWSTR optional
    uint dwHeadersLength,   // DWORD
    IntPtr lpOptional,   // void* optional
    uint dwOptionalLength,   // DWORD
    uint dwTotalLength,   // DWORD
    UIntPtr dwContext   // UINT_PTR
);
<DllImport("WINHTTP.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WinHttpSendRequest(
    hRequest As IntPtr,   ' void* in/out
    <MarshalAs(UnmanagedType.LPWStr)> lpszHeaders As String,   ' LPCWSTR optional
    dwHeadersLength As UInteger,   ' DWORD
    lpOptional As IntPtr,   ' void* optional
    dwOptionalLength As UInteger,   ' DWORD
    dwTotalLength As UInteger,   ' DWORD
    dwContext As UIntPtr   ' UINT_PTR
) As Boolean
End Function
' hRequest : void* in/out
' lpszHeaders : LPCWSTR optional
' dwHeadersLength : DWORD
' lpOptional : void* optional
' dwOptionalLength : DWORD
' dwTotalLength : DWORD
' dwContext : UINT_PTR
Declare PtrSafe Function WinHttpSendRequest Lib "winhttp" ( _
    ByVal hRequest As LongPtr, _
    ByVal lpszHeaders As LongPtr, _
    ByVal dwHeadersLength As Long, _
    ByVal lpOptional As LongPtr, _
    ByVal dwOptionalLength As Long, _
    ByVal dwTotalLength As Long, _
    ByVal dwContext As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WinHttpSendRequest = ctypes.windll.winhttp.WinHttpSendRequest
WinHttpSendRequest.restype = wintypes.BOOL
WinHttpSendRequest.argtypes = [
    ctypes.POINTER(None),  # hRequest : void* in/out
    wintypes.LPCWSTR,  # lpszHeaders : LPCWSTR optional
    wintypes.DWORD,  # dwHeadersLength : DWORD
    ctypes.POINTER(None),  # lpOptional : void* optional
    wintypes.DWORD,  # dwOptionalLength : DWORD
    wintypes.DWORD,  # dwTotalLength : DWORD
    ctypes.c_size_t,  # dwContext : UINT_PTR
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WINHTTP.dll')
WinHttpSendRequest = Fiddle::Function.new(
  lib['WinHttpSendRequest'],
  [
    Fiddle::TYPE_VOIDP,  # hRequest : void* in/out
    Fiddle::TYPE_VOIDP,  # lpszHeaders : LPCWSTR optional
    -Fiddle::TYPE_INT,  # dwHeadersLength : DWORD
    Fiddle::TYPE_VOIDP,  # lpOptional : void* optional
    -Fiddle::TYPE_INT,  # dwOptionalLength : DWORD
    -Fiddle::TYPE_INT,  # dwTotalLength : DWORD
    Fiddle::TYPE_UINTPTR_T,  # dwContext : UINT_PTR
  ],
  Fiddle::TYPE_INT)
#[link(name = "winhttp")]
extern "system" {
    fn WinHttpSendRequest(
        hRequest: *mut (),  // void* in/out
        lpszHeaders: *const u16,  // LPCWSTR optional
        dwHeadersLength: u32,  // DWORD
        lpOptional: *mut (),  // void* optional
        dwOptionalLength: u32,  // DWORD
        dwTotalLength: u32,  // DWORD
        dwContext: usize  // UINT_PTR
    ) -> 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 WinHttpSendRequest(IntPtr hRequest, [MarshalAs(UnmanagedType.LPWStr)] string lpszHeaders, uint dwHeadersLength, IntPtr lpOptional, uint dwOptionalLength, uint dwTotalLength, UIntPtr dwContext);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WINHTTP_WinHttpSendRequest' -Namespace Win32 -PassThru
# $api::WinHttpSendRequest(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength, dwTotalLength, dwContext)
#uselib "WINHTTP.dll"
#func global WinHttpSendRequest "WinHttpSendRequest" sptr, sptr, sptr, sptr, sptr, sptr, sptr
; WinHttpSendRequest hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength, dwTotalLength, dwContext   ; 戻り値は stat
; hRequest : void* in/out -> "sptr"
; lpszHeaders : LPCWSTR optional -> "sptr"
; dwHeadersLength : DWORD -> "sptr"
; lpOptional : void* optional -> "sptr"
; dwOptionalLength : DWORD -> "sptr"
; dwTotalLength : DWORD -> "sptr"
; dwContext : UINT_PTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "WINHTTP.dll"
#cfunc global WinHttpSendRequest "WinHttpSendRequest" sptr, wstr, int, sptr, int, int, sptr
; res = WinHttpSendRequest(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength, dwTotalLength, dwContext)
; hRequest : void* in/out -> "sptr"
; lpszHeaders : LPCWSTR optional -> "wstr"
; dwHeadersLength : DWORD -> "int"
; lpOptional : void* optional -> "sptr"
; dwOptionalLength : DWORD -> "int"
; dwTotalLength : DWORD -> "int"
; dwContext : UINT_PTR -> "sptr"
; BOOL WinHttpSendRequest(void* hRequest, LPCWSTR lpszHeaders, DWORD dwHeadersLength, void* lpOptional, DWORD dwOptionalLength, DWORD dwTotalLength, UINT_PTR dwContext)
#uselib "WINHTTP.dll"
#cfunc global WinHttpSendRequest "WinHttpSendRequest" intptr, wstr, int, intptr, int, int, intptr
; res = WinHttpSendRequest(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength, dwTotalLength, dwContext)
; hRequest : void* in/out -> "intptr"
; lpszHeaders : LPCWSTR optional -> "wstr"
; dwHeadersLength : DWORD -> "int"
; lpOptional : void* optional -> "intptr"
; dwOptionalLength : DWORD -> "int"
; dwTotalLength : DWORD -> "int"
; dwContext : UINT_PTR -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	winhttp = windows.NewLazySystemDLL("WINHTTP.dll")
	procWinHttpSendRequest = winhttp.NewProc("WinHttpSendRequest")
)

// hRequest (void* in/out), lpszHeaders (LPCWSTR optional), dwHeadersLength (DWORD), lpOptional (void* optional), dwOptionalLength (DWORD), dwTotalLength (DWORD), dwContext (UINT_PTR)
r1, _, err := procWinHttpSendRequest.Call(
	uintptr(hRequest),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpszHeaders))),
	uintptr(dwHeadersLength),
	uintptr(lpOptional),
	uintptr(dwOptionalLength),
	uintptr(dwTotalLength),
	uintptr(dwContext),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function WinHttpSendRequest(
  hRequest: Pointer;   // void* in/out
  lpszHeaders: PWideChar;   // LPCWSTR optional
  dwHeadersLength: DWORD;   // DWORD
  lpOptional: Pointer;   // void* optional
  dwOptionalLength: DWORD;   // DWORD
  dwTotalLength: DWORD;   // DWORD
  dwContext: NativeUInt   // UINT_PTR
): BOOL; stdcall;
  external 'WINHTTP.dll' name 'WinHttpSendRequest';
result := DllCall("WINHTTP\WinHttpSendRequest"
    , "Ptr", hRequest   ; void* in/out
    , "WStr", lpszHeaders   ; LPCWSTR optional
    , "UInt", dwHeadersLength   ; DWORD
    , "Ptr", lpOptional   ; void* optional
    , "UInt", dwOptionalLength   ; DWORD
    , "UInt", dwTotalLength   ; DWORD
    , "UPtr", dwContext   ; UINT_PTR
    , "Int")   ; return: BOOL
●WinHttpSendRequest(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength, dwTotalLength, dwContext) = DLL("WINHTTP.dll", "bool WinHttpSendRequest(void*, char*, dword, void*, dword, dword, int)")
# 呼び出し: WinHttpSendRequest(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength, dwTotalLength, dwContext)
# hRequest : void* in/out -> "void*"
# lpszHeaders : LPCWSTR optional -> "char*"
# dwHeadersLength : DWORD -> "dword"
# lpOptional : void* optional -> "void*"
# dwOptionalLength : DWORD -> "dword"
# dwTotalLength : DWORD -> "dword"
# dwContext : UINT_PTR -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。