1 /******************************************************************************* 2 3 copyright: Copyright (c) 2004 Kris Bell. All rights reserved 4 5 license: BSD style: $(LICENSE) 6 7 version: Initial release: January 2006 8 9 author: Kris 10 11 *******************************************************************************/ 12 13 module tango.net.http.HttpGet; 14 15 public import tango.net.Uri; 16 17 private import tango.net.http.HttpClient, 18 tango.net.http.HttpHeaders; 19 20 /******************************************************************************* 21 22 Supports the basic needs of a client making requests of an HTTP 23 server. The following is a usage example: 24 --- 25 // open a web-page for reading (see HttpPost for writing) 26 auto page = new HttpGet ("http://www.digitalmars.com/d/intro.html"); 27 28 // retrieve and flush display content 29 Cout (cast(char[]) page.read) (); 30 --- 31 32 *******************************************************************************/ 33 34 class HttpGet : HttpClient 35 { 36 alias HttpClient.read read; 37 38 /*********************************************************************** 39 40 Create a client for the given URL. The argument should be 41 fully qualified with an "http:" or "https:" scheme, or an 42 explicit port should be provided. 43 44 ***********************************************************************/ 45 46 this (const(char)[] url) 47 { 48 this (new Uri(url)); 49 } 50 51 /*********************************************************************** 52 53 Create a client with the provided Uri instance. The Uri should 54 be fully qualified with an "http:" or "https:" scheme, or an 55 explicit port should be provided. 56 57 ***********************************************************************/ 58 59 this (Uri uri) 60 { 61 super (HttpClient.Get, uri); 62 63 // enable header duplication 64 getResponseHeaders().retain (true); 65 } 66 67 /*********************************************************************** 68 69 ***********************************************************************/ 70 71 void[] read () 72 { 73 auto buffer = super.open(); 74 try { 75 if (super.isResponseOK()) 76 buffer.load (getResponseHeaders().getInt(HttpHeader.ContentLength)); 77 } finally {super.close();} 78 return buffer.slice(); 79 } 80 } 81 82 83 /******************************************************************************* 84 85 *******************************************************************************/ 86 87 debug (HttpGet) 88 { 89 import tango.io.Console; 90 91 void main() 92 { 93 // open a web-page for reading (see HttpPost for writing) 94 auto page = new HttpGet ("http://www.digitalmars.com/d/intro.html"); 95 96 // retrieve and flush display content 97 Cout (cast(char[]) page.read) (); 98 99 foreach (header; page.getResponseHeaders) 100 Cout (header.name.value) (header.value).newline; 101 } 102 }