〜 ネットワーク 〜
サーバーのリソースのサイズを取得 (要WININET.DLL)

サーバー上にあるページや画像等のファイル情報をパソコンにダウンロードする際、
バッファサイズが小さすぎると正常に取得できないし、大きすぎると無駄にメモリを確保することになるので、
最適なサイズ分だけ確保する手を打たなくてはなりません。
この場合に使えるのが、HTTPサーバーにリクエストを送信して、
作成日時やファイルサイズ等のファイル情報のみを取得する今回のTipsとなります。
使用するAPI関数はHttpQueryInfoで、設定するのは第1引数から順に「HTTPリクエストハンドル」
「リクエスト値」「受取先変数ポインタ」「変数サイズ」「ヘッダインデクス」となります。
HTTPリクエストハンドルとは、作成したHTTP要求の識別ハンドルのことですが、
同じものなのかAPI関数InternetOpenUrlで取得したハンドルを渡すことでもいけるようです。
リクエスト値とはサーバーに要求するコードのことで以下のいずれかの定数を指定することが出来ます。
HTTP_QUERY_MIME_VERSION0x0000MIMEプロトコルのバージョン
HTTP_QUERY_CONTENT_TYPE0x0001リソースタイプ
HTTP_QUERY_CONTENT_TRANSFER_ENCODING0x0002リソースに適用されるエンコーディング法
HTTP_QUERY_CONTENT_ID0x0003コンテンツの識別子
HTTP_QUERY_CONTENT_DESCRIPTION0x0004(レガシーアプリケーション互換性のために存在)
HTTP_QUERY_CONTENT_LENGTH0x0005リソースサイズ
HTTP_QUERY_CONTENT_LANGUAGE0x0006Receives the language that the content is in.
HTTP_QUERY_ALLOW0x0007サーバがサポートしている動詞
HTTP_QUERY_PUBLIC0x0008サーバに対して有効な動詞
HTTP_QUERY_DATE0x0009メッセージの作成時刻
HTTP_QUERY_EXPIRES0x000Aリソースの有効期限
HTTP_QUERY_LAST_MODIFIED0x000B最終更新時刻
HTTP_QUERY_MESSAGE_ID0x000C(現在サポートされていない)
HTTP_QUERY_URI0x000Dリソースを特定できるURI
HTTP_QUERY_DERIVED_FROM0x000E(現在サポートされていない)
HTTP_QUERY_COST0x000F(現在サポートされていない)
HTTP_QUERY_LINK0x0010(レガシーアプリケーション互換性のために存在)
HTTP_QUERY_PRAGMA0x0011実装依存のデータ
HTTP_QUERY_VERSION0x0012サーバに最後に返された応答コード
HTTP_QUERY_STATUS_CODE0x0013サーバから返された状態コード
HTTP_QUERY_STATUS_TEXT0x0014サーバから返された補足テキスト
HTTP_QUERY_RAW_HEADERS0x0015NULL文字で区切られた全てのヘッダ
HTTP_QUERY_RAW_HEADERS_CRLF0x0016改行(CR/LF)で区切られた全てのヘッダ
HTTP_QUERY_CONNECTION0x0017接続状態
HTTP_QUERY_ACCEPT0x0018Retrieves the acceptable media types for the response.
HTTP_QUERY_ACCEPT_CHARSET0x0019Retrieves the acceptable character sets for the response.
HTTP_QUERY_ACCEPT_ENCODING0x001ARetrieves the acceptable content-coding values for the response.
HTTP_QUERY_ACCEPT_LANGUAGE0x001BRetrieves the acceptable natural languages for the response.
HTTP_QUERY_AUTHORIZATION0x001CRetrieves the authorization credentials used for a request.
HTTP_QUERY_CONTENT_ENCODING0x001DReceives any additional content codings that have been applied to the entire resource.
HTTP_QUERY_FORWARDED0x001E(レガシーアプリケーション互換性のために存在)
HTTP_QUERY_FROM0x001FRetrieves the e-mail address for the human user
who controls the requesting user agent if the From header is given.
HTTP_QUERY_IF_MODIFIED_SINCE0x0020Retrieves the contents of the If-Modified-Since header.
HTTP_QUERY_LOCATION0x0021Retrieves the absolute URI used in a Location response-header.
HTTP_QUERY_ORIG_URI0x0022(レガシーアプリケーション互換性のために存在)
HTTP_QUERY_REFERER0x0023Receives the URI of the resource where the requested URI was obtained.
HTTP_QUERY_RETRY_AFTER0x0024Retrieves the amount of time the service is expected to be unavailable.
HTTP_QUERY_SERVER0x0025Retrieves information about the software used by the origin server to handle the request.
HTTP_QUERY_TITLE0x0026(レガシーアプリケーション互換性のために存在)
HTTP_QUERY_USER_AGENT0x0027Retrieves information about the user agent that made the request.
HTTP_QUERY_WWW_AUTHENTICATE0x0028Retrieves the authentication scheme and realm returned by the server.
HTTP_QUERY_PROXY_AUTHENTICATE0x0029Retrieves the authentication scheme and realm returned by the proxy.
HTTP_QUERY_ACCEPT_RANGES0x002ARetrieves the types of range requests that are accepted for a resource.
HTTP_QUERY_SET_COOKIE0x002BReceives the value of the cookie set for the request.
HTTP_QUERY_COOKIE0x002CRetrieves any cookies associated with the request.
HTTP_QUERY_REQUEST_METHOD0x002Dリクエストで使用された動詞
HTTP_QUERY_REFRESH0x002E(レガシーアプリケーション互換性のために存在)
HTTP_QUERY_CONTENT_DISPOSITION0x002F(レガシーアプリケーション互換性のために存在)
HTTP_QUERY_AGE0x0030Retrieves the Age response-header field,
which contains the sender's estimate of the amount of time since
the response was generated at the origin server.
HTTP_QUERY_CACHE_CONTROL0x0031Retrieves the cache control directives.
HTTP_QUERY_CONTENT_BASE0x0032Retrieves the base URI for resolving relative URLs within the entity.
HTTP_QUERY_CONTENT_LOCATION0x0033Retrieves the resource location for the entity enclosed in the message.
HTTP_QUERY_CONTENT_MD50x0034Retrieves a MD5 digest of the entity-body for the purpose of providing
an end-to-end message integrity check (MIC) for the entity-body.
HTTP_QUERY_CONTENT_RANGE0x0035Retrieves the location in the full entity-body where the partial entity-body
should be inserted and the total size of the full entity-body.
HTTP_QUERY_ETAG0x0036Retrieves the entity tag for the associated entity.
HTTP_QUERY_HOST0x0037Retrieves the Internet host and port number of the resource being requested.
HTTP_QUERY_IF_MATCH0x0038Retrieves the contents of the If-Match request-header field.
HTTP_QUERY_IF_NONE_MATCH0x0039Retrieves the contents of the If-None-Match request-header field.
HTTP_QUERY_IF_RANGE0x003ARetrieves the contents of the If-Range request-header field.
HTTP_QUERY_IF_UNMODIFIED_SINCE0x003BRetrieves the contents of the If-Unmodified-Since request-header field.
HTTP_QUERY_MAX_FORWARDS0x003CRetrieves the number of proxies or gateways
that can forward the request to the next inbound server.
HTTP_QUERY_PROXY_AUTHORIZATION0x003DRetrieves the header that is used to identify the user to a proxy
that requires authentication.
HTTP_QUERY_RANGE0x003ERetrieves the byte range of an entity.
HTTP_QUERY_TRANSFER_ENCODING0x003FRetrieves the type of transformation that has been applied to the message body
so it can be safely transferred between the sender and recipient.
HTTP_QUERY_UPGRADE0x0040Retrieves the additional communication protocols that are supported by the server.
HTTP_QUERY_VARY0x0041Retrieves the header that
indicates that the entity was selected from a number of available representations
of the response using server-driven negotiation.
HTTP_QUERY_VIA0x0042Retrieves the intermediate protocols and
recipients between the user agent and the server on requests, and
between the origin server and the client on responses.
HTTP_QUERY_WARNING0x0043Retrieves additional information about the status of a response that
may not be reflected by the response status code.
HTTP_QUERY_EXPECT0x0044Retrieves the Expect header,
which indicates whether the client application should expect 100 series responses.
HTTP_QUERY_PROXY_CONNECTION0x0045Retrieves the Proxy-Connection header.
HTTP_QUERY_UNLESS_MODIFIED_SINCE0x0046Retrieves the Unless-Modified-Since header.
HTTP_QUERY_ECHO_REQUEST0x0047(現在サポートされていない)
HTTP_QUERY_ECHO_REPLY0x0048(現在サポートされていない)
HTTP_QUERY_ECHO_HEADERS0x0049(現在サポートされていない)
HTTP_QUERY_ECHO_HEADERS_CRLF0x004A(現在サポートされていない)
受取先変数ポインタには取得した値を格納する変数のアドレスを指定し、 変数サイズも数値で直接指定するのではなく、変数に入れて、その変数のアドレスを渡します。 ヘッダインデクスは同一のヘッダが存在する場合にどのヘッダかを識別するためのインデックスを指定します。 上記のリクエストのうち、サイズを取得するのはHTTP_QUERY_CONTENT_LENGTHになりますが、 返される値がコードなので、数値として受け取るために下記の修飾フラグを加算する必要があります。
HTTP_QUERY_FLAG_NUMBER0x2000000032ビット値として返す

