HTTP URL 请求成功但 StringBuilder 响应 returns 为空

HTTP URL request is successful but StringBuilder response returns null

[Java] 我的 HTTP 请求使用 URL 的 openStream() 方法来读取和拉取 URL 的内容。我的 servlet 能够成功访问 URL 并且也成功遍历 while 循环内的内容。但是,StringBuilder value/HTML 响应返回空值。 “内容”的所有调试日志都显示为空。

这是我的代码。任何帮助将不胜感激。

enter image description here

 Writer writer = response.getWriter();

        try {
            StringBuilder content = new StringBuilder("ContentEDL ");
            LOG.info("EDLHost: Inside try block");
            String str = "https://www.google.com";
            URL url = new URL(str);
            LOG.error("EDLStr: " + str);
            LOG.error("EDLHost: " + url.getHost());
            //HttpURLConnection con = (HttpURLConnection) url.openConnection();
            //LOG.error("EDLCode: "+ con.getResponseCode());
            //URLConnection con = url.openConnection();
            LOG.error("EDLFile : " + url.getFile());
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null)
                LOG.error("Inside EDLWhileLoop " + inputLine);
                content.append(inputLine);
                LOG.error("ContentAppended: " + content);
                writer.append(content);
                //System.out.println(inputLine);
            LOG.error("EDLContent : " + content);
            in.close();
            //writer.append(content);
        } catch (IOException e) {
            LOG.error("EDLexception: {}", e);
            e.printStackTrace();
        }

我认为 while 循环括号有问题。试试下面的代码。

Writer writer = response.getWriter();

try {
    StringBuilder content = new StringBuilder("ContentEDL ");
    LOG.info("EDLHost: Inside try block");
    String str = "https://www.google.com";
    URL url = new URL(str);
    LOG.error("EDLStr: " + str);
    LOG.error("EDLHost: " + url.getHost());
    //HttpURLConnection con = (HttpURLConnection) url.openConnection();
    //LOG.error("EDLCode: "+ con.getResponseCode());
    //URLConnection con = url.openConnection();
    LOG.error("EDLFile : " + url.getFile());
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
        LOG.error("Inside EDLWhileLoop " + inputLine);
        content.append(inputLine);
    }
        LOG.error("ContentAppended: " + content);
        writer.append(content);
        //System.out.println(inputLine);
    LOG.error("EDLContent : " + content);
    in.close();
    //writer.append(content);
} catch (IOException e) {
    LOG.error("EDLexception: {}", e);
    e.printStackTrace();
}