zoukankan      html  css  js  c++  java
  • android 取网址的HTML代码

    /**
         * 
         * 
    @param aUrl 网址
         * 
    @param aEncode 编码
         * 
    @return 返回的HTML代码
         * 
    @throws Exception 对外抛出异常
         
    */
        public String getHTML(String aUrl, String aEncode) throws Exception {
            URL url = new URL(aUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            if (conn.getResponseCode() == 200){
                InputStream inputStream = conn.getInputStream();                
                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while( (len = inputStream.read(buffer)) != -1){
                    outStream.write(buffer, 0, len);
                }
                String htmlStr = new String(outStream.toByteArray(), aEncode);
                inputStream.close();
                outStream.close();
                return htmlStr;
            }
            return null;
        }
  • 相关阅读:
    Linux crontab 的常用定时方式
    Windows 查看端口及进程信息
    java.io.IOException: com.esotericsoftware.kryo.KryoException
    Linux 如何让挂载的硬盘永久生效
    六边形架构-微服务基石
    包和工具
    谈一谈对java简单的理解
    HTTP报文 「HTTP
    四层 or 七层 「HTTP
    setTimeout不生效
  • 原文地址:https://www.cnblogs.com/jxgxy/p/2636548.html
Copyright © 2011-2022 走看看