0
votes

I am trying to consume rss feed via simple rest service call. But I was getting connection timeout. sample rss feed: https://www.yahoo.com/news/rss/

my code:

public class Test {

// http://localhost:8080/RESTfulExample/json/product/get
public static void main(String a[]){

      try {

        URL url = new URL("https://www.yahoo.com/news/rss/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");
        //conn.setConnectTimeout(999999);
/*
        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }*/

        BufferedReader br = new BufferedReader(new InputStreamReader(
            (conn.getInputStream())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

        conn.disconnect();

      } catch (MalformedURLException e) {

        e.printStackTrace();

      } catch (IOException e) {

        e.printStackTrace();

      }

    }

}

Exception:

java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:579) at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:625) at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:160) at sun.net.NetworkClient.doConnect(NetworkClient.java:180) at sun.net.www.http.HttpClient.openServer(HttpClient.java:432) at sun.net.www.http.HttpClient.openServer(HttpClient.java:527) at sun.net.www.protocol.https.HttpsClient.(HttpsClient.java:275) at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:371) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:933) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1301) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) at Test.main

is this wrong approach/code ? if you can suggest better approach/code that would be great .

1
Primarily your code is missing server-client header field values when you make basic http or https requests, see en.wikipedia.org/wiki/List_of_HTTP_header_fields for better details, secondly yahoo.com/news/rss is generating an xml file, so better not confuse it with jsonShayHaned
Your code works fine. As said by @ShayHaned, you have to set the request headers to application/xml from application/json. So are you behind a proxy/firewall?harshavmb
I modified to application/xml. Bu still the same issue. Regd proxy, I am not sure. If yes how should I set proxy. The same uri is working fine when I hit it on browser.user12
yes, I came to know that I am behind the proxy. How to access the urls if I am behind the proxy. How to set/unset proxy from above code ?user12

1 Answers

0
votes

Finally able to solve the issue by setting the proxies. Here is the code that worked for me.

public class Test {

public static void main(String a[]) throws ClientProtocolException, IOException{
    HttpClient httpClient = new DefaultHttpClient();
    HttpHost proxy=new HttpHost("hostname", portnumber, "scheme"); // ex: hostname="your org proxy host" portnumber=8080 scheme="http"
    httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY,proxy );

    HttpGet getRequest = new HttpGet(
        "https://www.yahoo.com/news/rss/");

    //getRequest.addHeader("accept", "application/xml");
    final RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
    getRequest.setConfig(config);


    HttpResponse response = httpClient.execute(getRequest);

    /*if (response.getStatusLine().getStatusCode() != 200) {
        throw new RuntimeException("Failed : HTTP error code : "
           + response.getStatusLine().getStatusCode());
    }*/

    BufferedReader br = new BufferedReader(
                     new InputStreamReader((response.getEntity().getContent())));

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }

    httpClient.getConnectionManager().shutdown();

}

    }