36.4.2 WWW Access

Last-modified: 2025-03-09 (日) 11:38:37

36.4.2 WWW アクセス
Octave はインターネット上の Web サイトと通信できます。web 関数は外部 Web ブラウザを起動し、サイトを対話的に表示します。残りの関数urlread( urlwrite、、、webread) webwriteは Octave の内部関数であり、Octave と URL (Uniform Resource Locator) で識別される Web サイトとの間でデータをインポートまたはエクスポートできます。

: status = web ()
: status = web (url)
: status = web (url, option)
: status = web (url, option_1, …, option_N)
: [status, h, url] = web (…)
デフォルトのシステム Web ブラウザーで URLを開きます。

引数を指定しないと、アドレスhttps://www.octave.orgが開かれます

MATLAB互換性のために追加のオプションを渡すこともできますが、無視されます。

‘-browser’ Open url in the default system browser.
‘-new’ No effect on the system browser.
‘-noaddressbox’ No effect on the system browser.
‘-notoolbar’ No effect on the system browser.
The return value status has one of the values:

‘0’ Found and opened system browser successfully.
‘1’ Cannot find the system browser.
‘2’ System browser found, but an error occurred.
The return values handle and url are currently unimplemented but given for compatibility.

See also: weboptions, webread, webwrite, urlread, urlwrite.

: s = urlread (url)
: [s, success] = urlread (url)
: [s, success, message] = urlread (url)
: […] = urlread (url, method, param)
URLで指定されたリモート ファイルをダウンロードし、その内容を文字列sで返します。

例えば:

s = urlread ("http://ftp.octave.org/pub/README");

ダウンロードが成功した場合、変数successは 1 になります。それ以外の場合は 0 になり、その場合、 messageにはエラー メッセージが含まれます。

出力引数が指定されておらず、エラーが発生した場合、そのエラーは Octave のエラー処理メカニズムを通じて通知されます。

この関数は libcurl を使用します。curl ライブラリは、HTTP、FTP、FILE プロトコルなどをサポートしています。ユーザー名とパスワードは URL で指定できます。例:

s = urlread ("http://user:password@example.com/file.txt");

GET および POST リクエストは、 methodとparamで指定できます。パラメータmethodは '得る' または '役職' であり、 paramはパラメータと値のペアのセル配列です。例:

s = urlread ("http://www.google.com/search",
            "get", {"query", "octave"});

See also: urlwrite.

: urlwrite (url, localfile)
: f = urlwrite (url, localfile)
: [f, success] = urlwrite (url, localfile)
: [f, success, message] = urlwrite (url, localfile)
URLで指定されたリモート ファイルをダウンロードし、 localfileとして保存します 。

例えば:

urlwrite ("http://ftp.octave.org/pub/README",

         "README.txt");

ダウンロードしたファイルのフルパスがfに返されます。

ダウンロードが成功した場合、変数successは 1 になります。それ以外の場合は 0 になり、その場合、 messageにはエラー メッセージが含まれます。

出力引数が指定されておらず、エラーが発生した場合、そのエラーは Octave のエラー処理メカニズムを通じて通知されます。

この関数は libcurl を使用します。curl ライブラリは、HTTP、FTP、FILE プロトコルなどをサポートしています。ユーザー名とパスワードは、次のように URL で指定できます。

urlwrite ("http://username:password@example.com/file.txt",

         "ファイル.txt");

GET および POST リクエストは、 methodとparamで指定できます。パラメータmethodは '得る' または '役職' であり、 paramはパラメータと値のペアのセル配列です。例:

urlwrite ("http://www.google.com/search", "search.html",

         "get", {"query", "octave"});

See also: urlwrite.

: response = webread (url)
: response = webread (url, name1, value1, …)
: response = webread (…, options)
RESTful Web サービスからコンテンツを読み取ります。

urlで指定された Web サービスからコンテンツを読み取り、そのコンテンツを応答で返します。

指定されたすべてのキーと値のペア ( name1、value1 、…) は、 のクエリパラメータとして追加されます。メッセージの本文にクエリを配置するには、 を使用しますwebwrite。Web サービスによって、許容されるクエリ パラメータが定義されます。

