英文:
Can't get the header of an online ShoutCast stream
问题
我已经连续工作了24小时,试图找出这个在线流的问题:
http://str45.streamakaci.com:8014
您可以尝试它,它是可以工作的,但是当我尝试使用Java从中获取头时,
我得到了这个:
发送 'GET' 请求到 URL:http://str45.streamakaci.com:8014
响应代码:-1
响应消息:{Content-type=[unknown/unknown]}
当我使用SMSSniffer来查看返回的HTTP头发生了什么,我得到了正确的头部:
GET / HTTP/1.1
Content-Type: text/html; charset=UTF-8
Icy-MetaData: 1
Accept-Encoding: gzip
Content-Encoding: gzip, deflate
Cache-Control: no-cache
User-Agent: Java/1.8.0_131
Host: str45.streamakaci.com:8014
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
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 Flemme MP3
icy-genre:Various
icy-url:http://www.radioflemme.com
content-type:audio/mpeg
icy-pub:0
icy-metaint:32768
icy-br:128
在头部下面有一些长文本。
这里是获取HTTP头的Java代码,该代码在其他ShoutCast流上运行正常,但在这个流上不起作用。
private static void sendGet() throws Exception {
String url = "http://str45.streamakaci.com:8014";
HttpURLConnection httpClient =
(HttpURLConnection) new URL(url).openConnection();
// optional default is GET
httpClient.setRequestMethod("GET");
//add request header
httpClient.setRequestProperty("Content-Type", "text/html; charset=UTF-8");
httpClient.setRequestProperty("Icy-MetaData", "1");
httpClient.setRequestProperty("Accept-Encoding", "gzip");
httpClient.setRequestProperty("Content-Encoding", "gzip, deflate");
httpClient.setRequestProperty("Cache-Control", "no-cache");
int responseCode = httpClient.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
String name = httpClient.getHeaderField("icy-metaint");
// System.out.println("Response name : " + name);
System.out.println("Response Message : " + httpClient.getHeaderFields());
}
请问是否有人可以帮助我解决这个问题。
谢谢
英文:
I've been working for 24 hours to find the problem with this online stream :
http://str45.streamakaci.com:8014
you can try it and it's working but when i try to get the header from it using Java
i get this :
Sending 'GET' request to URL : http://str45.streamakaci.com:8014
Response Code : -1
Response Message : {Content-type=[unknown/unknown]}
When i use SMSSniffer to see what happen to HTTP headers returned are correct and i get this
GET / HTTP/1.1
Content-Type: text/html; charset=UTF-8
Icy-MetaData: 1
Accept-Encoding: gzip
Content-Encoding: gzip, deflate
Cache-Control: no-cache
User-Agent: Java/1.8.0_131
Host: str45.streamakaci.com:8014
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
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 Flemme MP3
icy-genre:Various
icy-url:http://www.radioflemme.com
content-type:audio/mpeg
icy-pub:0
icy-metaint:32768
icy-br:128
with some long text below the headers.
here is the Java code to get the HTTP header which work fine with other ShoutCast stream but not this one.
private static void sendGet() throws Exception {
String url = "http://str45.streamakaci.com:8014";
HttpURLConnection httpClient =
(HttpURLConnection) new URL(url).openConnection();
// optional default is GET
httpClient.setRequestMethod("GET");
//add request header
httpClient.setRequestProperty("Content-Type", "text/html; charset=UTF-8");
httpClient.setRequestProperty("Icy-MetaData", "1");
httpClient.setRequestProperty("Accept-Encoding", "gzip");
httpClient.setRequestProperty("Content-Encoding", "gzip, deflate");
httpClient.setRequestProperty("Cache-Control", "no-cache");
int responseCode = httpClient.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
String name = httpClient.getHeaderField("icy-metaint");
// System.out.println("Response name : " + name);
System.out.println("Response Message : " + httpClient.getHeaderFields());
}
Please can anyone help me to solve this problem.
Regards
答案1
得分: 0
URL http://str45.streamakaci.com:8014 正确返回头信息,以下是在终端中使用 lynx 检查头信息的命令:
lynx -mime_header http://str45.streamakaci.com:8014
HTTP/1.0 200 OK
content-type:text/html
<HTML><HEAD><meta http-equiv="Content-Language" content="en-us"><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><meta http-equiv="Pragma" content="no-cache">.... HTML 部分
根据您的需求并不是100%清楚,所以我会提到:
http://str45.streamakaci.com:8014 是 Shoutcast 的“状态”页面的地址,不是音频流的地址。
如果您想要音频流,URL 将是:
http://str45.streamakaci.com:8014/;stream
这里的末尾的“stream”并不重要,可以是任何字符串,甚至 http://str45.streamakaci.com:8014/; 也可以正常工作。
所以,让我们来检查流的头信息:
lynx -mime_header "http://str45.streamakaci.com:8014/;stream"
ICY 200 OK
icy-notice1:<BR>This stream requires <a href="http://www.winamp.com/">Winamp</a><BR>
icy-notice2:SHOUTcast 分布式网络音频服务器/ Linux v1.9.8<BR>
icy-name:Radio Flemme MP3
icy-genre:Various
icy-url:http://www.radioflemme.com
content-type:audio/mpeg
icy-pub:0
icy-br:128
这些与 Java 无关,我无法为您提供代码,但可以确认这个 Shoutcast 正确地返回了头信息。
英文:
URL http://str45.streamakaci.com:8014 does return the header properly, here is a command to check the headers in your terminal using lynx:
lynx -mime_header http://str45.streamakaci.com:8014
HTTP/1.0 200 OK
content-type:text/html
<HTML><HEAD><meta http-equiv="Content-Language" content="en-us"><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><meta http-equiv="Pragma" content="no-cache">.... HTML follows
It is not 100% clear what you are looking for, so just in case I will mention that
http://str45.streamakaci.com:8014 URL is the address of Shoutcast "Status" page, not the audio stream.
If you want audio stream the URL will be:
http://str45.streamakaci.com:8014/;stream
"stream" at the end here is not important and can be any string or even http://str45.streamakaci.com:8014/; will work fine.
So, lets check the stream headers:
lynx -mime_header "http://str45.streamakaci.com:8014/;stream"
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 Flemme MP3
icy-genre:Various
icy-url:http://www.radioflemme.com
content-type:audio/mpeg
icy-pub:0
icy-br:128
all this is not specific to Java, I can not tell you the code, but can confirm that this Shoutcast is returning headers the right way.
答案2
得分: 0
似乎你的HTTP客户端与响应状态行不兼容:
ICY 200 OK
对于正常的HTTP连接,这个响应通常是类似于 HTTP/1.0 200 OK
的。旧版本的客户端支持这种格式,并将连接视为HTTP 1.0或0.9,但在某个时候,我们逐渐失去了这种能力。
理想情况下,你可以将这个服务器替换为现代化的解决方案。Icecast 是一个适用的替代品,同时也支持HTTPS。SHOUTcast DNAS 有更新的版本,但HTTPS会带来麻烦,并且有额外的许可要求,所以我现在甚至都不考虑它了。当然,你也可以使用HLS,根本不需要专门的流媒体服务器。(只需要了解在兼容性和延迟方面的权衡。)
如果你无法控制服务器,你需要以某种方式覆盖你的HTTP客户端,使其能够接受 ICY
风格的状态行。如果你无法做到这一点,你只能创建一个常规的TCP连接,并自行解析响应。
英文:
It seems that your HTTP client isn't compatible with the response status line:
ICY 200 OK
For a normal HTTP connection, this response is something like HTTP/1.0 200 OK
. Older clients supported this and treated the connection like HTTP 1.0 or 0.9, but somewhere along the way we've steadily lost this capability.
Ideally, you replace this server with something modern. Icecast is a drop-in replacement for you and also supports HTTPS. There are newer versions of the SHOUTcast DNAS, but HTTPS is a hassle and has additional licensing requirements so I don't even bother with it these days. And of course, you could also go HLS and not need a specialized streaming server at all. (Just understand the tradeoff there in terms of compatibility and latency.)
If you are not in control of the server, you will need to override your HTTP client somehow, to allow it to accept that ICY
-style status line. If you are unable to do that, you're stuck making a regular TCP connection and parsing the response yourself.
专注分享java语言的经验与见解,让所有开发者获益!
评论