5

I want to read data from a streaming icy protocol.The problem is that all the libraries that I've tried (dsj,MP3SPI) use the HttpUrlConnection to do this.However I've tried it on my windows 7 and I've received "Invalid http response" which is normal cause "HTTP 200 OK" is different from "ICY 200 OK".I know this could be accomplished with sockets but I'm a beginner so if any can provide a few lines o code so I can get an idea I would appreciate.Also if you have some solutions please share them.Thanx and have a nice day!

This is the code that I've tried:

URLConnection connection = new URL("89.47.247.59:8020").openConnection();
InputStream in = connection.getInputStream();
System.out.println("getting lots of bytes");
in.close();

The error is :

Exception in thread "main" java.io.IOException: Invalid Http response
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.jav‌​a:1328)
at javaapplication1.JavaApplication1.main(JavaApplication1.java:46) Java Result: 1

Sorry couldnt figure it out how to format code or add newline.

Edit: I included the code from your comment below..

nfechner
  • 17,295
  • 7
  • 45
  • 64
aureliangtx
  • 317
  • 6
  • 18

2 Answers2

5

Try this instead:

URL url=new URL("89.47.247.59:8020");
Socket socket=new Socket(url.getHost(), url.getPort());
OutputStream os=socket.getOutputStream();
String user_agent = "WinampMPEG/5.09";
String req="GET / HTTP/1.0\r\nuser-agent: "+user_agent+"\r\nIcy-MetaData: 1\r\nConnection: keep-alive\r\n\r\n";
os.write(req.getBytes());
is=socket.getInputStream();

This worked for me perfectly!

3

MP3SPI should work fine on all systems.

If you want to extract ICY metadata, check this code: https://gist.github.com/1008126 There's an IcyInputStream that opens the URL and returns a regular InputStream that you can attach to a decoder and it also extracts metadata like Artist and Track title.

I've written this code using information from here.

Denis Tulskiy
  • 19,012
  • 6
  • 50
  • 68
  • But tulskiy why am I receiving invalid http response from HttpUtlConnection?I saw code on the net that use HttpUrlConnection but why do I receive that error then?SO the returned code is -1. – aureliangtx Jul 31 '11 at 08:56
  • @aureliangtx: because SHOUTCast is not real HTTP, you said it yourself: it returns ICY 200 OK. IceCast serveres use normal HTTP and HttpURLCOnnection can parse their headers, SHOUTCast use the custom return code. Use abstract URLCOnnection instead of HttpUrlConnection. – Denis Tulskiy Jul 31 '11 at 09:01
  • This is the code that I've tried: URLConnection connection = new URL("http://89.47.247.59:8020").openConnection(); InputStream in = connection.getInputStream(); System.out.println("getting lots of bytes"); in.close(); .The error is : Exception in thread "main" java.io.IOException: Invalid Http response at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1328) at javaapplication1.JavaApplication1.main(JavaApplication1.java:46) Java Result: 1 .Sorry couldnt figure it out how to format code or add newline. – aureliangtx Jul 31 '11 at 10:14