forked from feifadaima/AutoJs-Docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.json
More file actions
325 lines (325 loc) · 19.2 KB
/
http.json
File metadata and controls
325 lines (325 loc) · 19.2 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
{
"source": "..\\api\\http.md",
"modules": [
{
"textRaw": "HTTP",
"name": "http",
"stability": 2,
"stabilityText": "Stable",
"desc": "<p>http模块提供一些进行http请求的函数。</p>\n",
"methods": [
{
"textRaw": "http.get(url[, options, callback])",
"type": "method",
"name": "get",
"signatures": [
{
"params": [
{
"textRaw": "`url` {string} 请求的URL地址,需要以\"http://\"或\"https://\"开头。如果url没有以\"http://\"开头,则默认为\"http://\"。 ",
"name": "url",
"type": "string",
"desc": "请求的URL地址,需要以\"http://\"或\"https://\"开头。如果url没有以\"http://\"开头,则默认为\"http://\"。"
},
{
"textRaw": "`options` {Object} 请求选项。参见[http.request()][]。 ",
"name": "options",
"type": "Object",
"desc": "请求选项。参见[http.request()][]。",
"optional": true
},
{
"textRaw": "`callback` {Function} 回调函数,可选,其参数是一个[Response][]对象。如果不加回调函数,则该请求将阻塞、同步地执行。 ",
"name": "callback",
"type": "Function",
"desc": "回调函数,可选,其参数是一个[Response][]对象。如果不加回调函数,则该请求将阻塞、同步地执行。",
"optional": true
}
]
},
{
"params": [
{
"name": "url"
},
{
"name": "options",
"optional": true
},
{
"name": "callback",
"optional": true
}
]
}
],
"desc": "<p>对地址url进行一次HTTP GET 请求。如果没有回调函数,则在请求完成或失败时返回此次请求的响应(参见[Response][])。</p>\n<p>最简单GET请求如下:</p>\n<pre><code>console.show();\nvar r = http.get("www.baidu.com");\nlog("code = " + r.statusCode);\nlog("html = " + r.body.string());\n</code></pre><p>采用回调形式的GET请求如下:</p>\n<pre><code>console.show();\nhttp.get("www.baidu.com", {}, function(res, err){\n if(err){\n console.error(err);\n return;\n }\n log("code = " + res.statusCode);\n log("html = " + res.body.string());\n});\n</code></pre><p>如果要增加HTTP头部信息,则在options参数中添加,例如:</p>\n<pre><code>console.show();\nvar r = http.get("www.baidu.com", {\n headers: {\n 'Accept-Language': 'zh-cn,zh;q=0.5',\n 'User-Agent': 'Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11'\n }\n});\nlog("code = " + r.statusCode);\nlog("html = " + r.body.string());\n</code></pre><p>一个请求天气并解析返回的天气JSON结果的例子如下:</p>\n<pre><code>var city = "广州";\nvar res = http.get("http://www.sojson.com/open/api/weather/json.shtml?city=" + city);\nif(res.statusCode != 200){\n toast("请求失败: " + res.statusCode + " " + res.statusMessage);\n}else{\n var weather = res.body.json();\n log(weather);\n toast(util.format("温度: %s 湿度: %s 空气质量: %s", weather.data.wendu,\n weather.data.shidu, weather.quality));\n}\n</code></pre>"
},
{
"textRaw": "http.post(url, data[, options, callback])",
"type": "method",
"name": "post",
"signatures": [
{
"params": [
{
"textRaw": "`url` {string} 请求的URL地址,需要以\"http://\"或\"https://\"开头。如果url没有以\"http://\"开头,则默认为\"http://\"。 ",
"name": "url",
"type": "string",
"desc": "请求的URL地址,需要以\"http://\"或\"https://\"开头。如果url没有以\"http://\"开头,则默认为\"http://\"。"
},
{
"textRaw": "`data` {string} | {Object} POST数据。 ",
"name": "data",
"type": "string",
"desc": "| {Object} POST数据。"
},
{
"textRaw": "`options` {Object} 请求选项。 ",
"name": "options",
"type": "Object",
"desc": "请求选项。",
"optional": true
},
{
"textRaw": "`callback` {Function} 回调,其参数是一个[Response][]对象。如果不加回调参数,则该请求将阻塞、同步地执行。 ",
"name": "callback",
"type": "Function",
"desc": "回调,其参数是一个[Response][]对象。如果不加回调参数,则该请求将阻塞、同步地执行。",
"optional": true
}
]
},
{
"params": [
{
"name": "url"
},
{
"name": "data"
},
{
"name": "options",
"optional": true
},
{
"name": "callback",
"optional": true
}
]
}
],
"desc": "<p>对地址url进行一次HTTP POST 请求。如果没有回调函数,则在请求完成或失败时返回此次请求的响应(参见[Response][])。</p>\n<p>其中POST数据可以是字符串或键值对。具体含义取决于options.contentType的值。默认为"application/x-www-form-urlencoded"(表单提交), 这种方式是JQuery的ajax函数的默认方式。</p>\n<p>一个模拟表单提交登录淘宝的例子如下:</p>\n<pre><code>var url = "https://login.taobao.com/member/login.jhtml";\nvar username = "你的用户名";\nvar password = "你的密码";\nvar res = http.post(url, {\n "TPL_username": username,\n "TPL_password": password\n});\nvar html = res.body.string();\nif(html.contains("页面跳转中")){\n toast("登录成功");\n}else{\n toast("登录失败");\n}\n</code></pre>"
},
{
"textRaw": "http.postJson(url[, data, options, callback])",
"type": "method",
"name": "postJson",
"signatures": [
{
"params": [
{
"textRaw": "`url` {string} 请求的URL地址,需要以\"http://\"或\"https://\"开头。如果url没有以\"http://\"开头,则默认为\"http://\"。 ",
"name": "url",
"type": "string",
"desc": "请求的URL地址,需要以\"http://\"或\"https://\"开头。如果url没有以\"http://\"开头,则默认为\"http://\"。"
},
{
"textRaw": "`data` {Object} POST数据。 ",
"name": "data",
"type": "Object",
"desc": "POST数据。",
"optional": true
},
{
"textRaw": "`options` {Object} 请求选项。 ",
"name": "options",
"type": "Object",
"desc": "请求选项。",
"optional": true
},
{
"textRaw": "`callback` {Function} 回调,其参数是一个[Response][]对象。如果不加回调参数,则该请求将阻塞、同步地执行。 ",
"name": "callback",
"type": "Function",
"desc": "回调,其参数是一个[Response][]对象。如果不加回调参数,则该请求将阻塞、同步地执行。",
"optional": true
}
]
},
{
"params": [
{
"name": "url"
},
{
"name": "data",
"optional": true
},
{
"name": "options",
"optional": true
},
{
"name": "callback",
"optional": true
}
]
}
],
"desc": "<p>以JSON格式向目标Url发起POST请求。如果没有回调函数,则在请求完成或失败时返回此次请求的响应(参见[Response][])。</p>\n<p>JSON格式指的是,将会调用<code>JSON.stringify()</code>把data对象转换为JSON字符串,并在HTTP头部信息中把"Content-Type"属性置为"application/json"。这种方式是AngularJS的ajax函数的默认方式。</p>\n<p>一个调用图灵机器人接口的例子如下:</p>\n<pre><code>var url = "http://www.tuling123.com/openapi/api";\nr = http.postJson(url, {\n key: "65458a5df537443b89b31f1c03202a80",\n info: "你好啊",\n userid: "1",\n});\ntoastLog(r.body.string());\n</code></pre>"
},
{
"textRaw": "http.postMultipart(url, files[, options, callback])",
"type": "method",
"name": "postMultipart",
"signatures": [
{
"params": [
{
"textRaw": "`url` {string} 请求的URL地址,需要以\"http://\"或\"https://\"开头。如果url没有以\"http://\"开头,则默认为\"http://\"。 ",
"name": "url",
"type": "string",
"desc": "请求的URL地址,需要以\"http://\"或\"https://\"开头。如果url没有以\"http://\"开头,则默认为\"http://\"。"
},
{
"textRaw": "`files` {Object} POST数据。 ",
"name": "files",
"type": "Object",
"desc": "POST数据。"
},
{
"textRaw": "`options` {Object} 请求选项。 ",
"name": "options",
"type": "Object",
"desc": "请求选项。",
"optional": true
},
{
"textRaw": "`callback` {Function} 回调,其参数是一个`Response`对象。如果不加回调参数,则该请求将阻塞、同步地执行。 ",
"name": "callback",
"type": "Function",
"desc": "回调,其参数是一个`Response`对象。如果不加回调参数,则该请求将阻塞、同步地执行。",
"optional": true
}
]
},
{
"params": [
{
"name": "url"
},
{
"name": "files"
},
{
"name": "options",
"optional": true
},
{
"name": "callback",
"optional": true
}
]
}
],
"desc": "<p>向目标地址发起类型为multipart/form-data的请求(通常用于文件上传等), 其中files参数是{name1: value1, name2: value2, ...}的键值对,value的格式可以是以下几种情况:</p>\n<ol>\n<li><code>string</code></li>\n<li>文件类型,即open()返回的类型</li>\n<li>[fileName, filePath]</li>\n<li>[fileName, mimeType, filePath]</li>\n</ol>\n<p>其中1属于非文件参数,2、3、4为文件参数。举个例子,最简单的文件上传的请求为:</p>\n<pre><code>var res = http.postMultipart(url, {\n file: open("/sdcard/1.txt")\n});\nlog(res.body.string());\n</code></pre><p>如果使用格式2,则代码为</p>\n<pre><code>var res = http.postMultipart(url, {\n file: ["1.txt", "/sdcard/1.txt"]\n});\nlog(res.body.string());\n</code></pre><p>如果使用格式3,则代码为</p>\n<pre><code>var res = http.postMultipart(url, {\n file: ["1.txt", "text/plain", "/sdcard/1.txt"]\n});\nlog(res.body.string());\n</code></pre><p>如果使用格式2的同时要附带非文件参数"appId=abcdefghijk",则为:</p>\n<pre><code>var res = http.postMultipart(url, {\n appId: "adcdefghijk",\n file: open("/sdcard/1.txt")\n});\nlog(res.body.string());\n</code></pre>"
},
{
"textRaw": "http.request(url[, options, callback])",
"type": "method",
"name": "request",
"signatures": [
{
"params": [
{
"textRaw": "`url` {string} 请求的URL地址,需要以\"http://\"或\"https://\"开头。如果url没有以\"http://\"开头,则默认为\"http://\"。 ",
"name": "url",
"type": "string",
"desc": "请求的URL地址,需要以\"http://\"或\"https://\"开头。如果url没有以\"http://\"开头,则默认为\"http://\"。"
},
{
"textRaw": "`options` {Object} 请求选项。参见[http.buildRequest()][]。 ",
"name": "options",
"type": "Object",
"desc": "请求选项。参见[http.buildRequest()][]。",
"optional": true
},
{
"textRaw": "`callback` {Function} 回调,其参数是一个[Response][]对象。如果不加回调参数,则该请求将阻塞、同步地执行。 ",
"name": "callback",
"type": "Function",
"desc": "回调,其参数是一个[Response][]对象。如果不加回调参数,则该请求将阻塞、同步地执行。",
"optional": true
}
]
},
{
"params": [
{
"name": "url"
},
{
"name": "options",
"optional": true
},
{
"name": "callback",
"optional": true
}
]
}
],
"desc": "<p>对目标地址url发起一次HTTP请求。如果没有回调函数,则在请求完成或失败时返回此次请求的响应(参见[Response][])。</p>\n<p>选项options可以包含以下属性:</p>\n<ul>\n<li><code>headers</code> {Object} 键值对形式的HTTP头部信息。有关HTTP头部信息,参见<a href=\"http://www.runoob.com/http/http-header-fields.html\">菜鸟教程:HTTP响应头信息</a>。</li>\n<li><code>method</code> {string} HTTP请求方法。包括"GET", "POST", "PUT", "DELET", "PATCH"。</li>\n<li><code>contentType</code> {string} HTTP头部信息中的"Content-Type", 表示HTTP请求的内容类型。例如"text/plain", "application/json"。更多信息参见<a href=\"http://www.runoob.com/http/http-content-type.html\">菜鸟教程:HTTP contentType</a>。</li>\n<li><code>body</code> {string} | {Array} | {Function} HTTP请求的内容。可以是一个字符串,也可以是一个字节数组;或者是一个以<a href=\"https://github.com/square/okio/blob/master/okio/src/main/java/okio/BufferedSink.java\">BufferedSink</a>为参数的函数。</li>\n</ul>\n<p>该函数是get, post, postJson等函数的基础函数。因此除非是PUT, DELET等请求,或者需要更高定制的HTTP请求,否则直接使用get, post, postJson等函数会更加方便。</p>\n"
}
],
"type": "module",
"displayName": "HTTP"
},
{
"textRaw": "Response",
"name": "response",
"desc": "<p>HTTP请求的响应。</p>\n",
"properties": [
{
"textRaw": "`statusCode` {number} ",
"type": "number",
"name": "statusCode",
"desc": "<p>当前响应的HTTP状态码。例如200(OK), 404(Not Found)等。</p>\n<p>有关HTTP状态码的信息,参见<a href=\"http://www.runoob.com/http/http-status-codes.html\">菜鸟教程:HTTP状态码</a>。</p>\n"
},
{
"textRaw": "`statusMessage` {string} ",
"type": "string",
"name": "statusMessage",
"desc": "<p>当前响应的HTTP状态信息。例如"OK", "Bad Request", "Forbidden"。</p>\n<p>有关HTTP状态码的信息,参见<a href=\"http://www.runoob.com/http/http-status-codes.html\">菜鸟教程:HTTP状态码</a>。</p>\n<p>例子:</p>\n<pre><code>var res = http.get("www.baidu.com");\nif(res.statusCode >= 200 && res.statusCode < 300){\n toast("页面获取成功!");\n}else if(res.statusCode == 404){\n toast("页面没找到哦...");\n}else{\n toast("错误: " + res.statusCode + " " + res.statusMessage);\n}\n</code></pre>"
},
{
"textRaw": "`headers` {Object} ",
"type": "Object",
"name": "headers",
"desc": "<p>当前响应的HTTP头部信息。该对象的键是响应头名称,值是各自的响应头值。 所有响应头名称都是小写的(吗)。</p>\n<p>有关HTTP头部信息,参见<a href=\"http://www.runoob.com/http/http-header-fields.html\">菜鸟教程:HTTP响应头信息</a>。</p>\n<p>例子:</p>\n<pre><code>console.show();\nvar res = http.get("www.qq.com");\nconsole.log("HTTP Headers:")\nfor(var headerName in res.headers){\n console.log("%s: %s", headerName, res.headers[headerName]);\n}\n</code></pre>"
},
{
"textRaw": "`body` {Object} ",
"type": "Object",
"name": "body",
"desc": "<p>当前响应的内容。他有以下属性和函数:</p>\n<ul>\n<li>bytes() {Array} 以字节数组形式返回响应内容</li>\n<li>string() {string} 以字符串形式返回响应内容</li>\n<li>json() {Object} 把响应内容作为JSON格式的数据并调用JSON.parse,返回解析后的对象</li>\n<li>contentType {string} 当前响应的内容类型</li>\n</ul>\n"
},
{
"textRaw": "`url` {number} 当前响应所对应的请求URL。 ",
"type": "number",
"name": "url",
"desc": "当前响应所对应的请求URL。"
},
{
"textRaw": "`method` {string} 当前响应所对应的HTTP请求的方法。例如\"GET\", \"POST\", \"PUT\"等。 ",
"type": "string",
"name": "method",
"desc": "当前响应所对应的HTTP请求的方法。例如\"GET\", \"POST\", \"PUT\"等。"
}
],
"type": "module",
"displayName": "Response"
}
]
}