java - RSS 阅读器 openStream()

标签 java rss rss-reader

我是 Java 的新手,但真的想在这方面做得更好。我正在尝试编写一个简单的 RSS 阅读器。这是代码:

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;
}
}

但它一直在捕获 IOException,我看到了“WTF”。 我意识到当 OpenStream() 开始工作时整个程序失败了。 我不知道如何修复它。

最佳答案

如前所述,您需要在建立连接之前设置您的代理参数/凭据

设置代理用户名密码以防您的代理通过身份验证。

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 {
    ...

我在代理后面测试了你的方法,它在设置参数后完美运行

关于java - RSS 阅读器 openStream(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21093362/

相关文章:

java - 动态 xml 到 mongoDB

php - curl : can't fetch rss from website because of CloudFlare

javascript - 无法在全局变量中存储值

html - 如何将 CSS 应用于 iframe?

php - 检查 url 是否有效以及 php 中的有效 XML

java - 使用 Sax 解析器的 RSS 阅读器丢失标题中的字符

java - 有什么方法可以恢复 Netbeans 7.2 书签导航的旧样式?

java - 如何在java中动态地将MenuItem添加到TrayIcon的菜单中?

java - 如何从 android 中的 firebase 数据库中的 uid 获取/检索多个数据?

python - 在 Pelican 静态站点生成器 ATOM 提要中使用绝对 URL