在Java中:
URL url = new URL("http://stackoverflow.com");
URLConnection connection = new URLConnection(url);
InputStream stream = url.openConnection();
// ... read stream like any file stream
Run Code Online (Sandbox Code Playgroud)
此代码适用于脚本编写和内部使用.我不反对将其用于生产用途.它不处理超时和连接失败.
我建议使用HttpClient库进行生产.它支持身份验证,重定向处理,线程,池等.