Reading from a URL Connection Java

前端 未结 3 1879
北恋
北恋 2020-12-21 09:15

I\'m trying to read html code from a URL Connection. In one case the html file I\'m trying to read includes 5 line breaks before the actual doc type declaration. In this cas

3条回答
  •  一生所求
    2020-12-21 09:52

    This:

    public class Main {
        public static void main(String[] args) 
            throws MalformedURLException, IOException 
        {
            URL pageUrl = new URL("http://www.google.com");
            URLConnection getConn = pageUrl.openConnection();
            getConn.connect();
            BufferedReader dis = new BufferedReader( 
                                     new InputStreamReader(
                                         getConn.getInputStream()));
            String myString;
            while ((myString = dis.readLine()) != null)
            {
                System.out.println(myString);
            }
        }
    }
    

    Works perfectly. The URL you are supplying, however, returns nothing.

提交回复
热议问题