网络函数
说明
- 网络模块函数主要是跟网络请求信息相关联
- 网络模块的对象前缀是http,例如 http.downloadFile()这样调用
http.request 万能请求函数
- HTTP万能请求
- @param param map参数,包含的参数有
- url:字符串 请求的地址
- timeout:整型毫秒,超时时间
- method: POST ,GET,PUT 字符串,代表请求的方法
- proxy: 代理地址,map参数 包含主机和端口 例如
{"host":"11","port":111}
- followRedirects:是否自动跳转 true 或者 false
- requestBody: 请求的body体,如果是JSON,就是JSON字符串
- userAgent:字符串 HTTP 的UA
- ignoreContentType:是否忽略内容类型 true 或者 false
- ignoreHttpErrors:是否忽略错误 true 或者 false
- maxBodySize : 整型,HTTP BODY最大值
- referrer:字符串,请求来源
- header: HTTP 请求头,map参数,例如
{"UA":"test"}
- cookie: HTTP 请求Cookie,map参数, 例如
{"a":1}
- data:HTTP POST的数据,map参数, 例如
{"a":1}
- file:要上传的文件,例如
{"file1":"a1.png","file2":"a2.png"}
- responseCharset: 字符串,强制设置响应内容的编码集
- url:字符串 请求的地址
- @return Response 对象或者null
function main() {
http_request();
}
function http_request() {
//url:string
//timeout:int ms
//method: post ,get
//proxy: {"host":"11","port":111}
//followRedirects:true false
//requestBody: string
//userAgent:string
//ignoreContentType:true false
//ignoreHttpErrors:true false
//maxBodySize : int
//referrer:string
//header:{"UA":"test"}
//cookie:{"a":1}
//data:{"a":1}
//file:{}
//responseCharset: string
var md = utils.dataMd5("12345");
var md2 = utils.fileMd5("D:/sb.png");
var url = "http://192.168.0.5:8081/api/request";
var proxy = {"host": "192.168.0.5", "port": "100"};
var userAgent = "xxx";
var followRedirects = false;
var requestBody = JSON.stringify({"A": 111});
var ignoreContentType = true;
var ignoreHttpErrors = true;
var referrer = "xxx";
var header = {
"Content-Type": " application/json; charset=UTF-8",
"User-Agent": "from test",
"ddd": md,
"dd2": md2,
"imei": device.getIMEI()
};
var cookie = {
"cookie1": "tst1",
"cookie2": "tst2"
};
var data = {
"a1": "aaa",
"pwd2": md,
"md2": md2
};
var file =
{
"file1": "a.png",
"file2": "f.png"
}
var params = {
"url": url,
"method": "POST",
"userAgent": userAgent,
"referrer": "baidu.com",
"cookie": cookie,
"data": data,
"file": file
};
var x = http.request(params);
if (x) {
logd("header=" + x.header);
logd("cookie=" + x.cookie);
logd("statusCode=" + x.statusCode);
logd("statusMessage=" + x.statusMessage);
logd("charset=" + x.charset);
logd("contentType=" + x.contentType);
logd("body=" + x.body);
} else {
loge("无结果");
}
}
main();
http.requestEx 万能请求函数(扩展)
- HTTP万能请求
- @param param map参数,包含的参数有
- url:字符串 请求的地址
- timeout:整型毫秒,超时时间
- method: POST ,GET,PUT 字符串,代表请求的方法
- proxy: 代理地址,map参数 包含主机和端口 例如
{"host":"11","port":111}
- followRedirects:是否自动跳转 true 或者 false
- requestBody: 请求的body体,如果是JSON,就是JSON字符串
- userAgent:字符串 HTTP 的UA
- ignoreContentType:是否忽略内容类型 true 或者 false
- ignoreHttpErrors:是否忽略错误 true 或者 false
- maxBodySize : 整型,HTTP BODY最大值
- referrer:字符串,请求来源
- header: HTTP 请求头,map参数,例如
{"UA":"test"}
- cookie: HTTP 请求Cookie,map参数, 例如
{"a":1}
- data:HTTP POST的数据,map参数, 例如
{"a":1}
- file:要上传的文件,例如
{"file1":"a1.png","file2":"a2.png"}
- responseCharset: 字符串,强制设置响应内容的编码集
- url:字符串 请求的地址
- @return Response 对象或者null
function main() {
http_request();
}
function http_request() {
//url:string
//timeout:int ms
//method: post ,get
//proxy: {"host":"11","port":111}
//followRedirects:true false
//requestBody: string
//userAgent:string
//ignoreContentType:true false
//ignoreHttpErrors:true false
//maxBodySize : int
//referrer:string
//header:{"UA":"test"}
//cookie:{"a":1}
//data:{"a":1}
//file:{}
//responseCharset: string
var md = utils.dataMd5("12345");
var md2 = utils.fileMd5("D:/sb.png");
var url = "http://192.168.0.5:8081/api/request";
var proxy = {"host": "192.168.0.5", "port": "100"};
var userAgent = "xxx";
var followRedirects = false;
var requestBody = JSON.stringify({"A": 111});
var ignoreContentType = true;
var ignoreHttpErrors = true;
var referrer = "xxx";
var header = {
"Content-Type": " application/json; charset=UTF-8",
"User-Agent": "from test",
"ddd": md,
"dd2": md2,
"imei": device.getIMEI()
};
var cookie = {
"cookie1": "tst1",
"cookie2": "tst2"
};
var data = {
"a1": "aaa",
"pwd2": md,
"md2": md2
};
var file = {
"file1": "a.png",
"file2": "b.png"
}
var params = {
"url": url,
"method": "POST",
"userAgent": userAgent,
"referrer": "baidu.com",
"cookie": cookie,
"data": data,
"file": file
};
var x = http.requestEx(params);
if (x) {
logd("header=" + x.header);
//直接取值
logd("header=" + x.header["Location"]);
for (var d in x.header) {
logd("header key " + d + " " + x.header[d]);
}
logd("cookie=" + x.cookie);
for (var d in x.cookie) {
logd("cookie key " + d + " " + x.cookie[d]);
}
logd("cookie=" + x.cookie["aa"]);
logd("statusCode=" + x.statusCode);
logd("statusMessage=" + x.statusMessage);
logd("charset=" + x.charset);
logd("contentType=" + x.contentType);
logd("body=" + x.body);
} else {
loge("无结果");
}
}
main();
http.downloadFile 下载文件
- 下载远程文件到本地,不支持断点续传
- @param remoteUrl 远程文件URL
- @param file 要保存到本地的文件对象
- @param timeout 下载超时,单位是毫秒
- @param headers – 头标志 例如
{"a":1}
- @return true 代表成功 false代表失败
function main() {
var url = "https://imtt.dd.qq.com/16891/apk/DF4FD15AF9A9B51BA74D2710CF738EEF.apk?fsname=com.ishugui_3.9.2.3068_3923068.apk&csr=1bbd";
var x = http.downloadFile(url, "D:/ss.apk", 10 * 1000, {"User-Agent": "test"});
logd("download result- " + x);
}
main();