vendredi 25 juillet 2008

Python URL Handling (HTTP)

Now this is another example of the expressiveness you get from a scripting language:

Get some HTTP resource using Python urllib:
 import urllib 
my_url='http://diveintomark.org/xml/atom.xml'
data = urllib.urlopen(my_url).read()
print data

Same thing in Java:

 public class HttpClient
{
public static void main(String[] args) throws IOException {
URL url = new URL("http://diveintomark.org/xml/atom.xml");
URLConnection con = url.openConnection();
con.connect();
HttpURLConnection httpConnex = (HttpURLConnection) con;
InputStream inputStream = httpConnex.getInputStream();
BufferedReader b = new BufferedReader(new InputStreamReader(inputStream));
String l="";
while ((l=b.readLine())!=null)
System.out.println(l);
}
}

Aucun commentaire: