【脚本语言JavaScript】Ringo JS-模块 ringo / httpclient

Ringo 是一个 JavaScript 平台

ECMA JavaScript 规范将该语言描述为面向对象的编程语言,用于在主机环境中执行计算和处理计算对象。每个用 JavaScript 编写的应用程序都需要一个主机环境,它提供特定于环境的对象和 API 来执行 I / O。 Ringo 为 JavaScript 提供了这样一个环境,并附带一组模块以使应用程序开发更容易。由于其作为通用编程语言的特性,JavaScript 可以用来解决各种各样的问题,而 Ringo 可以帮助您这么做。利用 Ringo,编写命令行工具,复杂的 Web 应用程序甚至基于 Java UI 技术的 GUI 应用程序都很容易。

脚本语言如 JavaScript 需要一个引擎来解释和执行程序。 Ringo 没有自己的引擎。相反,它使用 Mozilla Rhino,一种 Java 中的 JavaScript 实现。犀牛的最初发展始于 Netscape 时代,并一直持续到现在。基本思想是将 JavaScript 程序编译为 Java 字节码,Java 字节码可以由 Java 虚拟机(JVM)执行。犀牛还提供了对 Java 标准类库和其他每个 Java 类的轻松访问。这使得将现有的 Java 库集成到新的 JavaScript 应用程序变得很容易。例如:Ringo 不是编写自己的 I / O 系统,而是使用现有的 Java I / O 类,并将它们封装起来以提供从 JavaScript 更容易的访问。

Ringo 在服务器或专用机器上执行 JavaScript,而不是在 Web 浏览器上下文中执行。如果您已经从基于 HTML 的应用程序中了解 JavaScript,则这是主要区别。没有什么像一个窗口对象,你没有一个 DOM 来操纵 HTML 对象。尽管如此,很多事情会像你从浏览器中知道的那样。您可以使用 console.log() 调试到控制台,但也有专用的日志记录模块可用于更复杂的日志记录。

Ringo 最大的优势之一就是模块系统。 Ringo 并没有自己构建代码,而是拥有一个易于使用的模块系统。它基于 CommonJS 模块,这是用于保持代码可互换的服务器端 JavaScript 环境的规范。如果您了解 Node.js 的模块,您还知道如何在 Ringo 中编写模块。一个模块封装了 JavaScript 方法和变量,并将它们与其他模块隔离。

模块 ringo / httpclient

用于发送 HTTP 请求和接收 HTTP 响应的模块。

Example

var {request} = require('ringo/httpclient');
var exchange = request({
   method: 'GET',
   url: 'http://ringojs.org/',
   headers: {
      'x-custom-header': 'foobar'
   }
});

if(exchange.status == 200) {
   console.log(exchange.content);
}

Functions

Class BinaryPart

Class Exchange

Instance Properties

Class TextPart


BinaryPart (data, fileName, contentType)

Example

request({
  url: "http://example.org/post-multipart",
  method: "POST",
  contentType: "multipart/form-data",
  data: {
    "image": new BinaryPart(binaryStream, "image.png"),
    "document": new BinaryPart(binaryStream, "invoce.doc", "application/msword")
  }
});

Parameters

Stringdata

the data

StringfileName

(optional) file name

StringcontentType

(optional) content type of the file

Returns

BinaryPart

A newly constructed BinaryPart instance


Exchange (url, options)

Parameters

Stringurl

The URL

Objectoptions

The options

Returns

Exchange

A newly constructed Exchange instance


Exchange.prototype. connection

此 Exchange 实例使用的连接。


Exchange.prototype. content

响应体为 String。


Exchange.prototype. contentBytes

响应正文为 ByteArray。


Exchange.prototype. contentLength

响应内容的长度。


Exchange.prototype. contentType

响应内容类型。


Exchange.prototype. cookies

由服务器设置的 Cookie。


Exchange.prototype. encoding

响应编码。


Exchange.prototype. headers

响应标题。


Exchange.prototype. message

响应状态消息。


Exchange.prototype. status

响应状态码。


Exchange.prototype. url

此 Exchange 实例包装的 URL。


TextPart (data, charset, fileName)

Example

request({
  url: "http://example.org/post-multipart",
  method: "POST",
  contentType: "multipart/form-data",
  data: {
    "simple": new TextPart("my string", "utf-8"),
    "text": new TextPart(textStream, "utf-8"),
    "txtFile": new TextPart(txtStream, "utf-8", "test.txt")
  }
});

Parameters

String|TextStreamdata

text data to write

Stringcharset

the charset

StringfileName

(optional) file name

Returns

TextPart

A newly constructed TextPart instance


del (url, data)

执行 DELETE 请求。

Parameters

Stringurl

The URL

Object|Stringdata

The data to append as GET parameters to the URL

Returns

Exchange

The Exchange instance representing the request and response


get (url, data)

执行 GET 请求。

Parameters

Stringurl

The URL

Object|Stringdata

The data to append as GET parameters to the URL

Returns

Exchange

The Exchange instance representing the request and response


post (url, data)

执行 POST 请求。

Parameters

Stringurl

The URL

Object|String|Stream|Binarydata

The data to send to the server

Returns

Exchange

The Exchange instance representing the request and response


put (url, data)

执行 PUT 请求。

Parameters

Stringurl

The URL

Object|String|Stream|Binarydata

The data send to the server

Returns

Exchange

The Exchange instance representing the request and response


request (options)

提出一个通用的请求。

通用请求选项

选项对象可能包含以下属性:

  • url: 请求网址
  • method: 请求方法,如 GET 或 POST
  • data: 请求参数作为字符串或GET,DELETE等类似方法的对象。对于POST或PUT请求,主体必须是字符串,对象,流或二进制文件。对于多部分形式的POST请求,所有参数值必须是 TextPart 或 BinaryPart 的实例。
  • headers: 请求标题
  • username: 用于 HTTP 认证的用户名
  • password: 用于 HTTP 认证的密码
  • proxy: proxy-settings as string (http://proxy-hostname:port) or object {host: "proxy-hostname", port: 3128}
  • contentType: contentType。如果设置为 multipart / form-data,则 PUT 和 POST 请求的数据将被视为多部分表单上传。
  • binary: 如果内容应作为二进制传送,则为 true,否则将解码为字符串
  • followRedirects: 是否应该自动跟踪 HTTP 重定向(响应代码 3xx);默认值:true
  • readTimeout: 以毫秒为单位的读取超时设置。 0 返回意味着该选项被禁用(即,无限超时);默认值:30000 ms(或直到 impl 决定其时间)
  • connectTimeout: 设置一个指定的超时值,以毫秒为单位,用于打开与此 URLConnection 引用的资源的通信链接。零超时被解释为无限超时。默认值:60000 毫秒(或直到 impl 决定其时间)

Parameters

Objectoptions 

Returns

Exchange

exchange object

See

get
post
put
del

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值