Some of Sunday's edits have been lost. The edits from Saturday that were reverted have been restored. Site is now hosted on prgmr.com. Thank you for your patience. This notice will be removed one week from posting. --Michael Mol 18:12, 7 March 2010 (UTC)
HTTP
From Rosetta Code
Print a URL's content (source code) to the console. There is a separate task for HTTPS Requests.
[edit] 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);
}
}
}
[edit] Ada
Library: AWS
with Ada.Text_IO; use Ada.Text_IO;
with AWS.Client;
with AWS.Response;
procedure HTTP_Request is
begin
Put_Line (AWS.Response.Message_Body (AWS.Client.Get (URL => "http://www.rosettacode.org")));
end HTTP_Request;
[edit] AutoHotkey
UrlDownloadToFile, http://rosettacode.org, url.html
Run, cmd /k type url.html
[edit] C
Library: libcurl
#include <stdio.h>
#include <stdlib.h>
#include "curl/curl.h"
int
main(void)
{
CURL *curl;
char buffer[CURL_ERROR_SIZE];
if ((curl = curl_easy_init()) != NULL) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.rosettacode.org/");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, buffer);
if (curl_easy_perform(curl) != CURLE_OK) {
fprintf(stderr, "%s\n", buffer);
return EXIT_FAILURE;
}
curl_easy_cleanup(curl);
}
return EXIT_SUCCESS;
}
[edit] C++
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
int main() {
WSADATA wsaData;
WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
addrinfo *result = NULL;
addrinfo hints;
ZeroMemory( &hints, sizeof( hints ) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
getaddrinfo( "74.125.45.100", "80", &hints, &result ); // http://www.google.com
SOCKET s = socket( result->ai_family, result->ai_socktype, result->ai_protocol );
connect( s, result->ai_addr, (int)result->ai_addrlen );
freeaddrinfo( result );
send( s, "GET / HTTP/1.0\n\n", 16, 0 );
char buffer[512];
int bytes;
do {
bytes = recv( s, buffer, 512, 0 );
if ( bytes > 0 ) {
for ( int i = 0; i < bytes; ++i ) {
std::cout << buffer[i];
}
}
} while ( bytes > 0 );
return 0;
}
[edit] C#
using System;
using System.Text;
using System.Net;
class Program
{
static void Main(string[] args)
{
WebClient wc = new WebClient();
string content = wc.DownloadString("http://www.google.com");
Console.WriteLine(content);
}
}
[edit] Clojure
(defn get-http [url]
(let [sc (java.util.Scanner.
(.openStream (java.net.URL. url)))]
(while (.hasNext sc)
(println (.nextLine sc)))))
(get-http "http://www.rosettacode.org")
[edit] Common Lisp
CLISP provides an extension function to read http sources. Other implementations may do this differently. Works with: CLISP
(defun wget-clisp (url)
(ext:with-http-input (stream url)
(loop for line = (read-line stream nil nil)
while line
do (format t "~a~%" line))))
Library: DRAKMA
First grabbing the entire body as a string, and then by pulling from a stream (as in the CLISP example).
(defun wget-drakma-string (url &optional (out *standard-output*))
"Grab the body as a string, and write it to out."
(write-string (drakma:http-request url) out))
(defun wget-drakma-stream (url &optional (out *standard-output*))
"Grab the body as a stream, and write it to out."
(loop with body = (drakma:http-request url :want-stream t)
for line = (read-line body nil nil)
while line do (write-line line out)
finally (close body)))
[edit] D
import tango.io.Console;
import tango.net.SocketConduit,
tango.net.InternetAddress;
void main()
{
auto site = new SocketConduit;
site.connect (new InternetAddress("google.com",80)).write ("GET / HTTP/1.0\n\n");
Cout.stream.copy (site);
}
[edit] E
when (def t := <http://www.rosettacode.org> <- getText()) -> {
println(t)
}
[edit] Erlang
[edit] 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.
[edit] 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
[edit] F#
In F# we can just use the .NET library to do this so its the same as the C# example.
let wget (url : string) =
use c = new System.Net.WebClient()
c.DownloadString(url)
printfn "%s" (wget "http://www.rosettacode.com/")
However unlike C#, F# can use an asynchronous workflow to avoid blocking any threads while waiting for a response from the server. To asynchronously download three url's at once...
let wgetAsync url =
async { let request = WebRequest.Create (url:string)
use! response = request.AsyncGetResponse()
use responseStream = response.GetResponseStream()
use reader = new StreamReader(responseStream)
return reader.ReadToEnd() }
let urls = ["http://www.rosettacode.com/"; "http://www.yahoo.com/"; "http://www.google.com/"]
let content = urls
|> List.map wgetAsync
|> Async.Parallel
|> Async.RunSynchronously
[edit] Factor
USE: http.client
"http://www.rosettacode.org" http-get nip print
[edit] Forth
Works with: GNU Forth version 0.7.0 This works at the socket level, returning both the HTTP headers and page contents.
include unix/socket.fs
s" localhost" 80 open-socket
dup s\" GET / HTTP/1.0\n\n" rot write-socket
dup pad 8092 read-socket type
close-socket
[edit] Groovy
new URL("http://www.rosettacode.org").eachLine { println it }
[edit] Haskell
Using Library: HTTP from HackageDB
import Network.Browser
import Network.HTTP
import Network.URI
httpreq = do
rsp <- Network.Browser.browse $ do
setAllowRedirects True
setOutHandler $ const (return ())
request $ getRequest "http://www.rosettacode.org/"
putStrLn $ rspBody $ snd rsp
[edit] 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
[edit] J
Using getHTTP from Web Scraping
getHTTP 'http://www.rosettacode.org'
[edit] 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());
}
}
Apache Commons IO
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);
}
}
[edit] Lua
local http = require("socket.http")
function url_encode(str)
if (str) then
str = string.gsub (str, "\n", "\r\n")
str = string.gsub (str, "([^%w ])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end
function url_decode(str)
str = string.gsub (str, "+", " ")
str = string.gsub (str, "%%(%x%x)",
function(h) return string.char(tonumber(h,16)) end)
str = string.gsub (str, "\r\n", "\n")
return str
end
local page = http.request( 'http://www.google.com/m/search?q=' .. url_encode("lua") )
print( page )
[edit] Mathematica
Print[Import["http://www.google.com/webhp?complete=1&hl=en", "Source"]]
[edit] OCaml
let () =
let url = "http://www.rosettacode.org" in
let _,_, page_content = make_request ~url ~kind:GET () in
print_endline page_content;
;;
The source code of the function make_request is here.
[edit] Oz
When creating a file object, it is possible to specify an URL instead of a filename:
declare
fun {GetPage Url}
F = {New Open.file init(url:Url)}
Contents = {F read(list:$ size:all)}
in
{F close}
Contents
end
in
{System.showInfo {GetPage "http://www.rosettacode.org"}}
Library: OzHttpClient
If you need more fine-grained control of the request, you could use a custom library:
declare
[HTTPClient] = {Module.link ['x-ozlib://mesaros/net/HTTPClient.ozf']}
fun {GetPage Url}
Client = {New HTTPClient.urlGET
init(inPrms(toFile:false toStrm:true)
httpReqPrms
)}
OutParams
HttpResponseParams
in
{Client getService(Url ?OutParams ?HttpResponseParams)}
{Client closeAll(true)}
OutParams.sOut
end
in
{System.showInfo {GetPage "http://www.rosettacode.org"}}
[edit] Perl
use LWP::Simple;
print get("http://www.rosettacode.org");
[edit] PHP
print(file_get_contents("http://www.rosettacode.org"));
[edit] PicoLisp
(load "@lib/http.l")
(client "rosettacode.org" 80 NIL # Connect to rosettacode
(out NIL (echo)) ) # Echo to standard output
[edit] PowerShell
$wc = New-Object Net.WebClient
$wc.DownloadString('http://www.rosettacode.org')
[edit] PureBasic
InitNetwork()
OpenConsole()
tmpdir$ = GetTemporaryDirectory()
filename$ = tmpdir$ + "PB_tempfile" + Str(Random(200000)) + ".html"
If ReceiveHTTPFile("http://rosettacode.org/wiki/Main_Page", filename$)
If ReadFile(1, filename$)
Repeat
PrintN(ReadString(1))
Until Eof(1)
Input()
; to prevent console from closing if on windows
CloseFile(1)
EndIf
DeleteFile(filename$)
EndIf
Another solution using general networking commands
InitNetwork()
OpenConsole()
id = OpenNetworkConnection("rosettacode.org", 80)
SendNetworkString(id, "GET /wiki/Main_Page HTTP/1.1" + Chr(10) + "Host: rosettacode.org" + Chr(10) + Chr(10))
Repeat
If NetworkClientEvent(id) = 2
a$ = Space(1000)
ReceiveNetworkData(id, @a$, 1000)
out$ + a$
EndIf
Until FindString(out$, "</html>", 0)
PrintN(out$)
; next line only to prevent console from closing on Windows
Input()
Of course you could use wget too.
[edit] Python
import urllib
url = urllib.urlopen("http://www.rosettacode.org")
print url.read()
url.close()
import urllib
print urllib.urlopen("http://rosettacode.org").read()
Python 3
import urllib.request
print(urllib.request.urlopen("http://rosettacode.org").read())
[edit] R
Library: RCurl
Library: XML
First, retrieve the webpage.
library(RCurl)
webpage <- getURL("http://rosettacode.org")
#If you are linking to a page that no longer exists and need to follow the redirect, use followlocation=TRUE
webpage <- getURL("http://www.rosettacode.org", .opts=list(followlocation=TRUE))
#If you are behind a proxy server, you will need to use something like:
webpage <- getURL("http://rosettacode.org",
.opts=list(proxy="123.123.123.123", proxyusername="domain\\username", proxypassword="mypassword", proxyport=8080))
#Don't forget that backslashes in your username or password need to be escaped!
Now parse the html code into a tree and print the html.
library(XML)
pagetree <- htmlTreeParse(webpage )
pagetree$children$html
[edit] REBOL
print read http://rosettacode.org
[edit] Ruby
require 'open-uri'
require 'kconv'
puts open("http://rosettacode.org").read
[edit] Tcl
package require http
set request [http::geturl "http://www.rosettacode.org"]
puts [http::data $request]
http::cleanup $request
[edit] UNIX Shell
curl -L http://rosettacode.org/
lynx -source http://rosettacode.org/
(sleep 2 && echo "GET / HTTP/1.0" && echo && sleep 2) | nc rosettacode.org 80
(sleep 2 && echo "GET / HTTP/1.0" && echo && sleep 2) | telnet rosettacode.org 80
wget -O - -o /dev/null http://rosettacode.org/
[edit] Visual Basic .NET
Imports System.Net
Dim client As WebClient = New WebClient()
Dim content As String = client.DownloadString("http://www.google.com")
Console.WriteLine(content)







