gpt4 book ai didi

Java HttpURLConnection 使用不同的链接返回相同的 HTML

转载 作者:行者123 更新时间:2023-12-02 05:59:01 24 4
gpt4 key购买 nike

我的用例需要读取一堆不同的 URL 的 HTML 代码,并且所有链接都来自同一个网站。我通过函数传递了 2 个不同的 URL,但如果我说 function("hello.html/hello") ,那么我只能得到第一次传递函数时的结果,之后 function("hello.html/goodbye") 两个函数都会返回完全相同的 HTML 代码。这可能是什么?

最佳答案

下面在控制台显示两个不同的html内容:

package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class URLReader {
public static void main(String[] args) throws Exception {
System.out.println(function("https://www.nasdaq.com/fr/symbol/ndaq/real-time"));
System.out.println(function("https://www.nasdaq.com/fr/symbol/ndaq/stock-chart"));
}

public static String function(String pURL) throws IOException {
URL lURL = new URL(pURL);
BufferedReader lIn = new BufferedReader(
new InputStreamReader(lURL.openStream()));

String lInputLine;
StringBuilder lBuilder = new StringBuilder();
while ((lInputLine = lIn.readLine()) != null)
lBuilder.append(lInputLine);
lIn.close();
return lBuilder.toString();
}
}

关于Java HttpURLConnection 使用不同的链接返回相同的 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55990161/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com