HTTP: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 55: Line 55:
link cfunc
link cfunc
procedure main(args)
procedure main(args)

get(args[1], args[2], args[3])
get(args[1])
end
end
procedure get(host, port, url)
procedure get(url)
local f
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")
f := tconnect(host, port) | stop("Unable to connect")
writes(f, "GET ", url ," HTTP/1.0\r\n\r\n")
writes(f, "GET ", path | "/" ," HTTP/1.0\r\n\r\n")
while write(read(f))
while write(read(f))
end
end

Using it
Using it
|icon req.icn www.rosettacode.org 80 /
|icon req.icn http://www.rosettacode.org


=={{header|Java}}==
=={{header|Java}}==

Revision as of 14:28, 9 December 2008

Task
HTTP
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.

C#

Not tested.

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);
  }
}

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

<java>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());         
    }

}</java>

Apache Commons IO

<java>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);    	    	    		    
   }

}</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 url = urllib.urlopen("http://www.rosettacode.org") print url.read() url.close()</python>

<python>import urllib print urllib.urlopen("http://rosettacode.org").read()</python>

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]