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

HttpSendRequestA

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

シグネチャ

// WININET.dll  (ANSI / -A)
#include <windows.h>

BOOL HttpSendRequestA(
    void* hRequest,
    LPCSTR lpszHeaders,   // optional
    DWORD dwHeadersLength,
    void* lpOptional,   // optional
    DWORD dwOptionalLength
);

パラメーター

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

戻り値の型: BOOL

公式ドキュメント

指定されたリクエストを HTTP サーバーに送信します。通常 HttpSendRequestEx に渡されるデータを超える追加データを送信できます。(ANSI)

戻り値

成功した場合は 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  (ANSI / -A)
#include <windows.h>

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

HttpSendRequestA = ctypes.windll.wininet.HttpSendRequestA
HttpSendRequestA.restype = wintypes.BOOL
HttpSendRequestA.argtypes = [
    ctypes.POINTER(None),  # hRequest : void*
    wintypes.LPCSTR,  # lpszHeaders : LPCSTR 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')
HttpSendRequestA = Fiddle::Function.new(
  lib['HttpSendRequestA'],
  [
    Fiddle::TYPE_VOIDP,  # hRequest : void*
    Fiddle::TYPE_VOIDP,  # lpszHeaders : LPCSTR optional
    -Fiddle::TYPE_INT,  # dwHeadersLength : DWORD
    Fiddle::TYPE_VOIDP,  # lpOptional : void* optional
    -Fiddle::TYPE_INT,  # dwOptionalLength : DWORD
  ],
  Fiddle::TYPE_INT)
#[link(name = "wininet")]
extern "system" {
    fn HttpSendRequestA(
        hRequest: *mut (),  // void*
        lpszHeaders: *const u8,  // LPCSTR 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.Ansi, SetLastError = true)]
public static extern bool HttpSendRequestA(IntPtr hRequest, [MarshalAs(UnmanagedType.LPStr)] string lpszHeaders, uint dwHeadersLength, IntPtr lpOptional, uint dwOptionalLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WININET_HttpSendRequestA' -Namespace Win32 -PassThru
# $api::HttpSendRequestA(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength)
#uselib "WININET.dll"
#func global HttpSendRequestA "HttpSendRequestA" sptr, sptr, sptr, sptr, sptr
; HttpSendRequestA hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength   ; 戻り値は stat
; hRequest : void* -> "sptr"
; lpszHeaders : LPCSTR optional -> "sptr"
; dwHeadersLength : DWORD -> "sptr"
; lpOptional : void* optional -> "sptr"
; dwOptionalLength : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "WININET.dll"
#cfunc global HttpSendRequestA "HttpSendRequestA" sptr, str, int, sptr, int
; res = HttpSendRequestA(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength)
; hRequest : void* -> "sptr"
; lpszHeaders : LPCSTR optional -> "str"
; dwHeadersLength : DWORD -> "int"
; lpOptional : void* optional -> "sptr"
; dwOptionalLength : DWORD -> "int"
; BOOL HttpSendRequestA(void* hRequest, LPCSTR lpszHeaders, DWORD dwHeadersLength, void* lpOptional, DWORD dwOptionalLength)
#uselib "WININET.dll"
#cfunc global HttpSendRequestA "HttpSendRequestA" intptr, str, int, intptr, int
; res = HttpSendRequestA(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength)
; hRequest : void* -> "intptr"
; lpszHeaders : LPCSTR optional -> "str"
; dwHeadersLength : DWORD -> "int"
; lpOptional : void* optional -> "intptr"
; dwOptionalLength : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	wininet = windows.NewLazySystemDLL("WININET.dll")
	procHttpSendRequestA = wininet.NewProc("HttpSendRequestA")
)

// hRequest (void*), lpszHeaders (LPCSTR optional), dwHeadersLength (DWORD), lpOptional (void* optional), dwOptionalLength (DWORD)
r1, _, err := procHttpSendRequestA.Call(
	uintptr(hRequest),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpszHeaders))),
	uintptr(dwHeadersLength),
	uintptr(lpOptional),
	uintptr(dwOptionalLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function HttpSendRequestA(
  hRequest: Pointer;   // void*
  lpszHeaders: PAnsiChar;   // LPCSTR optional
  dwHeadersLength: DWORD;   // DWORD
  lpOptional: Pointer;   // void* optional
  dwOptionalLength: DWORD   // DWORD
): BOOL; stdcall;
  external 'WININET.dll' name 'HttpSendRequestA';
result := DllCall("WININET\HttpSendRequestA"
    , "Ptr", hRequest   ; void*
    , "AStr", lpszHeaders   ; LPCSTR optional
    , "UInt", dwHeadersLength   ; DWORD
    , "Ptr", lpOptional   ; void* optional
    , "UInt", dwOptionalLength   ; DWORD
    , "Int")   ; return: BOOL
●HttpSendRequestA(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength) = DLL("WININET.dll", "bool HttpSendRequestA(void*, char*, dword, void*, dword)")
# 呼び出し: HttpSendRequestA(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength)
# hRequest : void* -> "void*"
# lpszHeaders : LPCSTR optional -> "char*"
# dwHeadersLength : DWORD -> "dword"
# lpOptional : void* optional -> "void*"
# dwOptionalLength : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "wininet" fn HttpSendRequestA(
    hRequest: ?*anyopaque, // void*
    lpszHeaders: [*c]const u8, // LPCSTR optional
    dwHeadersLength: u32, // DWORD
    lpOptional: ?*anyopaque, // void* optional
    dwOptionalLength: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;
proc HttpSendRequestA(
    hRequest: pointer,  # void*
    lpszHeaders: cstring,  # LPCSTR optional
    dwHeadersLength: uint32,  # DWORD
    lpOptional: pointer,  # void* optional
    dwOptionalLength: uint32  # DWORD
): int32 {.importc: "HttpSendRequestA", stdcall, dynlib: "WININET.dll".}
pragma(lib, "wininet");
extern(Windows)
int HttpSendRequestA(
    void* hRequest,   // void*
    const(char)* lpszHeaders,   // LPCSTR optional
    uint dwHeadersLength,   // DWORD
    void* lpOptional,   // void* optional
    uint dwOptionalLength   // DWORD
);
ccall((:HttpSendRequestA, "WININET.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Cstring, UInt32, Ptr{Cvoid}, UInt32),
      hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength)
# hRequest : void* -> Ptr{Cvoid}
# lpszHeaders : LPCSTR optional -> Cstring
# dwHeadersLength : DWORD -> UInt32
# lpOptional : void* optional -> Ptr{Cvoid}
# dwOptionalLength : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t HttpSendRequestA(
    void* hRequest,
    const char* lpszHeaders,
    uint32_t dwHeadersLength,
    void* lpOptional,
    uint32_t dwOptionalLength);
]]
local wininet = ffi.load("wininet")
-- wininet.HttpSendRequestA(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength)
-- hRequest : void*
-- lpszHeaders : LPCSTR optional
-- dwHeadersLength : DWORD
-- lpOptional : void* optional
-- dwOptionalLength : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('WININET.dll');
const HttpSendRequestA = lib.func('__stdcall', 'HttpSendRequestA', 'int32_t', ['void *', 'str', 'uint32_t', 'void *', 'uint32_t']);
// HttpSendRequestA(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength)
// hRequest : void* -> 'void *'
// lpszHeaders : LPCSTR optional -> 'str'
// dwHeadersLength : DWORD -> 'uint32_t'
// lpOptional : void* optional -> 'void *'
// dwOptionalLength : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("WININET.dll", {
  HttpSendRequestA: { parameters: ["pointer", "buffer", "u32", "pointer", "u32"], result: "i32" },
});
// lib.symbols.HttpSendRequestA(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength)
// hRequest : void* -> "pointer"
// lpszHeaders : LPCSTR optional -> "buffer"
// dwHeadersLength : DWORD -> "u32"
// lpOptional : void* optional -> "pointer"
// dwOptionalLength : DWORD -> "u32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t HttpSendRequestA(
    void* hRequest,
    const char* lpszHeaders,
    uint32_t dwHeadersLength,
    void* lpOptional,
    uint32_t dwOptionalLength);
