Question

I am new to Java , but really want to become better at it. I'm trying to write a simple RSS reader. Here's the code:

import java.io.*;
import java.net.*;

public class RSSReader {
public static void main(String[] args) {
    System.out.println(readRSS("http://www.usnews.com/rss/health-news"));
}
public static String readRSS(String urlAddress){
    try {
            URL rssUrl = new URL(urlAddress);
            BufferedReader in = new BufferedReader(new InputStreamReader(rssUrl.openStream()));
            String sourceCode = "";
            String line;
            while((line = in.readLine())!=null){
                if(line.contains("<title>")){
                    int firstPos = line.indexOf("<title>");
                    String temp = line.substring(firstPos);
                    temp = temp.replace("<title>","");
                    int lastPos = temp.indexOf("</title>");
                    temp = temp.substring(0,lastPos);
                    sourceCode +=temp+"\n";
                }
            }
        System.out.println("YAAAH"+sourceCode);
        in.close();

        return sourceCode;
    }   catch (MalformedURLException ue) {
            System.out.println("Malformed URL");
    }   catch (IOException ioe) {
            System.out.println("WTF?");
    }
    return null;
}
}

But it is catching IOException all the time, and I see "WTF". I realised that the whole program fails when OpenStream() starts its' work. I don't know how to fix it.

Was it helpful?

Solution

As indicated, you would need to set your proxy parameters/credentials right before you establish a connection.

Set proxy username and password only in case your proxy is authenticated.

public static String readRSS(String urlAddress) {

System.setProperty("http.proxyHost", YOUR_PROXY_HOST);
System.setProperty("http.proxyPort", YOUR_PROXY_PORT);

//Below 2 for authenticated proxies only
System.setProperty("http.proxyUser", YOUR_USERNAME);
System.setProperty("http.proxyPassword", YOUR_PASSWORD);

 try {
    ...

I tested your method behind a proxy and it works perfectly, after setting the parameters i.e.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top