options はweboptions、他の HTTP リクエスト オプションを追加するために使用できるオブジェクトです。この引数は、どちらの呼び出し形式でも使用できます。help weboptionsサポートされている HTTP オプションの完全なリストについては、を参照してください。
See also: weboptions, webwrite.

: response = webwrite (url, name1, value1, …)
: response = webwrite (url, data)
: response = webwrite (…, options)
RESTful Web サービスにデータを書き込みます。

urlで指定された Web サービスにコンテンツを書き込み、responseで応答を返します。

指定されたすべてのキーと値のペア ( name1、value1、…) は、クエリ パラメータのペアとしてリクエスト メソッドの本文 ( get、 post、putなど) に追加されます。

options はweboptions、他の HTTP リクエスト オプションを追加するために使用できるオブジェクトです。この引数は、どちらの呼び出し形式でも使用できます。help weboptionsサポートされている HTTP オプションの完全なリストについては、を参照してください。
See also: weboptions, webread.

: output = weboptions ()
: output = weboptions (name1, value1, …)
RESTful Web サービスのパラメータを指定します。

weboptions入力がない場合、weboptions Web サービスへのリクエストのパラメータを指定するためのデフォルトのオブジェクトを返します。 オブジェクトは、関数および関数weboptionsへのオプションの入力引数として使用できます 。 webreadwebwrite

複数の名前と値のペアの引数は、 name1、value1、name2、value2などの ように任意の順序で指定できます。

オプション名は、以下の表に指定されているオプション名のいずれ かと正確に一致する必要があります。

利用可能なオプションは次のとおりです。

‘CharacterEncoding’ — Specify the character encoding of the data:
‘auto’ (default), ‘UTF-8’, ‘US-ASCII’ ‘auto’ chooses an encoding based on the content-type of the data.

‘UserAgent’ — Specify the User Agent for the connection.
Default value is ‘GNU Octave/version’, where ‘version’ is the current version of Octave as returned by version.

‘Timeout’ — Specify the timeout value for the connection in seconds.
Default is 10 seconds. ‘Inf’ is not currently supported.

‘Username’ — User identifier for a basic HTTP connection.
Default is NULL. It must be a string.

‘Password’ — User authentication password for HTTP connection.
Default is NULL. It must be a string or character vector. Programming Note: If you display a weboption object with the Password property set, the value is displayed as a string containing '*'. However, the object stores the value of the Password property as plain text.

‘KeyName’ — Specify the name of an additional key to be added to the HTTP request header. It should be coupled with ‘KeyValue’. It must be a string or character vector.
‘KeyValue’ — Specify the value of the key ‘KeyName’.
‘KeyName’ must be present in order to assign to this field.

‘HeaderFields’ — Specify the header fields for the connection.
Names and values of header fields, specified as an m-by-2 array of strings or cell array of character vectors to add to the HTTP request header. HeaderFields{i,1} is the name of a field and HeaderFields{i,2} is its value.

weboptions ("HeaderFields", {"Content-Length" "78";"Content-Type" "application/json"})
Creates a weboptions object that contains two header fields:
Content-Length with value 78 and Content-Type with value application/json.
‘ContentType’ — Specify the content type of the data.
The following values are available: ‘auto’, ‘text’, ‘json’

Default is ‘auto’. It automatically determines the content type. All other formats like ‘audio’, ‘binary’, etc. available in MATLAB are not currently supported.

‘ContentReader’ — Not yet implemented. Only for MATLAB compatibility.
‘MediaType’ — Not yet implemented. Only for MATLAB compatibility.
‘RequestMethod’ — Specifies the type of request to be made.
The following methods are available: ‘get’, ‘put’, ‘post’, ‘delete’, ‘patch’

webread uses the HTTP GET method. webwrite uses the HTTP POST method as default.

‘ArrayFormat’ – Not yet implemented. Only for MATLAB compatibility.
‘CertificateFilename’ — Not yet implemented. Only for MATLAB compatibility.
See also: webread, webwrite.