C, "WININET.dll");
// $ffi->HttpSendRequestA(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength);
// hRequest : void*
// lpszHeaders : LPCSTR 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.ASCII_OPTIONS);
    boolean HttpSendRequestA(
        Pointer hRequest,   // void*
        String lpszHeaders,   // LPCSTR optional
        int dwHeadersLength,   // DWORD
        Pointer lpOptional,   // void* optional
        int dwOptionalLength   // DWORD
    );
}
@[Link("wininet")]
lib LibWININET
  fun HttpSendRequestA = HttpSendRequestA(
    hRequest : Void*,   # void*
    lpszHeaders : UInt8*,   # LPCSTR 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 HttpSendRequestANative = Int32 Function(Pointer<Void>, Pointer<Utf8>, Uint32, Pointer<Void>, Uint32);
typedef HttpSendRequestADart = int Function(Pointer<Void>, Pointer<Utf8>, int, Pointer<Void>, int);
final HttpSendRequestA = DynamicLibrary.open('WININET.dll')
    .lookupFunction<HttpSendRequestANative, HttpSendRequestADart>('HttpSendRequestA');
// hRequest : void* -> Pointer<Void>
// lpszHeaders : LPCSTR optional -> Pointer<Utf8>
// dwHeadersLength : DWORD -> Uint32
// lpOptional : void* optional -> Pointer<Void>
// dwOptionalLength : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function HttpSendRequestA(
  hRequest: Pointer;   // void*
  lpszHeaders: PAnsiChar;   // LPCSTR optional
  dwHeadersLength: DWORD;   // DWORD
  lpOptional: Pointer;   // void* optional
  dwOptionalLength: DWORD   // DWORD
): BOOL; stdcall;
  external 'WININET.dll' name 'HttpSendRequestA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let httpsendrequesta =
  foreign "HttpSendRequestA"
    ((ptr void) @-> string @-> uint32_t @-> (ptr void) @-> uint32_t @-> returning int32_t)
(* hRequest : void* -> (ptr void) *)
(* lpszHeaders : LPCSTR optional -> string *)
(* 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 ("HttpSendRequestA" http-send-request-a :convention :stdcall) :int32
  (h-request :pointer)   ; void*
  (lpsz-headers :string)   ; LPCSTR 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 $HttpSendRequestA = Win32::API::More->new('WININET',
    'BOOL HttpSendRequestA(LPVOID hRequest, LPCSTR lpszHeaders, DWORD dwHeadersLength, LPVOID lpOptional, DWORD dwOptionalLength)');
# my $ret = $HttpSendRequestA->Call($hRequest, $lpszHeaders, $dwHeadersLength, $lpOptional, $dwOptionalLength);
# hRequest : void* -> LPVOID
# lpszHeaders : LPCSTR optional -> LPCSTR
# dwHeadersLength : DWORD -> DWORD
# lpOptional : void* optional -> LPVOID
# dwOptionalLength : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い
類似 API