URL中如果包含的特殊字元,比如#、+、&等,如果不對其進行轉義,則通過Http不能進行下載(Client通過Get方法訪問HttpServer)。
比如URL為 :http://192.168.49.20:8192/mnt/sdcard/MIUI/music/mp3/only love +can_the verse.mp3
如果不對 + 進行轉義,在HttpServer中收到GET命令後,有一個對uri進行decode的操作(當然如果不執行decode操作,應該就可以不用轉義了,這裡是因為有些URL進行的encode的操作,所有必須decode),decode會把 +
轉為空白格,再根據decode後的uri在HttpServer裡面拿資料,由於HttpServer裡面是有加號的,所有會報http404資源不存在的錯誤。解決辦法是在decode之前將 加號用“%2b”代替,decode會把“%2b”轉為 +,再去get,就可以了。
Dongle中整個對URL的處理是:
1、取原始URL:http://192.168.49.20:8192/mnt/sdcard/MIUI/music/mp3/only love +can_the verse.mp3
2、對原始URL特殊字元進行轉義:http://192.168.49.20:8192/mnt/sdcard/MIUI/music/mp3/only love %2bcan_the verse.mp3
3、點擊播放時給Dongle設定的URL為:http://192.168.49.20:8192/mnt/sdcard/MIUI/music/mp3/only love %2bcan_the verse.mp3
4、Dongle向手機HttpServer取資料的URL為:http://192.168.49.20:8192/mnt/sdcard/MIUI/music/mp3/only love +can_the verse.mp3
5、HttpServer對收到的URL轉義:http://192.168.49.20:8192/mnt/sdcard/MIUI/music/mp3/only love %2bcan_the verse.mp3
6、HttpServer對轉義後的URL進行decode,得到:http://192.168.49.20:8192/mnt/sdcard/MIUI/music/mp3/only love +can_the verse.mp3
7、Dongle成功下載並播放。
好像繞了一圈,沒事找事一樣。如果沒有decode這一個過程,完全可以不用這一輪的東西啊。可是圖片的URL為了相容中文名稱,必須進行encode,所有到Dongle向HttpServer GET的時候就需要對URL進行decode了。也可以在decode的時候判斷GET的是不是圖片,如果不是圖片就不decode,這樣應該也可以吧。