java – 使用HttpURLConnection下载html时的奇怪行为
发布时间:2020-12-15 03:07:55 所属栏目:Java 来源:网络整理
导读:在我的 Android维基百科阅读器应用程序中,我正在使用HttpURLConnection下载文章的html,一些用户报告他们无法看到文章,而是看到一些CSS,所以看起来他们的操作符在下载之前以某种方式预处理html,而其他维基百科读者似乎工作正常. 示例网址:http://en.m.wikipe
|
在我的
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.rn: " + 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 解决方法
使用HTTPS阻止操作符重写页面. (没有引用)
不是我能看到的 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读
