How to fetch HTML in Java

后端 未结 5 1157
不思量自难忘°
不思量自难忘° 2020-12-05 13:35

Without the use of any external library, what is the simplest way to fetch a website\'s HTML content into a String?

5条回答
  •  眼角桃花
    2020-12-05 14:17

    This has worked well for me:

    URL url = new URL(theURL);
    InputStream is = url.openStream();
    int ptr = 0;
    StringBuffer buffer = new StringBuffer();
    while ((ptr = is.read()) != -1) {
        buffer.append((char)ptr);
    }
    

    Not sure at to whether the other solution(s) provided are any more efficient or not.

提交回复
热议问题