星空网 > 软件开发 > 操作系统

Android 设置代理(验证用户名和密码)

 这几天在研究在Android中,解析网页,但是公司内容,链接外网需要代理,并需要验证用户名和密码,十分头疼,网上查了下,没有头绪,最后总算在一个外国博客中看到类似的,记录下

 

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:scdp@2015";    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 设置代理(验证用户名和密码)

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

找钢网:https://www.ikjzd.com/w/1871
2C2P_电商商家支付方式:https://www.ikjzd.com/w/1872
Finnomena_泰国金融科技创企:https://www.ikjzd.com/w/1873
美菜网官网:https://www.ikjzd.com/w/1874
FlowAccount_泰国会计解决服务商:https://www.ikjzd.com/w/1875
Coupang_韩国电商公司:https://www.ikjzd.com/w/1876
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流