现在官方自带的http的命令少了个提交文件的功能,美中不足
无可奈何我就被迫营业封装了一个http的插件,可以支持upload功能
★ [p=26, null, left]目前插件还是内测版本,功能不完善且概率存在一定BUG,后续会进行迭代更新
”
功能特色
- 自定义header消息头
- 智能判定Content-Type类型
- 支持Android&IOS双端使用
- 统一的payload格式传入(table类型)
命令功能SHTTP.Get(url, opts)
- 功能:发送Get请求
- 参数:
- url: 必选,请求的url
- opts: 可选参数,包含以下参数的键值表headers: 自定义http消息头,可以是键值表或者字符串timeout: 请求超时时间,默认为10秒files: 上传文件路径
- 返回:返回请求结果
SHTTP.Post(url, data, opts)
- 功能:发送Get请求
- 参数:
- url: 必选,请求的url
- data: 必选,需要提交的请求数据,用键值表或者key-value存放提交数据
- opts: 可选参数,包含以下参数的键值表headers: 自定义http消息头,可以是键值表或者字符串timeout: 请求超时时间,默认为10秒files: 上传文件路径
- 返回:返回请求结果
使用范例Import
"SHTTP.lua"TracePrint SHTTP.Get(
"http://bbs.anjian.com/")
Import
"SHTTP.lua"Dim tHeaders = {null}
tHeaders[
"user-agent"] =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"TracePrint SHTTP.Get(
"http://bbs.anjian.com/", {
"headers": tHeaders})
// 设定超时时间
TracePrint SHTTP.Get(
"http://bbs.anjian.com/", {
"headers": tHeaders,
"timeout": 5})
Import
"SHTTP.lua"Dim sUrl=
"https://api.toolnb.com/Tools/Qrcode/qrcodeLogo.html"Dim tHeaders = {null}
tHeaders[
"user-agent"] =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"tHeaders[
"content-type"] =
"multipart/form-data"Dim tPayload = {null}
tPayload[
"images"] =
"undefined"tPayload[
"type"] =
"L"tPayload[
"size"] = 4
tPayload[
"data"] =
"https://www.toolnb.com"TracePrint SHTTP.Post(sUrl, tPayload, {
"headers": tHeaders})
Import
"SHTTP.lua"Dim sUrl=
"https://api.toolnb.com/Tools/Qrcode/qrcodeLogo.html"Dim tHeaders = {null}
tHeaders[
"user-agent"] =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"tHeaders[
"content-type"] =
"multipart/form-data"Dim tPayload = {null}
tPayload[
"type"] =
"L"tPayload[
"size"] = 4
tPayload[
"data"] =
"https://www.toolnb.com"TracePrint SHTTP.Post(sUrl, tPayload, {
"headers": tHeaders,
"files": {
"images":
"/storage/emulated/0/Download/flagCache/com.microvirt.launcher2.png"}})
对接百度OCR识别APIImport
"SHTTP.lua"Import
"ShanHai.lua"Import
"Cjson.lua"Dim sUrl =
"https://aip.baidubce.com/oauth/2.0/token"Dim tData = {null}
tData[
"grant_type"] =
"client_credentials"tData[
"client_id"] =
"bdCBL8ZMDp6jO2swNet3MLbo" // 填自己的client_id
tData[
"client_secret"] =
"m0O0qRmYqYp1fv2YhnQ6GMIWryHycOqN" // 填自己的client_secret
Dim sJson, tJson
// 获取api的access_token值
sJson = SHTTP.Post(sUrl, tData)
tJson = Cjson.Decode(sJson)
Dim Access_Token = tJson[
"access_token"]
sUrl =
"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic" &
"?access_token=" & Access_Token
// 需要先将图片转成base64编码
Dim ImgBase64 = ShanHai.ReadFileBase(GetSdcardDir() &
"/Pictures/test.png")
// 对内容进行url编码
ImgBase64 = ShanHai.CharToUrl(ImgBase64)
sJson = SHTTP.Post(sUrl, {
"image":ImgBase64},{
"headers":{
"Content-Type":
"application/x-www-form-urlencoded"}})
tJson = Cjson.Decode(sJson)
For i = 1 To Len(tJson[
"words_result"])
TracePrint tJson[
"words_result"]
["words"]
Next
插件下载(回复可见):
- SHTTP.lua (, 下载次数:41)