zoukankan      html  css  js  c++  java
  • 使用URLConnection获取页面返回的xml数据

        public static void main(String[] args) throws Exception {
    
            String path="http://flash.weather.com.cn/wmaps/xml/hubei.xml";
            URL url = new URL(path);//获得url对象
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();  //创建URLConnection连接
            conn.setReadTimeout(5*1000);
            conn.setRequestMethod("GET");
            BufferedReader bufr=new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));//设置文件输入流的编码,和url的编码一致
            String lines="";
            String result="";
            while ((lines=bufr.readLine())!=null) {
                result+=lines;
            }
            System.out.println(result);        
        }

    特殊的当请求页面被gzip压缩过之后

        public static void main(String[] args) throws Exception {
    
            String path="http://wthrcdn.etouch.cn/WeatherApi?citykey=101010100";
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
            conn.setReadTimeout(5*1000);
            conn.setRequestMethod("GET");
            BufferedReader bufr=new BufferedReader(new InputStreamReader(new GZIPInputStream(conn.getInputStream()),"UTF-8"));//使用GZIPInputStream解压缩
            String lines="";
            String result="";
            while ((lines=bufr.readLine())!=null) {
                result+=lines;
            }
            System.out.println(result);        
        }
  • 相关阅读:
    安装lamp服务器
    Linux ssh登录命令
    一些替代Xshell的软件推荐
    字符串输入的几种方式
    Java数据库操作的一些注意
    模拟堆
    web安全基础第一天
    情报搜集
    kali中的postgres怎么连接
    LeetCode:Validate Binary Search Tree
  • 原文地址:https://www.cnblogs.com/changshuo/p/5561989.html
Copyright © 2011-2022 走看看