HttpURLConnection returns 503 error when accessed through proxy

Shashank Kadne picture Shashank Kadne · Sep 4, 2012 · Viewed 9k times · Source

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.

Answer

Õzbek picture Õzbek · Sep 6, 2012

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.