HTTP: Difference between revisions
mNo edit summary |
(code -> lang) |
||
Line 4: | Line 4: | ||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
< |
<lang actionscript>package |
||
package |
|||
{ |
{ |
||
import flash.display.Sprite; |
import flash.display.Sprite; |
||
Line 25: | Line 24: | ||
} |
} |
||
} |
} |
||
}</lang> |
|||
} |
|||
</code> |
|||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<lang csharp>using System; |
||
using System; |
|||
using System.Net; |
using System.Net; |
||
using System.Text; |
using System.Text; |
||
Line 45: | Line 42: | ||
Console.WriteLine(content); |
Console.WriteLine(content); |
||
} |
} |
||
}</lang> |
|||
} |
|||
</code> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
Line 102: | Line 98: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<lang java5>import java.util.Scanner; |
||
import java.util.Scanner; |
|||
import java.net.URL; |
import java.net.URL; |
||
Line 112: | Line 107: | ||
while( sc.hasNext() ) System.out.println(sc.nextLine()); |
while( sc.hasNext() ) System.out.println(sc.nextLine()); |
||
} |
} |
||
}</lang> |
|||
} |
|||
</code> |
|||
Apache Commons IO |
Apache Commons IO |
||
⚫ | |||
<code java5> |
|||
⚫ | |||
import java.net.*; |
import java.net.*; |
||
Line 125: | Line 118: | ||
IOUtils.copy(new URL("http://rosettacode.org").openStream(),System.out); |
IOUtils.copy(new URL("http://rosettacode.org").openStream(),System.out); |
||
} |
} |
||
}</lang> |
|||
} |
|||
</code> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<lang perl>using LWP::Simple; |
||
⚫ | |||
using LWP::Simple; |
|||
⚫ | |||
</code> |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
⚫ | |||
<code php> |
|||
⚫ | |||
</code> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
Revision as of 22:47, 29 January 2009
You are encouraged to solve this task according to the task description, using any language you may know.
Print a URL's content (source code) to the console.
ActionScript
<lang actionscript>package {
import flash.display.Sprite; import flash.events.Event; import flash.net.*;
public class RequestExample extends Sprite { public function RequestExample() { var loader:URLLoader = new URLLoader(); loader.addEventListener(Event.COMPLETE, loadComplete); loader.load(new URLRequest("http://www.rosettacode.org")); }
private function loadComplete(evt:Event):void { trace(evt.target.data); } }
}</lang>
C#
<lang csharp>using System; using System.Net; using System.Text;
class Program {
static void Main() { string url = "http://www.rosettacode.org/"; WebClient wc = new WebClient(); byte[] data = wc.DownloadData(url); string content = Encoding.UTF8.GetString(data); Console.WriteLine(content); }
}</lang>
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
Icon
link cfunc procedure main(args) get(args[1]) end procedure get(url) local f, host, port, path url ? { ="http://" | ="HTTP://" host := tab(upto(':/') | 0) if not (=":" & (port := integer(tab(upto('/'))))) then port := 80 if pos(0) then path := "/" else path := tab(0) } write(host) write(path) f := tconnect(host, port) | stop("Unable to connect") writes(f, "GET ", path | "/" ," HTTP/1.0\r\n\r\n") while write(read(f)) end
Using it
|icon req.icn http://www.rosettacode.org
Java
<lang java5>import java.util.Scanner; import java.net.URL;
public class Main {
public static void main(String[] args) throws Exception { URL url = new URL("http://www.rosettacode.org"); Scanner sc = new Scanner(url.openStream()); while( sc.hasNext() ) System.out.println(sc.nextLine()); }
}</lang>
Apache Commons IO
<lang java5>import org.apache.commons.io.IOUtils; import java.net.*;
public class Main {
public static void main(String[] args) throws Exception { IOUtils.copy(new URL("http://rosettacode.org").openStream(),System.out); }
}</lang>
Perl
<lang perl>using LWP::Simple; print get("http://www.rosettacode.org");</lang>
PHP
<lang php>print(file_get_contents("http://www.rosettacode.org"));</lang>
Python
import urllib
url = urllib.urlopen("http://www.rosettacode.org")
print url.read()
url.close()
import urllib
print urllib.urlopen("http://rosettacode.org").read()
Ruby
require 'open-uri'
require 'kconv'
puts open("http://rosettacode.org").read
Tcl
package require http
set request [http::geturl "http://www.rosettacode.org"]
puts [http::data $request]
UNIX Shell
wget http://www.rosettacode.org -O tmp -o /dev/null
cat tmp
rm tmp
or
curl -s http://www.rosettacode.org/