HTTP

# 说明 网络模块函数主要是跟网络请求信息相关联 网络模块的对象前缀是http,例如 http.downloadFile()这样调用 ## http.httpGet() Http GET 请求 @param url 请求的URL @param params 参数Map表 例如 {"a":"1"} 这样的参数或者字符串 @param timeout 超时时间 单位毫秒 @param headers – 头标志例如{“a”:“11”} @return 字符串 请求后返回的字符串 ```js function main(){ var url = "http://192.168.0.5:8081/api/httpGet?a=1"; var pa = {"b": "22"}; var x = http.httpGet(url, pa, 10 * 1000, {"User-Agent": "test"}); toast(" result-> " + x); loge("result -> " + x); } main(); ``` #### 获取苏宁时间 ```js var url = "http://quan.suning.com/getSysTime.do"; var result = http.httpGet(url, '', 10 * 1000); logd(result); logd(JSON.parse(result)); logd(JSON.parse(result).sysTime2); ``` ## http.httpGetDefault() Http GET 请求 @param url 请求的URL @param timeout 超时时间 单位毫秒 @param headers – 头标志例如{“a”:“11”} @return 字符串 请求后返回的字符串 ```js function main(){ var url = "http://192.168.0.5:8081/api/httpGet?a=1"; var x = http.httpGetDefault(url, 10 * 1000, {"User-Agent": "test"}); toast(" result-> " + x); loge("result -> " + x); } main(); ``` ## http.httpPost() Http Post 请求 @param url 请求的URL @param params 参数,例如 {"a":"1"} 这样的参数或者字符串 @param files 要上传的文件,例如 {"file1":"/sdcard/1.png"}这样的文件参数 @param timeout 超时时间 单位毫秒 @param headers – 头标志例如{“a”:“11”} @return 字符串 请求后返回的字符串 ```js function main(){ //不带文件的请求 var url = "http://192.168.0.5:8081/api/httpPost"; var pa = {"b": "我是b的值"}; var x = http.httpPost(url, pa, null, 10 * 1000, {"User-Agent": "test"}); toast(" result-> " + x); loge("result -> " + x); } main(); ``` ```js function main(){ //带上传文件的请求 var url = "http://192.168.0.5:8081/api/httpPost"; var pa = {"b": "我是b的值"}; var files = {"file1": "/sdcard/p.json", "file2": "/sdcard/z.xml"}; var x = http.httpPost(url, pa, files, 10 * 1000, {"User-Agent": "test"}); toast(" result-> " + x); loge("result -> " + x); } main(); ``` ## http.postJSON() HTTP POST JSON数据 @param url 请求的URL @param json json数据 @param timeout – 超时时间 单位毫秒 @param headers – 头标志例如{“a”:“11”} @return 字符串 请求后返回的字符串 ```js function main(){ var url = "http://192.168.0.5:8081/api/postJSON"; var pa = {"b": "我是b的值"}; var x = http.postJSON(url, pa, 10 * 1000, {"User-Agent": "test"}); toast(" result-> " + x); loge("result -> " + x); } main(); ``` ## 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:要上传的文件,集合参数,例如 [{"key":"a1","fileName":"a.txt","filePath":"/sdcard/"},{"key":"a1","fileName":"a.jpg","filePath":"/sdcard/","contentType":"image/jpg"}] 其中contentType可有可无 - responseCharset: 字符串,强制设置响应内容的编码集 @return Response 对象或者null ```js 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("/sdcard/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 = [ { "key": "file", "fileName": "f.png", "filePath": "/sdcard/sb.png" }, { "key": "file", "fileName": "f2.png", "filePath": "/sdcard/sde.png", "contentType": "image/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:要上传的文件,集合参数,例如 [{"key":"a1","fileName":"a.txt","filePath":"/sdcard/"},{"key":"a1","fileName":"a.jpg","filePath":"/sdcard/","contentType":"image/jpg"}] 其中contentType可有可无 - responseCharset: 字符串,强制设置响应内容的编码集 @return Response 对象或者null ```js 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("/sdcard/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 = [ { "key": "file", "fileName": "f.png", "filePath": "/sdcard/sb.png" }, { "key": "file", "fileName": "f2.png", "filePath": "/sdcard/sde.png", "contentType": "image/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”:“11”} @return true 代表成功 false代表失败 ```js 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, "/sdcard/ss.apk", 10 * 1000, {"User-Agent": "test"}); toast("download result-> " + x); } main(); ``` ## http.downloadFileDefault() 下载远程文件到本地,支持断点续传,默认超时时间为30秒 @param remoteUrl 远程文件URL @param file 要保存到本地的文件对象 @param headers – 头标志例如{“a”:“11”} @return true 代表成功 false代表失败 ```js 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.downloadFileDefault(url, "/sdcard/ss.apk", {"User-Agent": "test"}); toast("download result-> " + x); } main(); ``` ## http.newWebsocket() 创建一个websocket @param url 要连接的地址 @param header 参数头 @return {@link WebSocket } WebSocket对象 ```js // WebSocket 在线测试 // http://www.websocket-test.com/ //新建一个ws连接 var ws = http.newWebsocket("ws://121.40.165.18:8800", null); //设置连接打开的时候监听器 ws.onOpen(function (ws, code, msg) { logi("onOpen code " + code + " msg:" + msg); }) //设置有文本信息监听器 ws.onText(function (ws, text) { logi(" onText " + text); }) //设置关闭时候的监听器 ws.onClose(function (ws, code, reason) { logi(" onClose " + code + " reason : " + reason + " remote:"); }) ws.onError(function (ws, msg) { logi(" onError " + msg); }) // bytes 是 java的okio.ByteString 对象 ws.onBinary(function (ws, bytes) { logi(" onBinary " + bytes.hex()); logi(" onBinary " + bytes.utf8()); logi(" onBinary " + bytes.md5()); }) //开始连接 ws.connect(); sleep(1000); if (ws.isConnected()) { //发送文本 ws.sendText("hello,飞云编程学院"); } logd("isClosed " + ws.isClosed()) sleep(1000); //关闭连接 ws.close(); logd("isClosed " + ws.isClosed()) ``` # WebSocket对象 ## connect() 开始异步连接 ## isClosed() 是否已经关闭 @return true 代表已经关闭,false 未关闭 ## isConnected() 是否已经连接 @return true 代表已经连接,false 未关闭 ## close() 关闭链接 ## sendText() 发送文本消息 @param text 文本信息 ## sendBinary() 发送字节信息 @param bin okio.ByteString 对象 ## onOpen() 当连接打开的时候事件回调 @param callback 回调函数 ## onText() 当有文本信息发送过来的时候回调 @param callback 回调函数 ## onClose() 当关闭的时候回调 @param callback 回调函数 ## onError() 当发生错误的时候回调 @param callback 回调函数 ## onBinary() 当有二进制数据过来的时候回调 @param callback 回调函数