我试图打开我的大学网站来阅读他们的菜单。我已经写了一个版本,可以直接读取菜单链接到菜单链接的菜单,但是我想将它拉回一点,以便从网站上读取菜单,而不是直接链接(如果链接发生更改) 。网站不喜欢Java吗?
这里是我打开的网址: https://nccudining.sodexomyway.com/dining-choices/index.html
每当我打开链接的网站,这是我得到的输出:
302
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="http://m-nccudining.sodexomyway.com/dining-choices/index.html">here</a>.</h2>
</body></html>
它输出的网址是移动版的网站,但当我尝试使用该网址时,它不输出任何内容。
这是我的代码:
import java.io.*;
import java.net.*;
public class test
{
public static void main(String[] args)
{
URL url = null;
try
{
url = new URL("https://nccudining.sodexomyway.com/dining-choices/index.html");
HttpURLConnection test = (HttpURLConnection) url.openConnection();
test.setInstanceFollowRedirects(true);
test.connect();
System.out.println(test.getResponseCode());
} catch (MalformedURLException e1)
{
System.out.println("URL cannot be opened.");
return;
}
BufferedReader in = null;
try
{
in = new BufferedReader(new InputStreamReader(url.openStream()));
} catch (IOException e)
{
System.out.println("Error");
}
String inputLine;
try
{
while ((inputLine = in.readLine()) != null)
{
System.out.println(inputLine);
}
} catch (IOException e)
{
System.out.println("Error");
}
}
}
我所有的try/catch循环道歉。我不想仅仅从一开始就抛出一个IOException异常,因为我听说这是不好的做法。无论如何,这段代码只是打开URL,建立一个连接,所以我可以确保URL实际存在,并尝试阅读它的HTML。它适用于我尝试过的任何其他网站,包括谷歌。
我的问题是为什么我的代码不能读取网站的正确源代码?我的代码有问题吗(我想在HttpsURLConnection中添加并允许重定向会起作用),还是仅仅是网站,我有什么可以绕开每周菜单的页面来绕过这些?
找到解决方案!感谢@ShayHaned的修复。我添加下列行到HttpURLConnection类所以我得到一个200响应代码,而不是302:
test = (HttpURLConnection) url.openConnection();
test.setRequestMethod("GET");
test.setRequestProperty("User-Agent", "Mozilla/5.0");
test.setInstanceFollowRedirects(true);
然后我从URL打开流从HttpURLConnection类获取输入流改变了的InputStream,如图所示:
BufferedReader in = new BufferedReader(new InputStreamReader(test.getInputStream()));
这给了我正在寻找的HTML。
什么是响应代码?如果它不是301或类似的,问题出现在服务器端:它们不发布重定向,所以Java没有遵循。 – EJP
@EJP这就是他的第一个代码块...... –
它读取网页的HTML。现在,它没有做任何事情,因为我只是想解决这个问题。 – ds777fighter