These days in the study in the Android, parsing the Web page, but the company content, link outside the network needs agents, and need to verify the user name and password, very headache, online check the next, no clue, finally in a foreign blog to see similar, recorded
URL url = new URL (urlstring); string Host=android.net.proxy.getdefaulthost (); int Port=android.net.proxy.getdefaultport (); socketaddress Address=null; try { address=new inetsocketaddress (host, port); } catch (Exception e) { system.out.println (E.getmessage ()); } proxy proxy=new Java.net.Proxy (Java.net.Proxy.Type.HTTP, address); string credentials = "z10127:[email protected]"; byte[] Toencode = null; try { Toencode = credentials.getbytes ("UTF-8"); } catch (Unsupportedencodingexception E1) { e1.printstacktrace (); } HttpURLConnection ucon=null; try { ucon = (httpurlconnection) url.openconnection (proxy); ucon.setconnecttimeout (50000);
Ucon.setrequestproperty ("Proxy-authorization", "Basic" + base64.encodetostring (Toencode, Base64.default));
Ucon.connect (); } catch (Exception e) {e.printstacktrace (); }
Ucon.connect (); int responsecode = Ucon.getresponsecode (); InputStream Instr=null; if (Responsecode = = HTTPURLCONNECTION.HTTP_OK) {instr = Ucon.getinputstream (); } Bufferedinputstream bis = new Bufferedinputstream (instr); Bytearraybuffer BAF = new Bytearraybuffer (1024); int current = 0; while (current = Bis.read ())! =-1) {//BAF: Append (New String (Baf,0,current, "UTF-8")); Baf.append ((byte) current); } instr.close (); Bis.close (); Ucon.disconnect (); Return encodingutils.getstring (Baf.tobytearray (), "gb2312");
Android Settings agent (verify user name and password)