Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Netzwerke (https://www.delphipraxis.net/14-netzwerke/)
-   -   Javascript zum Download? (https://www.delphipraxis.net/213841-javascript-zum-download.html)

Stewag 4. Okt 2023 15:04

Javascript zum Download?
 
Eine csv-Datei der Bundesbank, die ich jahrelang über eine einfache WinInet Funktion abrufen konnte, liefert nun dies:

"Bitte aktivieren sie Javascript um ihren Browser zu validieren. You need to enable Cookies to verify your Browser."

Ich habe es daraufhin mit idHTTP, TIdSSLIOHandlerSocketOpenSSL mit den beiden SSL libraries und TIdCookieManager probiert, mit demselben Ergebnis.

Das Problem scheint das Javascript zu sein, dass "den Browser validiert".
Mit Chrome kann ich die Texdatei problemlos herunterladen.

Hat jemand eine Idee? Muss ich tatsächlich eine Javascript Engine einbinden?
Wie geht das?

Hier die Adresse:
https://www.bundesbank.de/statistic-....EUR.BB.AC.A02
(Es kommt eine Fehlermeldung, aber der csv Datei wird trotzdem heruntergeladen)

mytbo 4. Okt 2023 15:39

AW: Javascript zum Download?
 
Zitat:

Zitat von Stewag (Beitrag 1527705)
Hat jemand eine Idee? Muss ich tatsächlich eine Javascript Engine einbinden?

Mit mORMot geht es:
Delphi-Quellcode:
uses
  mormot.core.base,
  mormot.core.text,
  mormot.core.os,
  mormot.net.client;

begin
  var url: RawUtf8 := 'https://www.bundesbank.de/statistic-rmi/StatisticDownload?tsId=BBEX3.M.JPY.EUR.BB.AC.A02';
  var content: RawByteString := HttpGet(url);
  if content <> '' then
    FileFromString(content, MakePath([Executable.ProgramFilePath, 'daten.dat']));
Bis bald...
Thomas

Kas Ob. 4. Okt 2023 16:33

AW: Javascript zum Download?
 
I am not witnessing anything like that !

Performing cURL
Code:
curl -v https://www.bundesbank.de/statistic-rmi/StatisticDownload?tsId=BBEX3.M.JPY.EUR.BB.AC.A02
The result
Code:
* timeout on name lookup is not supported
* timeout on name lookup is not supported
* Hostname was NOT found in DNS cache
*   Trying 185.173.228.8...
* Connected to www.bundesbank.de (185.173.228.8) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: D:\Program Files\OpenSSL-Win64\bin\curl-ca-bundle.crt
  CApath: none
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-ECDSA-AES256-GCM-SHA384
* Server certificate:
*        subject: C=DE; O=Deutsche Bundesbank; ST=Hessen; L=Frankfurt am Main; CN=bundesbank.de
*        start date: 2022-11-08 08:32:52 GMT
*        expire date: 2023-11-12 23:59:59 GMT
*        subjectAltName: www.bundesbank.de matched
*        issuer: C=DE; O=T-Systems International GmbH; OU=T-Systems Trust Center; ST=Nordrhein Westfalen; postalCode=57250; L=Netphen; street=Untere Industriestr. 20; CN=TeleSec ServerPass Class 2 CA
*        SSL certificate verify ok.
> GET /statistic-rmi/StatisticDownload?tsId=BBEX3.M.JPY.EUR.BB.AC.A02 HTTP/1.1
> User-Agent: curl/7.39.0
> Host: www.bundesbank.de
> Accept: */*
>
< HTTP/1.1 200 OK
< date: Wed, 04 Oct 2023 15:19:22 GMT
< content-type: text/csv
< set-cookie: INGRESSCOOKIE_statapp=1696432763.756.39.843280|7175a3d964339b4fb3d12ae49777d122; Path=/statistic-rmi; HttpOnly; Secure
< content-disposition: attachment; fileName=BBEX3.M.JPY.EUR.BB.AC.A02.csv
< cache-control: no-cache, no-store, max-age=0, must-revalidate
< pragma: no-cache
< expires: 0
< content-security-policy: default-src 'none';
< referrer-policy: same-origin
< set-cookie: SERVERID=ccb0110cb65faf6cfbfe4a89b6ff27d38eb2a14e; path=/; HttpOnly; Secure
< x-varnish: 47143146
< age: 0
< accept-ranges: bytes
< via: 1.1 Varnish
< content-length: 5228
< x-frame-options: SAMEORIGIN
< x-xss-protection: 1; mode=block
< x-content-type-options: nosniff
< x-permitted-cross-domain-policies: none
< strict-transport-security: max-age=31536000
<
&#1103;&#9559;&#9488;"";BBEX3.M.JPY.EUR.BB.AC.A02;BBEX3.M.JPY.EUR.BB.AC.A02_FLAGS
"";Euro-Referenzkurs der EZB / 1 EUR = ... JPY / Japan;
Einheit;JPY;
Dimension;Eins;
Stand vom;29.09.2023 16:12:08 Uhr;
1999-01;131,35;
1999-02;130,78;
1999-03;130,20;
1999-04;128,16;
1999-05;129,71;
1999-06;125,32;
1999-07;123,71;
1999-08;120,10;
1999-09;112,39;
1999-10;113,52;
1999-11;108,25;
.......
The request headers are minimal and does work and enough, no cookies, no hidden parameters and for sure no java script code manipulation or hashing or what so ever.
Code:
GET /statistic-rmi/StatisticDownload?tsId=BBEX3.M.JPY.EUR.BB.AC.A02 HTTP/1.1
User-Agent: curl/7.39.0
Host: www.bundesbank.de
Accept: */*
So you have to check HTTPS is used not falling back to HTTP, also check if the connection is not violating the security according to the bank site, which is very unlikely, but make sure the User-Agent is filled and not empty or blacklisted by the bank site server, in all cases compare check your request headers.
To check the header perform similar request to your own server, any web server will do and should be able to capture the headers, if you don't have a web server then use any web server example form any where you can put your hand on.

Stewag 4. Okt 2023 19:36

AW: Javascript zum Download?
 
Thank you Kas Ob. - Curl solved my problem!

Danke auch an Thomas! Curl einzubinden erschien mir in diesem Fall als die einfachere Lösung.

jaenicke 4. Okt 2023 21:13

AW: Javascript zum Download?
 
Das hat nichts mit Curl oder Indy zu tun, denn das kann der Server gar nicht mitbekommen. Der Unterschied liegt in der Anfrage. Vermutlich ist schlicht der Useragent ein anderer, der ja auch schon erwähnt wurde...

himitsu 4. Okt 2023 22:35

AW: Javascript zum Download?
 
Delphi-Quellcode:
uses Winapi.UrlMon;
if URLDownloadToFile(nil, 'deine URL', 'der Dateiname', 0, nil) <> S_OK then
  raise EProgrammerNotFound.Create('Peng');
Delphi-Quellcode:
uses Vcl.ExtActns;
var DownLoadURL: TDownLoadURL;
...
DownLoadURL.URL := ...;
DownLoadURL.Filename := ...;
if not DownLoadURL.Execute then
  raise EProgrammerNotFound.Create('Peng');


Aber so, wie die Fehlermeldung klingt, müssten es zwei Abfragen werden.
z.B. einmal die Seite aufrufen, auf welcher der Download verlinkt wird, das soll wohl ein Cookie bei dir speichern und dann die zweite Abfrage, inkl. des Cookie, um die Datei runterzuladen.
Aber natürlich kann diese Fehlermeldung absoluter Bullshit sein und es ist am Ende ganz einfach.

Wie mehrmals gesagt, kannst du einfach mal mit alt und neu eine Abfrage machen, schaust dir den Unterschied an und behebst Jenen.


Manche Server sperren z.B. gewisse "standard"-Dinge, wie z.B. einige Useragent-Strings,
weil andere Programme, die auch das Gleiche ausgeben, zu oft Mist gebaut wurde.

z.B. illegal automatisiert Daten abgefragt, obwohl es vielleicht nicht erlaubt war.

Außerdem gibt so manche API / Webserver gern auf, wenn man nicht den gewünschten, bzw. einen der unterstürtzen Typen angibt. (Accept)

Klaus01 5. Okt 2023 06:40

AW: Javascript zum Download?
 
.. dies hier läd auch die csv Datei ohne Probleme:
Delphi-Quellcode:
program ssltest;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, system.Classes, idhttp, IdSSLOpenSSL;

const
  url = 'https://www.bundesbank.de/statistic-rmi/StatisticDownload?tsId=BBEX3.M.JPY.EUR.BB.AC.A02';
var
  httpClient: TIDHttp;
  ioHandler: TIdSSLIOHandlerSocketOpenSSL;
  sResponse: TStringStream;
begin
  try
    httpClient := TIdHttp.Create(nil);
    try
      ioHandler := TIdSSLIOHandlerSocketOpenSSL.Create(httpClient);
      ioHandler.SSLOptions.Method := sslvTLSv1_2;
      ioHandler.SSLOptions.SSLVersions := [sslvTLSv1_2];
      httpClient.IOHandler := ioHandler;
      sResponse := TStringStream.Create;
      try
        httpClient.Get(url, sResponse);
        case httpClient.ResponseCode of
          200: sResponse.SaveToFile('c:\tmp\bb.csv');
        end;
      finally
        sResponse.Free;
      end;
    finally
      httpClient.Free;
    end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
Delphi 11.3

Grüße
Klaus

Bbommel 5. Okt 2023 08:02

AW: Javascript zum Download?
 
Und noch eine Lösung mit Delphi-Bordmitteln, also ohne curl und ohne Mormot - und auch ohne Indy, was es ggf. mit https etwas leichter macht:

Delphi-Quellcode:
program getPage;

{$APPTYPE CONSOLE}

uses
  System.SysUtils, System.Net.HttpClient;

var
  myClient: THTTPClient;
  response: IHTTPResponse;
  success: boolean;
  address: string;

begin
  try
    { TODO -oUser -cConsole Main : Code hier einfügen }
    success:=true;
    address:='https://www.bundesbank.de/statistic-rmi/StatisticDownload?tsId=BBEX3.M.JPY.EUR.BB.AC.A02';

    myClient:=THTTPClient.Create;
    try
      response:=myClient.Get(address);
    except
      writeln('Request failed');
      success:=false;
    end;

    if success and (response<>nil) then begin
      Writeln('Success. Displaying content.');
      Writeln(response.ContentAsString);
    end;
    myClient.Free;
    readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Stewag 5. Okt 2023 10:12

AW: Javascript zum Download?
 
Danke an alle - ihr seid großartig!!! :-D

Die Lösung von Klaus funktioniert auch unter dem älteren XE2, das ich aus Kompatibilitätsgründen zu einer älteren Komponente hierfür noch verwenden muss.

Bbommels Lösung erfordert ein neueres Delphi und läuft prima unter Delphi 11.

Union 5. Okt 2023 21:17

AW: Javascript zum Download?
 
Hast Du mal die Versionen der OpenSSL Dlls geprüft, die Du beim IOHandler lädst?


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:51 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz