HTTP: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 67: | Line 67: | ||
<python>import urllib |
<python>import urllib |
||
print urllib.urlopen("http://www.rosettacode.org").read()</python> |
print urllib.urlopen("http://www.rosettacode.org").read()</python> |
||
One-liner |
|||
$ python -c '__import__('urllib').urlopen("http://www.rosettacode.org").read()' |
Revision as of 19:36, 26 September 2008
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
Print a URL's content.
Erlang
Synchronous
-module(main). -export([main/1]). main([Url|[]]) -> inets:start(), case http:request(Url) of {ok, {_V, _H, Body}} -> io:fwrite("~p~n",[Body]); {error, Res} -> io:fwrite("~p~n", Res) end.
Asynchronous
-module(main). -export([main/1]). main([Url|[]]) -> inets:start(), http:request(get, {Url, [] }, [], [{sync, false}]), receive {http, {_ReqId, Res}} -> io:fwrite("~p~n",[Res]); _Any -> io:fwrite("Error: ~p~n",[_Any]) after 10000 -> io:fwrite("Timed out.~n",[]) end.
Using it
|escript ./req.erl http://www.rosettacode.org
Java
<java>import java.net.*; import java.io.*;
public class Main {
public static void main(String[] args) throws Exception { URL url = new URL("http://www.rosettacode.org"); InputStreamReader isr = new InputStreamReader(url.openStream()); BufferedReader reader = new BufferedReader(isr); String line = ""; while ( (line = reader.readLine()) != null) { System.out.println(line); } reader.close(); }
}</java>
Perl
<perl>using LWP::Simple; print get("http://www.rosettacode.org");</perl>
PHP
<php>print(file_get_contents("http://www.rosettacode.org"));</php>
Python
<python>import urllib print urllib.urlopen("http://www.rosettacode.org").read()</python>
One-liner
$ python -c '__import__('urllib').urlopen("http://www.rosettacode.org").read()'