Easy

net.Curl. Easy

net.Curl.Easy 用于配置。
net.Curl.Multi 通过 net.Curl.Multi#addHandle 执行网页请求。

获取(GET)网站内容:

// 创建一个 UTF-8 解码器
const decoder = new TextDecoder('utf-8');
// 创建一个 Curl Easy 实例
var curl = new net.Curl.Easy();
// 在 HMI 上,我们没有 CA 根证书链,因此不会验证证书
curl.setOpt(net.Curl.Easy.option.SSL_VERIFYPEER, false);
// 设置 URL
curl.setOpt(net.Curl.Easy.option.URL, "http://www.weintek.com/");
// 这是一个 GET 请求
curl.setOpt(net.Curl.Easy.option.HTTPGET, true);

var responseData = "";
curl.setOpt(net.Curl.Easy.option.WRITEFUNCTION, function (buf) {
    var resp = decoder.decode(buf);
    responseData += resp;
});

// 为了执行请求,我们使用 Multi 来处理
// 首先,创建一个 Multi 实例
var multi = new net.Curl.Multi();

// 然后设置事件回调函数
multi.onMessage((easyHandle, result) => {
    var error = net.Curl.Easy.strError(result);
    var responseCode = easyHandle.getInfo(net.Curl.info.RESPONSE_CODE);
    // responseData 由 WRITEFUNCTION 写入
    console.log(error, responseCode, responseData);
    multi.removeHandle(easyHandle);
});

// 执行请求
multi.addHandle(curl);

Members

(static) code :Number

The result code passed in net.Curl.Multi~onMessageCallback.

Code Description
OK No error
UNSUPPORTED_PROTOCOL Unsupported protocol
FAILED_INIT Failed initialization
URL_MALFORMAT URL using bad/illegal format or missing URL
NOT_BUILT_IN A requested feature, protocol or option was not found built-in in this libcurl due to a build-time decision.
COULDNT_RESOLVE_PROXY Couldn't resolve proxy name
COULDNT_RESOLVE_HOST Couldn't resolve host name
COULDNT_CONNECT Couldn't connect to server
FTP_WEIRD_SERVER_REPLY FTP: weird server reply
REMOTE_ACCESS_DENIED Access denied to remote resource
FTP_ACCEPT_FAILED FTP: The server failed to connect to data port
FTP_ACCEPT_TIMEOUT FTP: Accepting server connect has timed out
FTP_PRET_FAILED FTP: The server did not accept the PRET command.
FTP_WEIRD_PASS_REPLY FTP: unknown PASS reply
FTP_WEIRD_PASV_REPLY FTP: unknown PASV reply
FTP_WEIRD_227_FORMAT FTP: unknown 227 response format
FTP_CANT_GET_HOST FTP: can't figure out the host in the PASV response
HTTP2 Error in the HTTP2 framing layer
FTP_COULDNT_SET_TYPE FTP: couldn't set file type
PARTIAL_FILE Transferred a partial file
FTP_COULDNT_RETR_FILE FTP: couldn't retrieve (RETR failed) the specified file
QUOTE_ERROR Quote command returned error
HTTP_RETURNED_ERROR HTTP response code said error
WRITE_ERROR Failed writing received data to disk/application
UPLOAD_FAILED Upload failed (at start/before it took off)
READ_ERROR Failed to open/read local data from file/application
OUT_OF_MEMORY Out of memory
OPERATION_TIMEDOUT Timeout was reached
FTP_PORT_FAILED FTP: command PORT failed
FTP_COULDNT_USE_REST FTP: command REST failed
RANGE_ERROR Requested range was not delivered by the server
HTTP_POST_ERROR Internal problem setting up the POST
SSL_CONNECT_ERROR SSL connect error
BAD_DOWNLOAD_RESUME Couldn't resume download
FILE_COULDNT_READ_FILE Couldn't read a file:// file
LDAP_CANNOT_BIND LDAP: cannot bind
LDAP_SEARCH_FAILED LDAP: search failed
FUNCTION_NOT_FOUND A required function in the library was not found
ABORTED_BY_CALLBACK Operation was aborted by an application callback
BAD_FUNCTION_ARGUMENT A libcurl function was given a bad argument
INTERFACE_FAILED Failed binding local connection end
TOO_MANY_REDIRECTS Number of redirects hit maximum amount
UNKNOWN_OPTION An unknown option was passed in to libcurl
TELNET_OPTION_SYNTAX Malformed telnet option
PEER_FAILED_VERIFICATION SSL peer certificate or SSH remote key was not OK
GOT_NOTHING Server returned nothing (no headers, no data)
SSL_ENGINE_NOTFOUND SSL crypto engine not found
SSL_ENGINE_SETFAILED Can not set SSL crypto engine as default
SSL_ENGINE_INITFAILED Failed to initialise SSL crypto engine
SEND_ERROR Failed sending data to the peer
RECV_ERROR Failure when receiving data from the peer
SSL_CERTPROBLEM Problem with the local SSL certificate
SSL_CIPHER Couldn't use specified SSL cipher
SSL_CACERT Peer certificate cannot be authenticated with given CA certificates
SSL_CACERT_BADFILE Problem with the SSL CA cert (path? access rights?)
BAD_CONTENT_ENCODING Unrecognized or bad HTTP Content or Transfer-Encoding
LDAP_INVALID_URL Invalid LDAP URL
FILESIZE_EXCEEDED Maximum file size exceeded
USE_SSL_FAILED Requested SSL level failed
SSL_SHUTDOWN_FAILED Failed to shut down the SSL connection
SSL_CRL_BADFILE Failed to load CRL file (path? access rights?, format?)
SSL_ISSUER_ERROR Issuer check against peer certificate failed
SEND_FAIL_REWIND Send failed since rewinding of the data stream failed
LOGIN_DENIED Login denied
TFTP_NOTFOUND TFTP: File Not Found
TFTP_PERM TFTP: Access Violation
REMOTE_DISK_FULL Disk full or allocation exceeded
TFTP_ILLEGAL TFTP: Illegal operation
TFTP_UNKNOWNID TFTP: Unknown transfer ID
REMOTE_FILE_EXISTS Remote file already exists
TFTP_NOSUCHUSER TFTP: No such user
CONV_FAILED Conversion failed
CONV_REQD Caller must register CURLOPT_CONV_ callback options
REMOTE_FILE_NOT_FOUND Remote file not found
SSH Error in the SSH layer
AGAIN Socket not ready for send/recv
RTSP_CSEQ_ERROR RTSP CSeq mismatch or invalid CSeq
RTSP_SESSION_ERROR RTSP session error
FTP_BAD_FILE_LIST Unable to parse FTP file list
CHUNK_FAILED Chunk callback failed
NO_CONNECTION_AVAILABLE The max connection limit is reached
SSL_PINNEDPUBKEYNOTMATCH SSL public key does not match pinned public key
SSL_INVALIDCERTSTATUS SSL server certificate status verification FAILED
Type:
  • Number
Example

net.Curl.Easy.option.VERBOSE

var curl = new net.Curl.Easy();
// enable verbose mode
curl.setOpt(curl.option.VERBOSE, true);

(static) option :Number

/** 可用选项列在下方:

行为选项 (BEHAVIOR OPTIONS)

选项 描述
VERBOSE 显示详细信息
HEADER 在输出主体中包含头部信息
NOPROGRESS 关闭进度条

回调选项 (CALLBACK OPTIONS)

选项 描述
WRITEFUNCTION 处理数据写入的回调函数
READFUNCTION 处理数据读取的回调函数
SEEKFUNCTION 处理数据查找的回调函数
XFERINFOFUNCTION 处理进度条的回调函数
HEADERFUNCTION 处理接收头部的回调函数

错误选项 (ERROR OPTIONS)

选项 描述
FAILONERROR 在 HTTP 4xx 错误时失败

网络选项 (NETWORK OPTIONS)

选项 描述
URL 需要处理的 URL
PROTOCOLS 允许的协议
REDIR_PROTOCOLS 允许重定向的协议
PROXY 代理服务器
PROXYPORT 代理服务器端口
PROXYTYPE 代理类型
NOPROXY 排除使用代理的主机
HTTPPROXYTUNNEL 通过 HTTP 代理进行隧道连接
SOCKS5_GSSAPI_SERVICE Socks5 GSSAPI 服务名称
SOCKS5_GSSAPI_NEC Socks5 GSSAPI NEC 模式
INTERFACE 绑定连接到本地接口
LOCALPORT 绑定连接到本地端口
LOCALPORTRANGE 绑定连接到本地端口范围
DNS_CACHE_TIMEOUT DNS 缓存超时时间
BUFFERSIZE 请求更小的缓冲区大小
PORT 连接的端口号
TCP_NODELAY 禁用 Nagle 算法
ADDRESS_SCOPE 本地地址的 IPv6 作用域
TCP_KEEPALIVE 启用 TCP 保持连接
TCP_KEEPIDLE 发送保持连接前的空闲时间
TCP_KEEPINTVL 发送保持连接探测的间隔时间

账号与密码选项 (NAMES and PASSWORDS OPTIONS) (认证)

选项 描述
USERPWD 用户名和密码
PROXYUSERPWD 代理用户名和密码
USERNAME 用户名
PASSWORD 密码
LOGIN_OPTIONS 登录选项
PROXYUSERNAME 代理用户名
PROXYPASSWORD 代理密码
HTTPAUTH HTTP 服务器认证方法
TLSAUTH_USERNAME TLS 认证用户名
TLSAUTH_PASSWORD TLS 认证密码
PROXYAUTH HTTP 代理认证方法
SASL_IR 启用 SASL 初始响应
XOAUTH2_BEARER OAuth2 令牌

HTTP 选项

选项 描述
AUTOREFERER 自动设置 Referer: 头部
ACCEPT_ENCODING Accept-Encoding 并自动解压数据
TRANSFER_ENCODING 请求的 Transfer-Encoding
FOLLOWLOCATION 跟随 HTTP 重定向
UNRESTRICTED_AUTH 允许对不同主机进行身份验证
MAXREDIRS 允许跟随的最大重定向次数
POSTREDIR 在 POST 请求后如何处理重定向
PUT 发送 HTTP PUT 请求
POST 发送 HTTP POST 请求
POSTFIELDS 以 POST 方式发送数据
POSTFIELDSIZE POST 数据的大小
POSTFIELDSIZE_LARGE POST 数据的大小(大数据)
COPYPOSTFIELDS 发送 POST 数据(并复制)
HTTPPOST 发送多部分表单 HTTP POST 请求
REFERER Referer: 头部
USERAGENT User-Agent: 头部
HTTPHEADER 自定义 HTTP 头部
HEADEROPT 控制自定义头部
PROXYHEADER 发送到代理的自定义 HTTP 头部
HTTP200ALIASES 200 OK 的替代版本
COOKIE 发送的 Cookie
COOKIESESSION 开始新的 Cookie 会话
HTTPGET 执行 HTTP GET 请求
HTTP_VERSION 使用的 HTTP 版本
IGNORE_CONTENT_LENGTH 忽略 Content-Length
HTTP_CONTENT_DECODING 禁用内容解码
HTTP_TRANSFER_DECODING 禁用传输解码
EXPECT_100_TIMEOUT_MS 100-continue 超时

SMTP 选项

选项 描述
MAIL_FROM 发件人的地址
MAIL_RCPT 收件人的地址
MAIL_AUTH 认证地址

协议选项

选项 描述
TRANSFERTEXT 使用文本传输
PROXY_TRANSFER_MODE 在代理 URL 上添加传输模式
CRLF 转换换行符
RANGE 范围请求
RESUME_FROM 断点续传
RESUME_FROM_LARGE 断点续传(大文件)
CUSTOMREQUEST 自定义请求/方法
FILETIME 请求文件的修改日期和时间
DIRLISTONLY 仅列出目录
NOBODY 不获取响应体内容
INFILESIZE 发送的文件大小
INFILESIZE_LARGE 发送的文件大小(大文件)
UPLOAD 上传数据
MAXFILESIZE 获取的最大文件大小
MAXFILESIZE_LARGE 获取的最大文件大小(大文件)
TIMECONDITION 设置时间条件请求
TIMEVALUE 时间条件请求的时间值

连接选项

选项 描述
TIMEOUT 整个请求的超时时间
TIMEOUT_MS 整个请求的毫秒级超时时间
LOW_SPEED_LIMIT 低速限制(低于此速度中止传输)
LOW_SPEED_TIME 低于设定速度持续的时间后触发低速终止
MAX_SEND_SPEED_LARGE 限制上传速度
MAX_RECV_SPEED_LARGE 限制下载速度
MAXCONNECTS 连接池中的最大连接数
FRESH_CONNECT 使用新的连接
FORBID_REUSE 禁止复用连接
CONNECTTIMEOUT 连接阶段的超时时间
CONNECTTIMEOUT_MS 连接阶段的毫秒级超时时间
IPRESOLVE 解析 IP 版本
CONNECT_ONLY 仅建立连接,不执行其他操作
USE_SSL 使用 TLS/SSL
RESOLVE 提供固定/虚假名称解析
DNS_INTERFACE 绑定名称解析到此接口
DNS_LOCAL_IP4 绑定名称解析到此 IPv4 地址
DNS_LOCAL_IP6 绑定名称解析到此 IPv6 地址
DNS_SERVERS ?
ACCEPTTIMEOUT_MS 服务器连接返回接受的超时时间(毫秒)

SSL 和安全选项

选项 描述
SSLCERTTYPE 客户端证书类型
SSLKEYTYPE 客户端密钥类型
KEYPASSWD 客户端密钥密码
SSL_ENABLE_ALPN 启用 ALPN
SSL_ENABLE_NPN 启用 NPN
SSLENGINE 使用 SSL 引擎的标识符
SSLENGINE_DEFAULT 默认 SSL 引擎
SSLVERSION 要使用的 SSL 版本
SSL_VERIFYHOST 验证 SSL 证书中的主机名
SSL_VERIFYPEER 验证 SSL 证书
CERTINFO 提取证书信息
PINNEDPUBLICKEY 设置固定的 SSL 公钥
SSL_CIPHER_LIST 要使用的加密算法
SSL_SESSIONID_CACHE 禁用 SSL 会话 ID 缓存
SSL_OPTIONS 控制 SSL 行为
KRBLEVEL Kerberos 安全级别
GSSAPI_DELEGATION 禁用 GSS-API 委派
Type:
  • Number
Example

net.Curl.Easy.option.VERBOSE

var curl = new net.Curl.Easy();
// 启用详细模式
curl.setOpt(net.Curl.Easy.option.VERBOSE, true);

Methods

(static) strError(code) → {string}

返回给定代码的描述。

Parameters:
Name Type Description
code Number

参见 net.Curl.Easy.code

Returns:
Type
string

getInfo(info)

获取信息。

Parameters:
Name Type Description
info Number

参见 net.Curl.info

reset()

移除之前使用 net.Curl.Easy#setOpt 配置的所有选项。

setOpt(option, parameter) → {Number}

设置请求的选项。

Parameters:
Name Type Description
option Number

参见 net.Curl.Easy.option

parameter any
Returns:
Type
Number