5

I am working on creating a Video sitemap for a site that has hosted videos on Brightcove video cloud. In order to get all the video information from the site, Brightcove suggests to read the response from their url of following form

http://api.brightcove.com/services/library?token="+accountToken+"&page_size=1&command=find_all_videos&output=JSON&get_item_count=true

the output of the url is in JSON, where accountToken is just an identifier of the account.

When I hit the above url with Token in the browser, it gives me the correct response.

I wrote below program snippet to read from that url

URL jsonURL = new URL("http://api.brightcove.com/services/library?token="+accountToken+"&page_size=1&command=find_all_videos&output=JSON&get_item_count=true");
        HttpURLConnection connection = (HttpURLConnection) jsonURL.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(connection.getInputStream()));
        String lineRead = "";
        while (reader.ready()) {
            lineRead = lineRead + reader.readLine();
        }

As my browser uses proxy, I added below code to include proxy settings

        System.setProperty("http.proxyHost", "my.proxyurl.com");
        System.setProperty("http.proxyPort", "80");

Without using proxy settings, it returns java.net.ConnectException: Connection refused: connect and with proxy it gives me java.io.IOException: Server returned HTTP response code: 503

So my question is , why is it giving me a 503(Service Unavailable) error ? From the browser its working fine.

Update 1: It seems like an issue with the Network. I pinged the domain and it said "Request Timed out". Working via HTTP though. Looks like an issue with the Firewall.

Shashank Kadne
  • 7,993
  • 6
  • 41
  • 54
  • @jtahlborn:Reverse proxy. With what context are you asking this ? – Shashank Kadne Sep 06 '12 at 15:57
  • Can you provide with temporary test accountToken? – Jama A. Sep 06 '12 at 15:59
  • is it a socks proxy perhaps? some other sort of proxy? – jtahlborn Sep 06 '12 at 16:00
  • @JamshidAsatillayev: Just remove the token parameter and try. It will return a JSON with an Error message. but that's not working too. – Shashank Kadne Sep 06 '12 at 16:05
  • @jtahlborn: I really don't know. what if it is a socks proxy? – Shashank Kadne Sep 06 '12 at 16:11
  • You probably getting some response body with your 503 response indicating the reason. At minimum, this will contain an indication whether 503 is returned by the proxy or by an API endpoint. Could you try reading the whole HTTP response and posting it here? – Sergey Mikhanov Sep 11 '12 at 22:26
  • 1
    Additionally, I would try using a tool like Wireshark to see the HTTP messages coming in and out -- maybe they not even reaching the proxy, or not being authenticated by it, etc – Sergey Mikhanov Sep 11 '12 at 22:28
  • 1
    I have tried your code (I am *also* behind the proxy) and it worked fine, taking into account these two lines in Jamshid's answer. Check once more if you entered correct address and port and if your proxy has been configured properly, has no security restrictions etc. – Miljen Mikic Sep 13 '12 at 07:32

2 Answers2

3

I think, it may due to your internet connection, I have tried your code I didn't get any 503(Service Unavailable). Check out with different connection connection(without proxy) and it should work. Or you can try it with slightly different approach:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("host", "port));
conn = new URL(jsonURL).openConnection(proxy);

If you have SOCKS type proxy, change Proxy's constructor parameter to Proxy.Type.SOCKS.

Jama A.
  • 15,680
  • 10
  • 55
  • 88
  • I already tried that approach for proxy....didn't try with type Proxy.Type.SOCKS though....let me try it and i ll let you know..... – Shashank Kadne Sep 07 '12 at 04:24
  • Not working. I got `java.net.SocketException: Malformed reply from SOCKS server`. No point in trying it in a different connection without proxy.I have to make it work in the client environment which requires proxy..:( – Shashank Kadne Sep 10 '12 at 14:32
  • As you said it looks like a network issue. I have added an update to my question. Your answer surely does deserve this bounty. Thanks. – Shashank Kadne Sep 13 '12 at 10:59
0

Minor correction to Jamas code

String host="myproxy.com";
int port=8080;
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));