http HTTPReadString
Return to Introduction  Previous page 
Unit
WinHTTP  

Declaration
function HTTPReadString(const URL: String; Timeout: Integer = 0): String;  

Description
The HTTPReadString function provides extremely simple way to receive some data from the Web by HTTP protocol, without using WinHTTP component and specifying its properties and handling the events. You just need to specify the URL (and optionally Timeout) and function will return downloaded data (or empty string, if remote host are unreachable or connection failed).  
 
Return value is the downloaded string, or empty string, which means that download failed for some reason.  
 
Parameters  
URLspecifies the URL of document which you wish to download;  
Timeoutoptional parameter, which specifies the time-out for downloading (in milliseconds). If a connection request takes longer than this time-out value, the request is cancele and function returns empty string (which means that download failed). Zero timeout (0) means infinite, thus the function will try to download the data without any forced interrupts.  
 
iiinfo Remarks
1.Don't forget to add "WinHTTP" into uses clause of your unit before using this function.  
2.When you call this function, the execution of procedure from which the call are made, will suspended for some time, until the HTTP request will be complete or failed (it looks just like if you'd used WinHTTP component with WaitThread property set to True).  

Example
var  
  Config: String;  
begin  
  Config := HTTPReadString('www.yourdomain.com/some_configuration.ini');  
  if Config <> '' then  
   begin  
    // deal with downloaded string  
   end;  
 
 
  // ANOTHER EXAMPLE: read the same config file with 10 seconds timeout  
  Config := HTTPReadString('www.yourdomain.com/some_configuration.ini', 10000);  
  if Config <> '' then  
   begin  
    // deal with downloaded string  
   end;  
end;  

See also
Timeouts and WaitThread properties of WinHTTP component.