Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Leave Comments, Critiques, and Suggestions Here?

HTTP Connectivity made easy

In todays web enabled world, the ability to send and recieve data via HTTP has become indespensable for modern applications. Luckily Tango provides us with the ability to send ( HttpPost ) , recieve ( HttpGet ) - and even set up your own HttpServer to handle HTTP requests.

method='get'

Likely the most common operation is reading a page or file off of the web. The HttpGet client makes this easy.

auto page = new HttpGet ("http://www.digitalmars.com/d/");

Cout (cast(char[]) page.read) ();

The read() function returns an array of void ( void![] ) , so you can read both binary and text pages off the web. To write a binary file for example:

  char[] url = "http://digitalmars.com/d/dmlogo.gif";
  HttpGet page = new HttpGet (url);
  File fout = new File("dmlogo.gif");
  fout.write(page.read);

method='post'

Another common option is POSTing to a web page, which can be accomplished through the HttpPost client.

/* coming soon */

Http Server

Tango even has the capability to set up your own HTTP server!

        class Provider : HttpProvider
        {
                override void service (HttpRequest request, HttpResponse response)
                {
                        // return an HTML page saying "HTTP Error: 200 OK"
                        response.sendError (HttpResponses.OK);
                }
        }

        // bind server to port 8080 on a local address
        auto addr = new InternetAddress (8080);

        // create a (1 thread) server using the ServiceProvider to service requests
        auto server = new HttpServer (new Provider, addr, 1, 100);

        // start listening for requests (but this thread does not listen)
        server.start;

        // send main thread to sleep
        Thread.sleep;

This little bit of code will start an HTTP server listening on port 8080, and make itself a daemon that will constantly listen on said port.

The Provider class here ( derived from HttpProvider ) is the class that will do the actually handling of the requests. For a complete tutorial on the HTTP Server, see [false "The HTTP Server Tutorial"]

Summary

The tango.net.http package provides a quick and easy way to interact with the internet. Wether its posting to a website, or simply downloading the most recent package , all can be done programatically with tango.net.http.