inetinit
[パラメータなし]インターネットサービスを開始するだけである為、パラメータは必要ない。

inetend
[パラメータなし]インターネットハンドルを解放するだけの為、パラメータは必要ない。
尚、終了時に自動的に呼び出されるので命令を実行する必要はない。

inetgetsize URL
URL取得するデータのアドレスを指定する。
尚、statには受け取ったデータサイズが返る。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
	ll_libload wininet, "wininet.dll"
	ll_getproc InternetOpen, "InternetOpenA", wininet
	ll_getproc InternetOpenUrl, "InternetOpenUrlA", wininet
	ll_getproc InternetCloseHandle, "InternetCloseHandle", wininet
	ll_getproc HttpQueryInfo "HttpQueryInfoA", wininet

#module
#deffunc inetinit
	mref stt, 64
	prm = 0, 0, 0, 0, 0
	agent = "hspbc"
	ll_getptr agent : ll_ret prm
	ll_callfunc prm, 5, InternetOpen@
	ll_ret hinet : stt = hinet
	return

#deffunc inetend onexit
	if hinet : ll_callfunc hinet, 1, InternetCloseHandle@
	return

#deffunc inetgetsize str
	mref url, 32
	mref stt, 64
	prm = hinet, 0, 0, 0, 0x80000000, 0
	ll_getptr url : ll_ret prm.1
	ll_callfunc prm, 6, InternetOpenUrl@
	ll_ret hfile
	i.1 = 4
	prm = hfile, 0x20000005, 0, 0, 0 // HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER
	ll_getptr i.0 : ll_ret prm.2
	ll_getptr i.1 : ll_ret prm.3
	ll_callfunc prm, 5, HttpQueryInfo@
	ll_callfunc hfile, 1, InternetCloseHandle@
	stt = i
	return
