Link to home
Start Free TrialLog in
Avatar of whpgabesz
whpgabesz

asked on

Delphi Download xml from https

Hello,

I would like to download an xml from a https site to a memo. The site is username and password protected. I am very beginner in this.

How can I do that? (if it is possible a step-by-step answer)

Thank you.
Avatar of Salih Maoui
Salih Maoui
Flag of Algeria image

try this function used WinInet for downloading. This is the code that worked for me:
function Download(URL, User, Pass, FileName: string): Boolean;
const
  BufferSize = 1024;
var
  hSession, hURL: HInternet;
  Buffer: array[1..BufferSize] of Byte;
  BufferLen: DWORD;
  F: File;
begin
   Result := False;
   hSession := InternetOpen('', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0) ;

   // Establish the secure connection
   InternetConnect (
     hSession,
     PChar(FullURL),
     INTERNET_DEFAULT_HTTPS_PORT,
     PChar(User),
     PChar(Pass),
     INTERNET_SERVICE_HTTP,
     0,
     0
   );

  try
    hURL := InternetOpenURL(hSession, PChar(URL), nil, 0, 0, 0) ;
    try
      AssignFile(f, FileName);
      Rewrite(f,1);
      try
        repeat
          InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen) ;
          BlockWrite(f, Buffer, BufferLen)
        until BufferLen = 0;
      finally
        CloseFile(f) ;
        Result := True;
      end;
    finally
      InternetCloseHandle(hURL)
    end
  finally
    InternetCloseHandle(hSession)
  end;
end;

Open in new window

Avatar of Ephraim Wangoya

The basics are to use TidHTTP and TIdSSLIOHandlerSocketOpenSSL components from Indy

You also have to download the open SSL libraries from
http://indy.fulgan.com/SSL/

For TidHTTP to use ssl, set the IOHandler property to the SSL component

IdHTTP1.IOHandler := IdSSLIOHandlerSocketOpenSSL1;

Then set the SSL version
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method := sslvSSLV3

Communicatiing with the server just involves calling Post or Get methods of IdHTTP1
The Post method will return a string which you can then examine

eg
var
  Response: string;
begin
  Response := IdHTTP1.Post('http://www.myhome.com');
end;

For what you are describing above I believe the server is expecting some kind of instructions from you in order to give you back the XML

I suggest you read up on Indy components and look at a few examples to begin with so you can get a better understanding of the issue.




Avatar of whpgabesz
whpgabesz

ASKER

maosalah: there is an error in your code : PChar(FullURL) -> PChar(URL)
Anyway it generates a very big file, without the real content.
ewangoya:

How can I install TIdSSLIOHandlerSocketOpenSSL  to delphi 7?
I downloaded the 2 dlls.

This should already be in your delphi 7, check the Indy components
hi,

I retry it and it's work fine so change the PChar(FullURL) to PChar(URL)


try that


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, WinInet;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
function Download(URL, User, Pass, FileName: string): Boolean;
const
  BufferSize = 1024;
var
  hSession, hURL: HInternet;
  Buffer: array[1..BufferSize] of Byte;
  BufferLen: DWORD;
  F: File;
begin
   Result := False;
   hSession := InternetOpen('', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0) ;

   // Establish the secure connection
   InternetConnect (
     hSession,
     PChar(URL),
     INTERNET_DEFAULT_HTTPS_PORT,
     PChar(User),
     PChar(Pass),
     INTERNET_SERVICE_HTTP,
     0,
     0
   );

  try
    hURL := InternetOpenURL(hSession, PChar(URL), nil, 0, 0, 0) ;
    try
      AssignFile(f, FileName);
      Rewrite(f,1);
      try
        repeat
          InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen) ;
          BlockWrite(f, Buffer, BufferLen)
        until BufferLen = 0;
      finally
        CloseFile(f) ;
        Result := True;
      end;
    finally
      InternetCloseHandle(hURL)
    end
  finally
    InternetCloseHandle(hSession)
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 if Download('https://xml.shavlik.com/mssecure.xml','','','c:\MyFile.xml') then
    showmessage('Download successfully ...')
 else
    showmessage('Error Url ...');

memo1.Lines.LoadFromFile('c:\MyFile.xml');
end;

end.

Open in new window

Hi!

Now your code seems to be working with your https address. But with mine it says I/O error 1784.
I think it some how it can not pass the identification. Anyway the error means the buffer is incorrect...
ASKER CERTIFIED SOLUTION
Avatar of whpgabesz
whpgabesz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Oh, If you are using Delphi 7 which come with Indy9 then you use
TIdSSLIOHandlerSocket

In Indy10 its changed to
TIdSSLIOHandlerSocketOpenSSL
I found the answer myself.