您的代碼不起作用,因爲服務器沒有發送有效的HTTP響應。請注意,發回HTTP請求,正在等待迴應... 200無頭,假定HTTP/0.9
$ wget -O- "http://70.36.96.24:13384/;stream.mp3"
--2014-01-20 17:09:12-- http://70.36.96.24:13384/;stream.mp3
Connecting to 70.36.96.24:13384... connected.
HTTP request sent, awaiting response... 200 No headers, assuming HTTP/0.9
Length: unspecified
Saving to: `STDOUT'
[<=> ] 0 --.-K/s ICY 200 OK
icy-notice1:<BR>This stream requires <a href="http://www.winamp.com/">Winamp</a><BR>
icy-notice2:SHOUTcast Distributed Network Audio Server/Linux v1.9.8<BR>
icy-name:Radio Brasileirissima
icy-genre:Musica Brasileira
icy-url:http://radiobrasileirissima.com.br
content-type:audio/mpeg
icy-pub:0
icy-br:64
....BINARY CONTENT SKIPPED....
您仍然可以通過跳過第幾行,直到字符序列\r\n\r\n
,其中icy-br:64
端讀取數據,並開始實際的音頻內容。爲此,您可以創建一個Socket,連接到它,併發送一個GET請求到`/;stream.mp3' 和讀取數據:
val timeout = 2000
val host = "70.36.96.24"
val port = 13384
val socket = new Socket
val endPoint = new InetSocketAddress(host, port)
socket.connect(endPoint, timeout);
// Here you send the GET request and read back the response.
// Every time you read the response, you have to skip
// the first few bytes until `\r\n\r\n`
EDIT1
注:您的GET請求必須包括額外的換行符爲它是一個HTTP請求
GET /;stream.mp3 HTTP/1.1
這裏是在同一個URL工作示例:
def socketurl = {
val timeout = 2000
val host = "70.36.96.24"
val port = 13384
val socket = new Socket
val endPoint = new InetSocketAddress(host, port)
socket.connect(endPoint, timeout);
val (in, out) = (socket.getInputStream(), socket.getOutputStream())
val httpRequest = "GET /;stream.mp3 HTTP/1.1\r\n\r\n"
val command = httpRequest.getBytes()
println(httpRequest)
out.write(command)
var buf = Vector(0, 0, 0, 0)
var c = in.read()
var cont = true
while (c != -1 && cont) {
buf = buf.drop(1) :+ c
print(c.toChar)
if (buf(0) == '\r' && buf(1) == '\n' && buf(2) == '\r' && buf(3) == '\n') {
cont = false
println("Its a match")
}
c = in.read()
}
while (c != -1) {
// keep buffering the data from here on!
}
}
// call the method
socketurl
我認爲鏈接已損壞。你可以用瀏覽器試試 – jilen
你在Linux和Mac上安裝了哪個版本的Java? – tuxdna
你是對的:用Java 6它的工作原理與Java 7它給出的錯誤:-) –