URL 连接上的 InputStreamReader 返回 null

InputStreamReader on a URL connection returning null

我正在学习 "Web Scraping with Java" 一书中的网页抓取教程。下面的代码给了我一个 nullPointerExcpetion。部分问题在于 (line = in.readLine()) 始终为空,因此第 33 行的 while 循环永远不会运行。我不知道为什么它总是空的。任何人都可以让我深入了解这个吗?此代码应在 CPython 上打印维基百科文章的第一段。

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.net.*;
import java.io.*;

public class WikiScraper {
    public static void main(String[] args) {
        scrapeTopic("/wiki/CPython");
    }
    public static void scrapeTopic(String url){
        String html = getUrl("http://www.wikipedia.org/"+url);
        Document doc = Jsoup.parse(html);
        String contentText = doc.select("#mw-content-text > p").first().text();
        System.out.println(contentText);
    }
    public static String getUrl(String url){
        URL urlObj = null;
        try{
            urlObj = new URL(url);
        }
        catch(MalformedURLException e){
            System.out.println("The url was malformed!");
            return "";
        }
        URLConnection urlCon = null;
        BufferedReader in = null;
        String outputText = "";
        try{
            urlCon = urlObj.openConnection();
            in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
            String line = "";
            while((line = in.readLine()) != null){
                outputText += line;
            }
            in.close();
        }catch(IOException e){
            System.out.println("There was an error connecting to the URL");
            return "";
        }
        return outputText;
    }
}

如果输入http://www.wikipedia.org//wiki/CPython in web browser, it will be redirected to https://en.wikipedia.org/wiki/CPython,那么

使用String html = getUrl("https://en.wikipedia.org/"+url);

改为String html = getUrl("http://www.wikipedia.org/"+url);

line = in.readLine()真的可以读点东西了