#global

	sdim url, 1024
	url = "http://www.rinku.zaq.ne.jp/ultimate/img/banner.jpg"
	objsize 100, 20
	pos  10, 10 : input url, 420
	pos 440, 10 : button "サイズ取得", *getsize
	inetinit
	stop

*getsize
	getpath name, url, 8
	inetgetsize url
	dialog name + "のサイズは" + stat + "バイト"
	stop

inetinit
[パラメータなし]インターネットサービスを開始するだけである為、パラメータは必要ない。

inetend
[パラメータなし]インターネットハンドルを解放するだけの為、パラメータは必要ない。
尚、終了時に自動的に呼び出されるので命令を実行する必要はない。

サイズ = inetgetsize(URL)
サイズ取得したデータサイズの受取先を指定する。
URL取得するデータのアドレスを指定する。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#uselib "wininet.dll"
#cfunc	global InternetOpen "InternetOpenA" sptr, int, int, int, int
#cfunc	global InternetOpenUrl "InternetOpenUrlA" int, sptr, int, int, int, int
#func	global InternetCloseHandle "InternetCloseHandle" int
#func	global HttpQueryInfo "HttpQueryInfoA" int, int, var, var, int

#module
#deffunc inetinit
	hinet = InternetOpen("hspbc")
	return hinet

#deffunc inetend onexit
	if hinet : InternetCloseHandle hinet
	return

#defcfunc inetgetsize str url, local i
	if hinet = 0 : inetinit
	hfile = InternetOpenUrl(hinet, url, , , 0x80000000, 0)
	i.1 = 4
	HttpQueryInfo hfile, 0x20000005, i, i.1 // HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER
	InternetCloseHandle hfile
	return i
#global

	sdim url, 1024
	url = "http://www.rinku.zaq.ne.jp/ultimate/img/banner.jpg"
	objsize 100, 20
	pos  10, 10 : input url, 420
	pos 440, 10 : button gosub "サイズ取得", *getsize
	inetinit
	stop

*getsize
	dialog getpath(url, 8) + "のサイズは" + inetgetsize(url) + "バイト"
	return