Question

I am trying to get an HTML page source content from this site: "http://207.200.96.231:8008" using Java. However the default libraries of Java did not help me in this one. I also tried using this tutorial, but it did not work either. I think the problem occurs because of a security protection of the site. When I run the following code provided below I get an exception: java.io.IOException: Invalid Http response.

Any ideas of how to implement the code? Or are there any libraries that can serve my need? So far I have tried JSoup and Jericho HTML Parser thinking that they would use a different approach connecting to the site I provided, but they also failed to work too.

String urlstr = "http://72.26.204.28:9484/played.html";

try {

    URL url = new URL(urlstr);

    URLConnection urlc = url.openConnection();

    InputStream stream = urlc.getInputStream();
    BufferedInputStream buf = new BufferedInputStream(stream);

    StringBuilder sb = new StringBuilder();

    while ( true){

    int data = buf.read();

    if ( data == -1)
        break;
    else
        sb.append((char)data);
    }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
}

EDIT (Problem Solved): With the help of Karai17 and trashgod I managed to solve this issue. Shoutcast page needs a user-agent to access its contents. So all we need to do was to add this code:

urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0");

The latest code looks like this:

try {
        URL url = new URL("http://207.200.96.231:8008/7.html");
        HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
        urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0");

        InputStream is = urlConnection.getInputStream();
        BufferedInputStream in = new BufferedInputStream(is);
        int c;
        while ((c = in.read()) != -1) {
            System.out.write(c);
        }
        urlConnection.disconnect();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
}
Was it helpful?

Solution

This stream appears to require Winamp.

$ curl -v http://207.200.96.231:8008
* About to connect() to 207.200.96.231 port 8008 (#0)
*   Trying 207.200.96.231... connected
* Connected to 207.200.96.231 (207.200.96.231) port 8008 (#0)
It appears to require [Winamp][2].

> GET / HTTP/1.1
> User-Agent: curl/...
> Host: 207.200.96.231:8008
> Accept: */*
> 
ICY 200 OK
icy-notice1:
This stream requires Winamp
icy-notice2:SHOUTcast Distributed Network Audio Server/Linux v1.9.93atdn
icy-name:Absolutely Smooth Jazz - SKY.FM - the world's smoothest jazz 24 hours a day icy-genre:Soft Smooth Jazz icy-url:http://www.sky.fm/smoothjazz/ content-type:audio/mpeg icy-pub:1 icy-br:96 ...

Addendum: You can read the stream like this:

URL url = new URL("http://207.200.96.231:8008");
URLConnection con = url.openConnection();
InputStream is = con.getInputStream();
BufferedInputStream in = new BufferedInputStream(is);
int c;
while ((c = in.read()) != -1) {
    System.out.write(c);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top