背景介紹
最近再做一個RSS閱讀工具給自己用,其中一個環(huán)節(jié)是從服務(wù)器端獲取一個包含了RSS源列表的json文件晒屎,再根據(jù)這個json文件下載喘蟆、解析RSS內(nèi)容缓升。核心代碼如下:
class PresenterImpl(val context: Context, val activity: MainActivity) : IPresenter {
private val URL_API = "https://vimerzhao.github.io/others/rssreader/RSS.json"
override fun getRssResource(): RssSource {
val gson = GsonBuilder().create()
return gson.fromJson(getFromNet(URL_API), RssSource::class.java)
}
private fun getFromNet(url: String): String {
val result = URL(url).readText()
return result
}
......
}
之前一直執(zhí)行地很好,直到前兩天我購買了一個vimerzhao.top
的域名蕴轨,并將原來的域名vimerzhao.github.io
重定向到了vimerzhao.top
港谊。這個工具就無法使用了,但在瀏覽器輸入URL_API
卻能得到數(shù)據(jù):
那為什么URL.readText()
沒有拿到數(shù)據(jù)呢橙弱?
不支持重定向
可以通過下面代碼測試:
import java.net.*;
import java.io.*;
public class TestRedirect {
public static void main(String args[]) {
try {
URL url1 = new URL("https://vimerzhao.github.io/others/rssreader/RSS.json");
URL url2 = new URL("http://vimerzhao.top/others/rssreader/RSS.json");
read(url1);
System.out.println("=--------------------------------=");
read(url2);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void read(URL url) {
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
得到結(jié)果如下:
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
=--------------------------------=
{"theme":"tech","author":"zhaoyu","email":"dutzhaoyu@gmail.com","version":"0.01","contents":[{"category":"綜合版塊","websites":[{"tag":"門戶網(wǎng)站","url":["http://geek.csdn.net/admin/news_service/rss","http://blog.jobbole.com/feed/","http://feed.cnblogs.com/blog/sitehome/rss","https://segmentfault.com/feeds","http://www.codeceo.com/article/category/pick/feed"]},{"tag":"知名社區(qū)","url":["https://stackoverflow.com/feeds","https://www.v2ex.com/index.xml"]},{"tag":"官方博客","url":["https://www.blog.google/rss/","https://blog.jetbrains.com/feed/"]},{"tag":"個人博客-行業(yè)","url":["http://feed.williamlong.info/","https://www.liaoxuefeng.com/feed/articles"]},{"tag":"個人博客-學(xué)術(shù)","url":["http://www.norvig.com/rss-feed.xml"]}]},{"category":"編程語言","websites":[{"tag":"Kotlin","url":["https://kotliner.cn/api/rss/latest"]},{"tag":"Python","url":["https://www.python.org/dev/peps/peps.rss/"]},{"tag":"Java","url":["http://www.codeceo.com/article/category/develop/java/feed"]}]},{"category":"行業(yè)動態(tài)","websites":[{"tag":"Android","url":["http://www.codeceo.com/article/category/develop/android/feed"]}]},{"category":"亂七八遭","websites":[{"tag":"Linux-綜合","url":["https://linux.cn/rss.xml","http://www.linuxidc.com/rssFeed.aspx","http://www.codeceo.com/article/tag/linux/feed"]},{"tag":"Linux-發(fā)行版","url":["https://blog.linuxmint.com/?feed=rss2","https://manjaro.github.io/feed.xml"]}]}]}
HTTP返回碼301歧寺,即發(fā)生了重定向〖辏可在瀏覽器上這個過程太快以至于我們看不到這個301界面的出現(xiàn)斜筐。這里需要說明的是URL.readText()
是Kotlin中一個擴展函數(shù)
,本質(zhì)還是調(diào)用了URL
類的openStream
方法蛀缝,部分源碼如下:
.....
/**
* Reads the entire content of this URL as a String using UTF-8 or the specified [charset].
*
* This method is not recommended on huge files.
*
* @param charset a character set to use.
* @return a string with this URL entire content.
*/
@kotlin.internal.InlineOnly
public inline fun URL.readText(charset: Charset = Charsets.UTF_8): String = readBytes().toString(charset)
/**
* Reads the entire content of the URL as byte array.
*
* This method is not recommended on huge files.
*
* @return a byte array with this URL entire content.
*/
public fun URL.readBytes(): ByteArray = openStream().use { it.readBytes() }
所以上面的測試代碼即說明了URL.readText()
失敗的原因顷链。
不過URL
不支持重定向是否合理?為什么不支持屈梁?還有待探究嗤练。