---
title: "获得URL的内容"
author: "chengjie177"
date: "2012-08-30T14:47:59Z"
tags: []
categories: ["互联网"]
slug: "url"
draft: false
lastmod: "2012-08-30T14:47:59Z"
id: 1667889
url: "https://wocece.iteye.com/blog/1667889"
---

```java
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) {
}
```