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

HttpSendRequestW

関数
指定したHTTPリクエストをサーバーへ送信する(Unicode版)。
DLLWININET.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

// WININET.dll  (Unicode / -W)
#include <windows.h>

BOOL HttpSendRequestW(
    void* hRequest,
    LPCWSTR lpszHeaders,   // optional
    DWORD dwHeadersLength,
    void* lpOptional,   // optional
    DWORD dwOptionalLength
);

パラメーター

名前方向説明
hRequestvoid*inHttpOpenRequest 関数の呼び出しによって返されたハンドル。
lpszHeadersLPCWSTRinoptionalリクエストに付加する追加ヘッダーを格納した null 終端文字列へのポインター。付加する追加ヘッダーがない場合、このパラメーターは NULL にできます。
dwHeadersLengthDWORDin追加ヘッダーのサイズ(TCHAR 単位)。このパラメーターが -1L で、かつ lpszHeadersNULL でない場合、関数は lpszHeaders がゼロ終端(ASCIIZ)であると見なし、長さを計算します。詳細は「解説」を参照してください。
lpOptionalvoid*inoptionalリクエストヘッダーの直後に送信する任意のデータを格納したバッファーへのポインター。このパラメーターは通常、POST 操作および PUT 操作で使用されます。任意のデータには、サーバーへ投稿するリソースや情報を指定できます。送信する任意のデータがない場合、このパラメーターは NULL にできます。
dwOptionalLengthDWORDin任意のデータのサイズ(バイト単位)。送信する任意のデータがない場合、このパラメーターはゼロにできます。

戻り値の型: BOOL

公式ドキュメント

指定したリクエストを HTTP サーバーに送信します。通常 HttpSendRequestEx に渡されるデータに加えて、呼び出し側が追加データを送信できます。(Unicode)

戻り値

成功した場合は TRUE を、それ以外の場合は FALSE を返します。拡張エラー情報を取得するには、GetLastError を呼び出します。

解説(Remarks)

HttpSendRequest は指定したリクエストを HTTP サーバーに送信し、リクエストと共に送信する追加ヘッダーをクライアントが指定できるようにします。

また、リクエストヘッダーの直後に HTTP サーバーへ送信する任意のデータをクライアントが指定することもできます。この機能は通常、PUT や POST などの「書き込み」操作で使用されます。

リクエストの送信後、HTTP サーバーからのステータスコードと応答ヘッダーが読み取られます。これらのヘッダーは内部で保持され、HttpQueryInfo 関数を通じてクライアントアプリケーションから利用できます。

アプリケーションは同一の HTTP リクエストハンドルを HttpSendRequest の複数回の呼び出しで使用できますが、関数を再度呼び出す前に、前回の呼び出しで返されたすべてのデータを読み取る必要があります。

オフラインモードでは、リソースがインターネットキャッシュ内に見つからない場合、HttpSendRequestERROR_FILE_NOT_FOUND を返します。

HttpSendRequest には 2 つのバージョンがあります。HttpSendRequestA(ANSI ビルドで使用)と HttpSendRequestW(Unicode ビルドで使用)です。dwHeadersLength が -1L で、かつ lpszHeadersNULL でない場合、次のようになります。HttpSendRequestA が呼び出された場合、関数は lpszHeaders がゼロ終端(ASCIIZ)であると見なし、長さを計算します。HttpSendRequestW が呼び出された場合、関数は ERROR_INVALID_PARAMETER で失敗します。

HttpSendRequestA 関数はヘッダーを ANSI 文字ではなく ISO-8859-1 文字として表現します。HttpSendRequestW 関数はヘッダーを、ISO-8859-1 文字を UTF-16LE 文字に変換したものとして表現します。その結果、追加するヘッダーが非 ASCII 文字を含む可能性がある場合に HttpSendRequestW 関数を使用することは決して安全ではありません。 代わりに、Codepage パラメーターを 28591 に設定した MultiByteToWideChar および WideCharToMultiByte 関数を使用して、ANSI 文字と UTF-16LE 文字の間のマッピングを行うことができます。
WinINet API の他のすべての側面と同様に、この関数を DllMain 内やグローバルオブジェクトのコンストラクター・デストラクターから安全に呼び出すことはできません。
WinINet はサーバー実装をサポートしていません。また、サービスから使用すべきではありません。サーバー実装やサービスでは Microsoft Windows HTTP Services (WinHTTP) を使用してください。
メモ

wininet.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして HttpSendRequest を定義します。エンコーディング中立なエイリアスの使用を、エンコーディング中立でないコードと混在させると、不一致が生じ、コンパイルエラーや実行時エラーを引き起こす可能性があります。詳細については、関数プロトタイプの規則を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// WININET.dll  (Unicode / -W)
#include <windows.h>

BOOL HttpSendRequestW(
    void* hRequest,
    LPCWSTR lpszHeaders,   // optional
    DWORD dwHeadersLength,
    void* lpOptional,   // optional
    DWORD dwOptionalLength
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WININET.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool HttpSendRequestW(
    IntPtr hRequest,   // void*
    [MarshalAs(UnmanagedType.LPWStr)] string lpszHeaders,   // LPCWSTR optional
    uint dwHeadersLength,   // DWORD
    IntPtr lpOptional,   // void* optional
    uint dwOptionalLength   // DWORD
);
<DllImport("WININET.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function HttpSendRequestW(
    hRequest As IntPtr,   ' void*
    <MarshalAs(UnmanagedType.LPWStr)> lpszHeaders As String,   ' LPCWSTR optional
    dwHeadersLength As UInteger,   ' DWORD
    lpOptional As IntPtr,   ' void* optional
    dwOptionalLength As UInteger   ' DWORD
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hRequest : void*
' lpszHeaders : LPCWSTR optional
' dwHeadersLength : DWORD
' lpOptional : void* optional
' dwOptionalLength : DWORD
Declare PtrSafe Function HttpSendRequestW Lib "wininet" ( _
    ByVal hRequest As LongPtr, _
    ByVal lpszHeaders As LongPtr, _
    ByVal dwHeadersLength As Long, _
    ByVal lpOptional As LongPtr, _
    ByVal dwOptionalLength As Long) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

HttpSendRequestW = ctypes.windll.wininet.HttpSendRequestW
HttpSendRequestW.restype = wintypes.BOOL
HttpSendRequestW.argtypes = [
    ctypes.POINTER(None),  # hRequest : void*
    wintypes.LPCWSTR,  # lpszHeaders : LPCWSTR optional
    wintypes.DWORD,  # dwHeadersLength : DWORD
    ctypes.POINTER(None),  # lpOptional : void* optional
    wintypes.DWORD,  # dwOptionalLength : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WININET.dll')
HttpSendRequestW = Fiddle::Function.new(
  lib['HttpSendRequestW'],
  [
    Fiddle::TYPE_VOIDP,  # hRequest : void*
    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)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "wininet")]
extern "system" {
    fn HttpSendRequestW(
        hRequest: *mut (),  // void*
        lpszHeaders: *const u16,  // LPCWSTR optional
        dwHeadersLength: u32,  // DWORD
        lpOptional: *mut (),  // void* optional
        dwOptionalLength: u32  // DWORD
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WININET.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool HttpSendRequestW(IntPtr hRequest, [MarshalAs(UnmanagedType.LPWStr)] string lpszHeaders, uint dwHeadersLength, IntPtr lpOptional, uint dwOptionalLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WININET_HttpSendRequestW' -Namespace Win32 -PassThru
# $api::HttpSendRequestW(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength)
#uselib "WININET.dll"
#func global HttpSendRequestW "HttpSendRequestW" wptr, wptr, wptr, wptr, wptr
; HttpSendRequestW hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength   ; 戻り値は stat
; hRequest : void* -> "wptr"
; lpszHeaders : LPCWSTR optional -> "wptr"
; dwHeadersLength : DWORD -> "wptr"
; lpOptional : void* optional -> "wptr"
; dwOptionalLength : DWORD -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "WININET.dll"
#cfunc global HttpSendRequestW "HttpSendRequestW" sptr, wstr, int, sptr, int
; res = HttpSendRequestW(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength)
; hRequest : void* -> "sptr"
; lpszHeaders : LPCWSTR optional -> "wstr"
; dwHeadersLength : DWORD -> "int"
; lpOptional : void* optional -> "sptr"
; dwOptionalLength : DWORD -> "int"
; BOOL HttpSendRequestW(void* hRequest, LPCWSTR lpszHeaders, DWORD dwHeadersLength, void* lpOptional, DWORD dwOptionalLength)
#uselib "WININET.dll"
#cfunc global HttpSendRequestW "HttpSendRequestW" intptr, wstr, int, intptr, int
; res = HttpSendRequestW(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength)
; hRequest : void* -> "intptr"
; lpszHeaders : LPCWSTR optional -> "wstr"
; dwHeadersLength : DWORD -> "int"
; lpOptional : void* optional -> "intptr"
; dwOptionalLength : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	wininet = windows.NewLazySystemDLL("WININET.dll")
	procHttpSendRequestW = wininet.NewProc("HttpSendRequestW")
)

// hRequest (void*), lpszHeaders (LPCWSTR optional), dwHeadersLength (DWORD), lpOptional (void* optional), dwOptionalLength (DWORD)
r1, _, err := procHttpSendRequestW.Call(
	uintptr(hRequest),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpszHeaders))),
	uintptr(dwHeadersLength),
	uintptr(lpOptional),
	uintptr(dwOptionalLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function HttpSendRequestW(
  hRequest: Pointer;   // void*
  lpszHeaders: PWideChar;   // LPCWSTR optional
  dwHeadersLength: DWORD;   // DWORD
  lpOptional: Pointer;   // void* optional
  dwOptionalLength: DWORD   // DWORD
): BOOL; stdcall;
  external 'WININET.dll' name 'HttpSendRequestW';
result := DllCall("WININET\HttpSendRequestW"
    , "Ptr", hRequest   ; void*
    , "WStr", lpszHeaders   ; LPCWSTR optional
    , "UInt", dwHeadersLength   ; DWORD
    , "Ptr", lpOptional   ; void* optional
    , "UInt", dwOptionalLength   ; DWORD
    , "Int")   ; return: BOOL
●HttpSendRequestW(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength) = DLL("WININET.dll", "bool HttpSendRequestW(void*, char*, dword, void*, dword)")
# 呼び出し: HttpSendRequestW(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength)
# hRequest : void* -> "void*"
# lpszHeaders : LPCWSTR optional -> "char*"
# dwHeadersLength : DWORD -> "dword"
# lpOptional : void* optional -> "void*"
# dwOptionalLength : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "wininet" fn HttpSendRequestW(
    hRequest: ?*anyopaque, // void*
    lpszHeaders: [*c]const u16, // LPCWSTR optional
    dwHeadersLength: u32, // DWORD
    lpOptional: ?*anyopaque, // void* optional
    dwOptionalLength: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc HttpSendRequestW(
    hRequest: pointer,  # void*
    lpszHeaders: WideCString,  # LPCWSTR optional
    dwHeadersLength: uint32,  # DWORD
    lpOptional: pointer,  # void* optional
    dwOptionalLength: uint32  # DWORD
): int32 {.importc: "HttpSendRequestW", stdcall, dynlib: "WININET.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "wininet");
extern(Windows)
int HttpSendRequestW(
    void* hRequest,   // void*
    const(wchar)* lpszHeaders,   // LPCWSTR optional
    uint dwHeadersLength,   // DWORD
    void* lpOptional,   // void* optional
    uint dwOptionalLength   // DWORD
);
ccall((:HttpSendRequestW, "WININET.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Cwstring, UInt32, Ptr{Cvoid}, UInt32),
      hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength)
# hRequest : void* -> Ptr{Cvoid}
# lpszHeaders : LPCWSTR optional -> Cwstring
# dwHeadersLength : DWORD -> UInt32
# lpOptional : void* optional -> Ptr{Cvoid}
# dwOptionalLength : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t HttpSendRequestW(
    void* hRequest,
    const uint16_t* lpszHeaders,
    uint32_t dwHeadersLength,
    void* lpOptional,
    uint32_t dwOptionalLength);
]]
local wininet = ffi.load("wininet")
-- wininet.HttpSendRequestW(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength)
-- hRequest : void*
-- lpszHeaders : LPCWSTR optional
-- dwHeadersLength : DWORD
-- lpOptional : void* optional
-- dwOptionalLength : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('WININET.dll');
const HttpSendRequestW = lib.func('__stdcall', 'HttpSendRequestW', 'int32_t', ['void *', 'str16', 'uint32_t', 'void *', 'uint32_t']);
// HttpSendRequestW(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength)
// hRequest : void* -> 'void *'
// lpszHeaders : LPCWSTR optional -> 'str16'
// dwHeadersLength : DWORD -> 'uint32_t'
// lpOptional : void* optional -> 'void *'
// dwOptionalLength : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("WININET.dll", {
  HttpSendRequestW: { parameters: ["pointer", "buffer", "u32", "pointer", "u32"], result: "i32" },
});
// lib.symbols.HttpSendRequestW(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength)
// hRequest : void* -> "pointer"
// lpszHeaders : LPCWSTR optional -> "buffer"
// dwHeadersLength : DWORD -> "u32"
// lpOptional : void* optional -> "pointer"
// dwOptionalLength : DWORD -> "u32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t HttpSendRequestW(
    void* hRequest,
    const uint16_t* lpszHeaders,
    uint32_t dwHeadersLength,
    void* lpOptional,
    uint32_t dwOptionalLength);
C, "WININET.dll");
// $ffi->HttpSendRequestW(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength);
// hRequest : void*
// lpszHeaders : LPCWSTR optional
// dwHeadersLength : DWORD
// lpOptional : void* optional
// dwOptionalLength : DWORD
// 構造体/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 Wininet extends StdCallLibrary {
    Wininet INSTANCE = Native.load("wininet", Wininet.class, W32APIOptions.UNICODE_OPTIONS);
    boolean HttpSendRequestW(
        Pointer hRequest,   // void*
        WString lpszHeaders,   // LPCWSTR optional
        int dwHeadersLength,   // DWORD
        Pointer lpOptional,   // void* optional
        int dwOptionalLength   // DWORD
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("wininet")]
lib LibWININET
  fun HttpSendRequestW = HttpSendRequestW(
    hRequest : Void*,   # void*
    lpszHeaders : UInt16*,   # LPCWSTR optional
    dwHeadersLength : UInt32,   # DWORD
    lpOptional : Void*,   # void* optional
    dwOptionalLength : UInt32   # DWORD
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef HttpSendRequestWNative = Int32 Function(Pointer<Void>, Pointer<Utf16>, Uint32, Pointer<Void>, Uint32);
typedef HttpSendRequestWDart = int Function(Pointer<Void>, Pointer<Utf16>, int, Pointer<Void>, int);
final HttpSendRequestW = DynamicLibrary.open('WININET.dll')
    .lookupFunction<HttpSendRequestWNative, HttpSendRequestWDart>('HttpSendRequestW');
// hRequest : void* -> Pointer<Void>
// lpszHeaders : LPCWSTR optional -> Pointer<Utf16>
// dwHeadersLength : DWORD -> Uint32
// lpOptional : void* optional -> Pointer<Void>
// dwOptionalLength : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function HttpSendRequestW(
  hRequest: Pointer;   // void*
  lpszHeaders: PWideChar;   // LPCWSTR optional
  dwHeadersLength: DWORD;   // DWORD
  lpOptional: Pointer;   // void* optional
  dwOptionalLength: DWORD   // DWORD
): BOOL; stdcall;
  external 'WININET.dll' name 'HttpSendRequestW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "HttpSendRequestW"
  c_HttpSendRequestW :: Ptr () -> CWString -> Word32 -> Ptr () -> Word32 -> IO CInt
