java - 在 Java 中从 URL 读取/写入内容的首选方法是什么

标签 java url

打开网站连接然后阅读该页面上的信息的首选方式是什么?似乎有很多关于不同部分的具体问题,但没有明确和简单的例子。

最佳答案

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/

相关文章:

java - 是否可以在一个事务中使用多个持久化单元,而不是 XA?

java - JSP动态表单 Action 属性没有JS?

java - spring版本测试初始化​​VM时出错

php - 如何从 codeigniter 中的 url 获取 #jumper 部分?

javascript - 更改哈希值时重新加载页面

javascript - 隐藏 URL 中的#Javascript

java - 从 SwingWorker 接收数据

java - Apache Commons HttpComponents 示例

css - 通过链接时控制路径

javascript - 将 URL 添加到一页 Web 应用程序