我是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()
开始工作时会失败。我不知道该如何解决。
如指示的那样,您需要在建立连接之前立即设置代理参数/凭据。
设置代理username
,password
仅在您的代理通过身份验证的情况下。
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 {
...
我在设置参数后测试了代理后的方法,并且效果很好,即
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句