首页 > 代码库 > Hbuilder MUI里面使用java.net.URL发送网络请求,操作cookie
Hbuilder MUI里面使用java.net.URL发送网络请求,操作cookie
1. 引入所需网络请求类:
var URL = plus.android.importClass("java.net.URL");
var URLConnection = plus.android.importClass("java.net.URLConnection");
var BufferedReader = plus.android.importClass("java.io.BufferedReader");
var InputStreamReader = plus.android.importClass("java.io.InputStreamReader");
var StrictMode = plus.android.importClass("android.os.StrictMode");
2. 忽略Android3.0开始强制程序不能在主线程中访问网络的强制策略
//以下两句必加,不然会请求不了网络,原因:
//android3.0版本开始就强制程序不能在主线程中访问网络,要把访问网络放在独立的线程中。
//以下两句可以忽略这些强制策略。
var policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
3. 使用URL进行网络请求
var httpConn = new URL("https://login.10086.cn/captchazh.htm?type=05×tamp=" + nowtime).openConnection(); // 设置通用属性 httpConn.setRequestProperty("Accept", "*/*"); httpConn.setRequestProperty("Content-Type", "image/jpeg"); httpConn.setRequestProperty("Connection", "Keep-Alive"); //设置Cookie //httpConn.setRequestProperty("Cookie", cookies); httpConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); // 建立实际的连接 httpConn.connect();
4. 获取请求头和Set-Cookie以及返回的数据:
// 响应头部获取 var htmlHeads = []; var cookies = ""; //取cookie for (var i = 1; i < 100; i++) { var key_ = httpConn.getHeaderFieldKey(i); if (key_ == null){ console.log("--------------------------------"+cookies); break; }else{ htmlHeads.push(key_ + ‘:‘ +httpConn.getHeaderField(i)); if(key_ == "Set-Cookie") { var cookie = httpConn.getHeaderField(i); cookies += cookie.substring(0, cookie.indexOf(";")+1); } } } //----------------获取输出 start----------------// var reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8")); var lines; //输出数据 var response = ""; while ((lines = reader.readLine()) != null) { response += lines; } reader.close(); //----------------获取输出 end----------------//
完整例子:
function gocook() { var nowtime = (new Date).getTime(); var URL = plus.android.importClass("java.net.URL"); var URLConnection = plus.android.importClass("java.net.URLConnection"); var BufferedReader = plus.android.importClass("java.io.BufferedReader"); var InputStreamReader = plus.android.importClass("java.io.InputStreamReader"); var StrictMode = plus.android.importClass("android.os.StrictMode"); //以下两句必加,不然会请求不了网络,原因: //android3.0版本开始就强制程序不能在主线程中访问网络,要把访问网络放在独立的线程中。 //以下两句可以忽略这些强制策略。 var policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); var httpConn = new URL("https://login.10086.cn/captchazh.htm?type=05×tamp=" + nowtime).openConnection(); // 设置通用属性 httpConn.setRequestProperty("Accept", "*/*"); httpConn.setRequestProperty("Content-Type", "image/jpeg"); httpConn.setRequestProperty("Connection", "Keep-Alive"); //设置Cookie //httpConn.setRequestProperty("Cookie", cookies); httpConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); // 建立实际的连接 httpConn.connect(); // 响应头部获取 var htmlHeads = []; var cookies = ""; //取cookie for (var i = 1; i < 100; i++) { var key_ = httpConn.getHeaderFieldKey(i); if (key_ == null){ console.log("--------------------------------"+cookies); break; }else{ htmlHeads.push(key_ + ‘:‘ +httpConn.getHeaderField(i)); if(key_ == "Set-Cookie") { var cookie = httpConn.getHeaderField(i); cookies += cookie.substring(0, cookie.indexOf(";")+1); } } } //----------------获取输出 start----------------// var reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8")); var lines; //输出数据 var response = ""; while ((lines = reader.readLine()) != null) { response += lines; } reader.close(); //----------------获取输出 end----------------// }
Hbuilder MUI里面使用java.net.URL发送网络请求,操作cookie
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。