How to download/grap web pages or XML document at internet website in Delphi XE?

CK CK 3788 
Created at
Updated at  
867 0 0

If you are considering Delphi XE as cross compiler for the multiple platforms such as Android, iOS, and Windows, you need to have the standard method to get web content such as the normal text, or XML document as a result of RESTful API.

Delphi provides the straightforward, and easy way can implement it. The followings are two reference example can implement it.

 

# Using Indy's TIdHTTP

uses IdHTTP;

procedure TForm1.Button1Click(Sender: TObject);
var
  S: string;
  IdHTTP: TIdHTTP;
begin
  IdHTTP := TIdHTTP.Create(nil);
  try
    S := IdHTTP.Get('http://www.tablemaster.webs.com/versioninfo.txt');
    ShowMessage(S);
  finally
    IdHTTP.Free;
  end;
end;

The above is the easiest way can download the webpage. And the below is the another example can download that based on TIdHTTP.

uses IdHTTP;

function ReadFromWeb(url:string):string;
const
  cUSER_AGENT = 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543 Safari/419.3';
var
  IdHTTP: TIdHTTP;
  Stream: TStringStream;
begin
  Result := '';

  IdHTTP := TIdHTTP.Create(nil);
  Stream := TStringStream.Create;

  try
    IdHTTP.Request.UserAgent := cUSER_AGENT;
    try
      IdHTTP.Get(url, Stream);
      Result := Stream.DataString;
    except
      Result := '';
    end;
  finally
    Stream.Free;
    IdHTTP.Free;
  end;
end;

Feeling is like that the second example is better stable than the first one based on my test.

 

# How to read XML

This is one of the easiest way can get XML document as a result of RESTful API.

procedure TForm1.Button1Click(Sender: TObject);
var
	XMLDoc: IXMLDOMDocument;
begin
	XMLDoc := CoDOMDocument.Create;
	XMLDoc.load('MyBooks.xml');
	Memo1.Lines.Text := XMLDoc.xml;
end;
Tags C# Java SQL TidURI Facebook X
Comments 0
Similar posts
  1. How To Check If Your Printer Is Connected Using C#
    1,025
  2. How to get process id in C# like windows task manager ?
    867
  3. How to convert integer to text string ?
    696
  4. How to convert text string to integer ?
    608
  5. How to resize and save PNG or Jpeg Image file ?
    629
  6. How to convert bitmap image to jpeg image ?
    353
  7. To load image from file
    446
  8. Java Kafka on CentOS 7: Install, Configure, & Run with Examples
    422
  9. SQL to Select a random row from a database table
    535
  10. How to speed up access to the RichTextBox Lines property
    828
  11. The best way to enable and disable buttons, menu items and toolbar buttons
    666
  12. How to restrict a program to a single instance
    727
  13. Graphics in C# - Irregular Forms
    905
  14. Extracting the Country from IP Address
    464
  15. Retrieving all image links from an HTML document
    416
  16. How to print character in C# by ASCII code
    475
  17. How To Print Text In C#
    453
  18. How to send binary data through C# CGI app?
    993
  19. Calling Your Main Thread From A Worker Thread In C#
    544
  20. How To Read And Write A Cookie
    359
  21. Using C# With Cookies
    620
  22. Setting TQuery SQL Statement
    12,155
  23. Your Comprehensive Guide to Flink & Kafka for Real-time Stream Processing
    406
  24. The Ultimate Data Engineering Roadmap
    1,069
  25. Java Variant Type
    197
  1. Building a Simple DNS Server in Delphi with TTL Support
    678
  2. Run PowerShell Command as Administrator in Delphi
    652
  3. Run command with Administrator permission in Delphi on Microsoft Windows
    760
  4. Delphi code to send data to web server based on POST method
    1,027
  5. Good Delphi components
    921
  6. Delphi Program can command level execution program with hidden mode
    694
  7. How To Turn On/Off Monitor with Delphi
    844
  8. How to get application version number in Delphi
    1,547
  9. The Delphi function to get My Videos folder
    2,302
  10. Get Application Data Path on Delphi
    834
  11. I want to select directory(folder) not file. How can I do that ?
    615
  12. Changing the Title of a Print Dialog in Delphi
    1,092
  13. Auto Logon Programming in Delphi
    1,024
  14. Thread Application Implementation in TThread
    662
  15. Is there directory selection VCL component in Delphi ?
    514
  16. How to launch the external application in Delphi ?
    1,061
  17. How to print external document ?
    413
  18. Delphi API to get windows temporary directory
    571
  19. Delphi API to get the current working directory
    1,246
  20. How to resize PNG file in Delphi ?
    1,207
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    27
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    24
  3. How do I create animated background for Google Meet?
    19,708
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    217
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    85
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    153
  7. Array of arguments passed to script in PHP
    85
  8. Invalid Auction Storms in Ad Tech
    93
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    197
  10. The Two Clocks of Ad Server Architecture
    63
  11. Frequency Cap Architecture Overview
    509
  12. Collecting Proof-of-Play from 100,000 Screens
    141
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    280
  14. What is RedisVL?
    105
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    224
  16. TPU & GPU Cluster Management Tools
    232
  17. The Concept of Scaffolding in AI Agents
    221
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    575
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    252
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    326
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    197
  22. Machine Learning: Training vs. Ranking
    213
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    217
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    189
  25. Designing a Data Curation System Around a Vector Database
    215
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    229
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    649
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    185
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    207
  30. Understanding Data Lineage
    252