Merhaba,
Ben genellikle XML dosyası indirme işlemini farklı bir kod ile yapıp, alınan bilgiyi tercih ettiğim bir XML kütüphanesi ile işlemeyi tercih ediyorum.
Kullandığınız Delphi sürümünü bildirmemişsiniz. Son sürümlerden ise System.NetHttpClient ünitesi içindeki TNetHTTPClient bileşenini kullanarak XML bilgisini okuyup ardından istediğiniz şekilde işleyebilirsiniz. Birçok kişi form üzerine bileşen nesnesi ekleyerek kullanıyor. Ben kod ile işlem yapmayı tercih ediyorum.
Aşağıdaki örnek kod sertifika hatasını gözardı etmenizi sağlayacaktır. Delphi 11.3 ile test edilmiştir.
FORM
Kod: Tümünü seç
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 442
ClientWidth = 628
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = 'Segoe UI'
Font.Style = []
OnCreate = FormCreate
TextHeight = 15
object Button1: TButton
Left = 8
Top = 8
Width = 113
Height = 33
Caption = 'TCMB XML '#304'ndir'
TabOrder = 0
OnClick = Button1Click
end
object Memo1: TMemo
Left = 8
Top = 55
Width = 612
Height = 379
Lines.Strings = (
'Memo1')
ScrollBars = ssBoth
TabOrder = 1
WordWrap = False
end
end
Ünite
Kod: Tümünü seç
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
System.Net.HttpClient, // IHTTPResponse
System.Net.URLClient, // TURLRequest
System.Net.HttpClientComponent; // TNetHttpClient
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function Get(const URL: string; out Response: string; const ReadTimeout: Cardinal = 5000; const ContentType: string = ''; const Accept: string = ''): Boolean;
procedure NetHTTPClient1ValidateServerCertificate(const Sender: TObject; const ARequest: TURLRequest; const Certificate: TCertificate; var Accepted: Boolean);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
LXML: string;
begin
Memo1.Clear();
if Self.Get('https://www.tcmb.gov.tr/kurlar/202310/18102023.xml', LXML) then
begin
Memo1.Text := LXML;
end
else
begin
if LXML.IsEmpty then
Memo1.Text := 'Veriler okunamadı.'
else
Memo1.Text := LXML; // bu noktada hata detayı içerir
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.Clear();
end;
procedure TForm1.NetHTTPClient1ValidateServerCertificate(const Sender: TObject; const ARequest: TURLRequest; const Certificate: TCertificate; var Accepted: Boolean);
begin
Accepted := True;
end;
function TForm1.Get(const URL: string; out Response: string; const ReadTimeout: Cardinal = 5000; const ContentType: string = ''; const Accept: string = ''): Boolean;
var
LHttp: System.Net.HttpClientComponent.TNetHttpClient;
LResponse: System.Net.HttpClient.IHTTPResponse;
begin
LHttp := nil;
try
LHttp := TNetHttpClient.Create(nil);
LHttp.OnValidateServerCertificate := Self.NetHTTPClient1ValidateServerCertificate;
LHttp.ConnectionTimeout := ReadTimeout;
LHttp.ResponseTimeout := ReadTimeout;
if ContentType <> EmptyStr then LHttp.ContentType := ContentType;
if ContentType <> EmptyStr then LHttp.Accept := Accept;
try
LResponse := LHttp.Get(URL);
if (LResponse.StatusCode < 200) or (LResponse.StatusCode > 299) then
begin
// Error
if LResponse.ContentAsString().IsEmpty then
Response := LResponse.StatusCode.ToString() + ': ' + LResponse.StatusText
else
Response := LResponse.ContentAsString();
Exit(False);
end;
Response := LResponse.ContentAsString();
except
on E: Exception do
begin
if Assigned(LResponse) then
begin
if LResponse.ContentAsString().IsEmpty then
Response := LResponse.StatusCode.ToString() + ': ' + LResponse.StatusText
else
Response := LResponse.ContentAsString();
end
else
begin
if Response.IsEmpty then Response := E.Message;
end;
// Eat exception and return False
Exit(False);
end;
end;
finally
LHttp.Free();
end;
Result := True;
end;
end.
NOT: Sunucu kullanıyorsunuz. Yukarıdaki kod Windows Server 2019 tüm güncellemeleri yapılmış sistemde sorunsuz çalışacaktır. Önceki sürüm sunucularda Geçtiğimiz sene bu aylarda Microsoft SSL kütüphanesinde ortaya çıkan sorun düzeltilmedi ise sorun yaşamanız olası. Windows sürümü olarak Windows 10 22H2 son güncellemeleri yüklenmiş sistemlerde çalışacaktır. Aynı SSL kütüphane hatası Windows 7 ve eski Windows 10 sistemleri de etkiledi.