-- hRequest : void* -> Ptr ()
-- lpszHeaders : LPCWSTR optional -> CWString
-- dwHeadersLength : DWORD -> Word32
-- lpOptional : void* optional -> Ptr ()
-- dwOptionalLength : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let httpsendrequestw =
  foreign "HttpSendRequestW"
    ((ptr void) @-> (ptr uint16_t) @-> uint32_t @-> (ptr void) @-> uint32_t @-> returning int32_t)
(* hRequest : void* -> (ptr void) *)
(* lpszHeaders : LPCWSTR optional -> (ptr uint16_t) *)
(* dwHeadersLength : DWORD -> uint32_t *)
(* lpOptional : void* optional -> (ptr void) *)
(* dwOptionalLength : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library wininet (t "WININET.dll"))
(cffi:use-foreign-library wininet)

(cffi:defcfun ("HttpSendRequestW" http-send-request-w :convention :stdcall) :int32
  (h-request :pointer)   ; void*
  (lpsz-headers (:string :encoding :utf-16le))   ; LPCWSTR optional
  (dw-headers-length :uint32)   ; DWORD
  (lp-optional :pointer)   ; void* optional
  (dw-optional-length :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $HttpSendRequestW = Win32::API::More->new('WININET',
    'BOOL HttpSendRequestW(LPVOID hRequest, LPCWSTR lpszHeaders, DWORD dwHeadersLength, LPVOID lpOptional, DWORD dwOptionalLength)');
# my $ret = $HttpSendRequestW->Call($hRequest, $lpszHeaders, $dwHeadersLength, $lpOptional, $dwOptionalLength);
# hRequest : void* -> LPVOID
# lpszHeaders : LPCWSTR optional -> LPCWSTR
# dwHeadersLength : DWORD -> DWORD
# lpOptional : void* optional -> LPVOID
# dwOptionalLength : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
類似 API