jjd*_*v80 7 java android http httpurlconnection
在我的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();
}
Run Code Online (Sandbox Code Playgroud)
我无法重现这个问题,但必须有办法解决这个问题吗?我甚至通过将setInstanceFollowRedirects设置为'false'来禁用重定向,但它没有帮助.
我错过了什么吗?
用户报告的示例: