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×tamp=" + 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×tamp= "+ 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