HttpClient

通过 DolphinDB 的 httpClient 插件,用户可以发送 HTTP 请求以及发送邮件。该插件使用第三方库 CURL 来进行 HTTP 相关的操作。

安装插件

版本要求

DolphinDB Server:2.00.10 及更高版本。支持 Linux x64,Linux ARM ,Windows x64。

安装步骤

  1. 在 DolphinDB 客户端中使用 listRemotePlugins 命令查看插件仓库中的插件信息。

    注意:仅展示当前操作系统和 server 版本支持的插件。若无预期插件,可自行编译(请自行选择对应分支下的插件)或在 DolphinDB 用户社区进行反馈。

    login("admin", "123456")
    listRemotePlugins()
  2. 使用 installPlugin 命令完成插件安装。

    installPlugin("httpClient")
  3. 使用 loadPlugin 命令加载插件。

    loadPlugin("httpClient")

接口说明

httpGet

发送HTTP GET请求。

语法

httpClient::httpGet(url, [params], [timeout], [headers], [config])

详情

发送 HTTP GET 请求。返回一个字典,包括以下键:

  • responseCode:请求返回的响应码。

  • headers:请求返回的头部。

  • text:请求返回的内容文本。

  • elapsed:请求经过的时间。

参数 url 请求的URL字符串。

params STRING 类型标量,或键和值都是 STRING 类型的字典,表示 http 请求的参数。指定后,params 将会放在 url 的后面。

假如url为 www.dolphindb.cn,

  • 如果 params 为一个字符串(例如,"example"),则发出的完整 http 报文的请求头中的 url 为 "http://www.dolphindb.cn?example "。
  • 如果 params 为一个字典(例如,两个键值对 "name"->"jack" 和"id"->"111"),则发出的完整http报文的请求头中的url为 "http://www.dolphindb.cn?id=111&name=jack "。

timeout 整型标量,表示超时时间,单位为毫秒。 headers STRING 类型标量,或键和值都是 STRING 类型的字典,表示 http 请求的头部。

  • 如果 headers 为一个字典(例如 "groupName"->"dolphindb" 和 "groupId"->"11"),则发出的完整 http 报文添加请求头是 "groupId:11" 和 "groupName:dolphindb"。
  • 如果 headers 为一个字符串,则必须是 "xx:xx" 格式,会直接把该字符串添加到请求头中。

config 一个 key 类型为 STRING 的字典,表示一些配置项。

配置项(key)值类型说明
proxySTRING可选参数,设置代理地址

例子

loadPlugin('/home/zmx/worker/DolphinDBPlugin/httpClient/PluginHttpClient.txt');
param=dict(string,string);
header=dict(string,string);
param['name']='zmx';
param['id']='111';
header['groupName']='dolphindb';
header['groupId']='11';
//Please set up your own httpServer ex.(python -m SimpleHTTPServer 8900)
url = "localhost:8900";
res = httpClient::httpGet(url,param,1000,header);

httpPost

发送 HTTP POST 请求。返回值同 httpGet 的返回值。

语法

httpClient::httpPost(url, [params], [timeout], [headers], [config])

参数

httpGet 的参数。

例子

loadPlugin('/home/zmx/worker/DolphinDBPlugin/httpClient/PluginHttpClient.txt');
param=dict(string,string);
header=dict(string,string);
param['name']='zmx';
param['id']='111';
header['groupName']='dolphindb';
header['groupId']='11';
//Please set up your own httpServer ex.(python -m SimpleHTTPServer 8900)
url = "localhost:8900";
res = httpClient::httpPost(url,param,1000,header);

emailSmtpConfig

语法

httpClient::emailSmtpConfig(emailName,host,post)

详情 配置邮件服务器。

参数

emailName STRING 类型标量,表示邮箱名称,格式为邮箱'@'字符后的字符串。如果是 qq 邮箱,则是 "qq.com "。如果是 yeah 邮箱,则是 "yeah.net "。

host STRING 类型标量,表示邮箱 SMTP 服务器的地址。

port INT 类型标量,表示邮箱服务器端口,默认为25。

例子

emailName="qq.com";
host="smtp.qq.com";
port=25;
httpClient::emailSmtpConfig(emailName,host,port);

sendEmail

语法

httpClient::sendEmail(userId,pwd,recipient,subject,body)

详情

发送邮件。返回一个字典,包括以下键:

  • userId:发送者邮箱账号。
  • recipient:接受者邮箱的集合的字符串。
  • responseCode:请求返回的响应码。
  • headers:请求返回的头部。
  • text:请求返回的内容文本。
  • elapsed:请求经过的时间。

注意:本插件使用 smtp 邮件传输协议,所以邮件服务器必须支持 smtp 协议和开启 smtp 端口,如果邮件服务商没有默认开启 smtp 端口,则需要开启该账号邮箱的 smtp 服务。还需要注意该邮件服务商是否提供邮箱授权码的功能,如果有,此时的参数 pwd 为邮箱授权码而非邮箱密码。

参数

userId STRING 类型标量,表示发送者邮箱账号。

pwd STRING 类型标量,表示发送者邮箱密码。

recipient STRING 类型标量,表示目标邮箱账号的一个字符串或一个字符串集合。

subject STRING 类型标量,表示邮件主题。

body STRING 类型标量,表示邮件正文。

例子

res=httpClient::sendEmail('MailFrom@xxx.com','xxxxx','Maildestination@xxx.com','This is a subject','It is a text');
recipient='Maildestination@xxx.com''Maildestination2@xxx.com''Maildestination3@xxx.com';
res=httpClient::sendEmail('MailFrom@xxx.com','xxxxx',recipient,'This is a subject','It is a text');