gpt4 book ai didi

java - 使用 HttpURLConnection 下载 html 时的奇怪行为

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:02:38 26 4
gpt4 key购买 nike

在我的 Android 维基百科阅读器应用程序中,我正在使用 HttpURLConnection 下载文章的 html,一些用户报告说他们看不到文章,而是看到了一些 css,所以他们的运营商似乎以某种方式预处理了 html在它被下载之前,而其他维基百科阅读器似乎工作正常。

示例网址:http://en.m.wikipedia.org/wiki/Black_Moon_(album)

我的方法:

public static String downloadString(String url) throws Exception
{
StringBuilder downloadedHtml = new StringBuilder();

HttpURLConnection urlConnection = null;
String line = null;
BufferedReader rd = null;

try
{
URL targetUrl = new URL(url);

urlConnection = (HttpURLConnection) targetUrl.openConnection();

if (url.toLowerCase().contains("/special"))
urlConnection.setInstanceFollowRedirects(true);
else
urlConnection.setInstanceFollowRedirects(false);

//read the result from the server
rd = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

while ((line = rd.readLine()) != null)
downloadedHtml.append(line + '\n');
}
catch (Exception e)
{
AppLog.e("An exception occurred while downloading data.\r\n: " + e);
e.printStackTrace();
}
finally
{
if (urlConnection != null)
{
AppLog.i("Disconnecting the http connection");
urlConnection.disconnect();
}

if (rd != null)
rd.close();
}

return downloadedHtml.toString();
}

我无法重现这个问题,但一定有办法解决这个问题吗?我什至通过将 setInstanceFollowRedirects 设置为“false”来禁用重定向,但这没有帮助。

我错过了什么吗?

用户报告的示例:

http://pastebin.com/1E3Hn2yX

最佳答案

carrier is somehow preprocessing the html before it's downloaded

a way to get around that?

使用 HTTPS 防止运营商重写页面。 (无引用)

Am I missing something?

我看不到

关于java - 使用 HttpURLConnection 下载 html 时的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22830482/

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