这是网上的一篇 blog,它列出了 HTTP GET 的 Java 和 Python 实现代码。
Java 实现:
import java.net.*; import java.io.*; public class jget { public static void main (String[] args) throws IOException { try { URL url = new URL("http://www.google.com"); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String str; while ((str = in.readLine()) != null) { System.out.println(str); } in.close(); } catch (MalformedURLException e) {} catch (IOException e) {} } }
Python 实现:
import urllib print urllib.urlopen('http://www.google.com').read()
Python 的实现代码比 Java 简单很多很多,这得益于 Python 内建的库。不过,这里并不是比较两种语言的优劣。主要是想看看,究竟要实现 HTTP GET,可以多简单?!看看下面其他人的代码。
Groovy(基于 JVM 的脚本语言):
new URL("http://google.com").text
下面还是 Python 的,是上面 Python 代码的简化版:
print __import__('urllib').urlopen('http://www.google.com').read()
参考资料: