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..

3
  • Way to welcome a new user: 2 downvotes! Do you think he'll come back and contribute to SO? Commented Jul 31, 2011 at 11:42
  • Well since you guys don't know the answer i'm not surprised. Commented Jul 31, 2011 at 15:44
  • by the way, you're currently at -1 because you have two downvotes and one upvote: I'm the one who gave you +1. Commented Jul 31, 2011 at 15:50

2 Answers 2

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.

3
  • 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. Commented Jul 31, 2011 at 8: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. Commented Jul 31, 2011 at 9:01
  • 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.java:1328) at javaapplication1.JavaApplication1.main(JavaApplication1.java:46) Java Result: 1 .Sorry couldnt figure it out how to format code or add newline. Commented Jul 31, 2011 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.