我正在尝试制作一个简单的Android应用,可以从网站(https://www.lottostat.dk/rssfeed.php)中检索彩票号码。我尝试使用此处提供的示例代码(并在下面插入):Using Java to pull data from a webpage?
使用原始目标网站(Using Java to pull data from a webpage?)时,示例代码效果很好,我可以在Android Studio的输出中读取整个底层html代码。但是当我将目标网站更改为我想从(https://www.lottostat.dk/rssfeed.php)获取数据的网站时,没有输出(br.readLine()返回null)。
这可能是什么问题?我是否可能需要一个不同的解决方案来阅读.php网站(即使底层代码似乎是纯XML)?
以下是工作原始示例代码供参考:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class DownloadPage {
public static void main(String[] args) throws IOException {
// Make a URL to the web page
URL url = new URL("http://stackoverflow.com/questions/6159118/using-java-to-pull-data-from-a-webpage");
// Get the input stream through URL Connection
URLConnection con = url.openConnection();
InputStream is =con.getInputStream();
// Once you have the Input Stream, it's just plain old Java IO stuff.
// For this case, since you are interested in getting plain-text web page
// I'll use a reader and output the text content to System.out.
// For binary content, it's better to directly read the bytes from stream and write
// to the target file.
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
// read each line and write to System.out
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
答案 0 :(得分:1)
显然,该网站依赖于用户代理。添加User-Agent标头可以解决问题。尝试使用
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "Mozilla/5.0");
InputStream is =con.getInputStream();
答案 1 :(得分:0)
添加一个用户代理,应该做的伎俩(用android 5.1.1设备测试):
URL url = new URL("https://www.lottostat.dk/rssfeed.php");
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "Mozilla");
替代方案:使用jsoup
Document doc = Jsoup.connect("https://www.lottostat.dk/rssfeed.php").userAgent("Mozilla").get();
String content = doc.toString();