java – 使用HttpURLConnection下载html时的奇怪行为
|
在我的 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阻止操作符重写页面. (没有引用)
不是我能看到的 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- junit – 指定端口时Spring Boot Actuator端点的
- java – ConcurrentHashMap.putIfAbsent的性能
- java – 将jsp表导出为excel,word,pdf
- java – SAXParseException XML-20221文本中的ch
- 解决Eclipse add external jars运行出现java.lan
- 将CSV文件导进MySQL表格的Java示例
- java – 为什么FileSystemXmlApplicationContext
- java – 为什么ConcurrentSkipListSet.contains需
- java – 从抽象类派生时如何服从equals()的合约
- java实现MD5加密的方法小结
