Hbuilder MUI use Java.net.URL to send network requests, manipulate cookies

Source: Internet
Author: User

1. Introduce the required network request class:

            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. Ignore Android3.0 to start forcing the program to not access the network's enforcement policy in the main thread

            The following two sentences must be added, or will not be able to request the network, because:            //android3.0 version of the mandatory program can not access the network in the main thread, to put the access network in a separate thread.            //The following two sentences can be ignored for these enforcement policies.            var policy = new StrictMode.ThreadPolicy.Builder (). Permitall (). build ();              Strictmode.setthreadpolicy (Policy);

3. Using URLs for network requests

            var httpconn = new URL ("https://login.10086.cn/captchazh.htm?type=05&timestamp=" + nowtime). OpenConnection ();            Set Common Properties            httpconn.setrequestproperty ("Accept", "*/*");            Httpconn.setrequestproperty ("Content-type", "image/jpeg");            Httpconn.setrequestproperty ("Connection", "keep-alive");            Set            the cookie//httpconn.setrequestproperty ("cookie", cookies);            Httpconn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 8.0; Windows NT 6.1) ");            Establish the actual connection            httpconn.connect ();

4. Get the request headers and Set-cookie and the returned data:

//Response head get var htmlheads = [];            var cookies = ""; Take a cookie for (var i = 1; I< -; 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);            }}}//----------------Get output start----------------// var reader= newBufferedReader (New InputStreamReader (Httpconn.getinputstream (), "UTF-8"));            var lines; Output data var response= ""; while ((Lines= Reader.readline ())!= null){response += lines; } reader.close (); ----------------Get output End----------------//

Complete Example:

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");            The following two sentences must be added, or will not be able to request the network, because://android3.0 version of the mandatory program can not access the network in the main thread, to put the access network in a separate thread.            These enforcement policies can be ignored in the following two sentences.              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 ();            Set Common Properties Httpconn.setrequestproperty ("Accept", "*/*");            Httpconn.setrequestproperty ("Content-type", "image/jpeg");            Httpconn.setrequestproperty ("Connection", "keep-alive");            Set the cookie//httpconn.setrequestproperty ("cookie", cookies); Httpconn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 8.0;            Windows NT 6.1) ");            Establish the actual connection httpconn.connect ();            Response Head get var htmlheads = [];            var cookies = ""; Take a cookie for (var i = 1; I< -; 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);            }}}//----------------Get output start----------------// var reader= newBufferedReader (New InputStreamReader (Httpconn.getinputstream (), "UTF-8"));            var lines; Output data var response= ""; while ((Lines= Reader.readline ())!= null){response += lines; } reader.close (); ----------------Get output End----------------//}

Hbuilder MUI use Java.net.URL to send network requests, manipulate cookies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.