牙叔教程 简单易懂
为什么做
有人用黄鸟抓了请求之后, 发现是PUT请求, 就不会搞了,
一般用户都只用过http.get, http.post, http.json,
用PUT的很少, 并且aj官方文档也没有PUT这个API,
所以, 我们一键转换, 把hcy的PUT转换成autojs的PUT
怎么做
1. 分析hcy的请求报文格式
PUT /userAdvertisingRecord HTTP/1.1 user-agent: Dart/2.14 (dart:io) accept: application/json accept-encoding: gzip content-length: 63 host: aaa.bbb.com authorization: Bearer eyJhbGci_MJmde-u4u6-g content-type: application/json {"companyId":"88405824","id":"8100","watchTime":16}
分为三个部分
Request-Line
请求方法 请求URI HTTP 协议版本
PUT /userAdvertisingRecord HTTP/1.1
Request Header Fields
键值对的格式
user-agent: Dart/2.14 (dart:io) accept: application/json accept-encoding: gzip content-length: 63 host: aaa.bbb.com authorization: Bearer eyJhbGci_MJmde-u4u6-g content-type: application/json
Message Body
{"companyId":"88405824","id":"8100","watchTime":16}
2. 读取hcy文本内容, 提取Request-Line, Request Header Fields, Message Body
let content = files.read(files.path("./http_req_001.hcy")); let lines = content.split("\r\n"); let requestLine = lines[0]; log(requestLine); let emptyLineIndex; var len = lines.length; for (var i = 0; i < len; i++) { if (lines[i] === "") { emptyLineIndex = i; break; } } let requestHeaderFields = lines.slice(1, emptyLineIndex); log(requestHeaderFields); let messageBody = lines[emptyLineIndex + 1]; log(messageBody);
3. 拼凑url
let host = ""; len = requestHeaderFields.length; for (var i = 0; i < len; i++) { let kv = requestHeaderFields[i].split(": "); if (kv[0] === "host") { host = kv[1]; break; } } let url = host + requestLine.split(" ")[1]; log(url);
4. 整理headers
let headers = {}; len = requestHeaderFields.length; for (var i = 0; i < len; i++) { let kv = requestHeaderFields[i].split(": "); headers[kv[0]]=kv[1]; } log(headers);
5. autojs的http模板替换
模板
let r = http .request(URL, { method: METHOD, body: JSON.stringify(BODY), headers: HEADERS, }) .body.string(); log(r);
变量映射
URL --> url METHOD --> requestLine.split(' ')[0] BODY --> messageBody ? JSON.parse(messageBody) : '' HEADERS --> headers
总结
以上就是黄鸟hcy请求转换为autojs请求的关键代码,
转换方法最重要的是要搞清楚, http报文格式,
然后就是字符串替换.
环境
Autojs版本: 9.0.10
名人名言
思路是最重要的, 其他的百度, bing, stackoverflow, github, 安卓文档, autojs文档, 最后才是群里问问
--- 牙叔教程
声明
部分内容来自网络
本教程仅用于学习, 禁止用于其他用途