打开网站连接然后阅读该页面上的信息的首选方式是什么?似乎有很多关于不同部分的具体问题,但没有明确和简单的例子。
最佳答案
Getting Text from a URL | Example Depot :
try {
// Create a URL for the desired page
URL url = new URL("http://hostname:80/index.html");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
// str is one line of text; readLine() strips the newline character(s)
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
当谈到“写入”一个 URL 时,我想您会想要像 Sending a POST Request Using a URL | Example Depot 这样的东西.
关于java - 在 Java 中从 URL 读取/写入内容的首选方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2861119/