首页 > 代码库 > 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&timestamp=" + 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&timestamp=" + 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