java读取url字符内容

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL; [size=x-small][/size]

public class UrlTest {
public static void main(String[] args) {
String urlvalue = "http://tool.oschina.net/codeformat/js/";
BufferedReader in = null;
try {
URL url = new URL(urlvalue);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
System.out.println(in.readLine().toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}[/size][/size][/size][/size]

猜你喜欢

转载自xiyuhanfei.iteye.com/blog/2406132