![]() |
TIdHTTPServer umd Umlaute
Für eine kleine Web-App habe ich mir eine Mini-Webserver geschrieben.
Dieser Webserver macht im Grund nichts anderes, als bei Anfrage den Content aus verschiedenen Dateien zu laden, die quasi als Templates vorliegen, bestimmte Textpassagen gegen dynamische Inforamtionsinhalte auszutauschen und zurück zum Cient zu senden. Das Ganze sie so aus:
Delphi-Quellcode:
Header.html:
const
HeaderFile = 'header.html'; Zaehler = 'zaehler.html'; implementation procedure AddTemplateFile(const Filename: string; var DestinationString: string); var f: TextFile; s: string; begin if FileExists(ExtractFilePath(FExefile)+Filename) then begin AssignFile(f, ExtractFilePath(FExefile)+Filename); Reset(f); try while not Eof(f) do begin Readln(f, s); DestinationString:=DestinationString+s; end; finally CloseFile(f); end; end; end; procedure GenerateZaehlerPage( var AResponseInfo: TIdHTTPResponseInfo; MaGUID, Username: string); begin s:=''; AddTemplateFile(HeaderFile, s); AddTemplateFile(Zaehler, s); s:=StringReplace(s, '==username==', Username, [rfReplaceAll]); AResponseInfo.ContentText:=s; AResponseInfo.ResponseNo:=200; end; procedure idhtpsrvr1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); begin AContext.Connection.IOHandler.DefStringEncoding := IndyTextEncoding_UTF8; AResponseInfo.CharSet:='utf-8'; if (ARequestInfo.Document='/zaehler.php') then begin GenerateZaehlerPage(AResponseInfo, ARequestInfo.Session.Content.Values['guid'], ARequestInfo.Session.Content.Values['realname']); Exit; end; end;
Code:
Zaehler.html:
<!DOCTYPE html>
<html lang="de"> <head> <title>Dummy-App</title> <meta charset="utf-8"> <meta name="apple-mobile-web-app-title" content="TNM Personal" /> <meta name="application-name" content="TMN Personal"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8; Content-Transfer-Encoding: quoted-printable" /> <meta name="format-detection" content="telephone=no" /> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> <meta name="mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-status-bar-style" content="black" /> <meta http-equiv="cache-control" content="max-age=0" /> <meta http-equiv="cache-control" content="no-cache" /> <meta http-equiv="expires" content="0" /> <meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" /> <meta http-equiv="pragma" content="no-cache" /> <script src="https://code.jquery.com/jquery-3.4.1.js"></script> <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css"> </head>
Code:
Ich habe das mal soweit zusammen gekürzt, damit klar wird, wie ich das mache.
<body>
<form data-ajax="false" id="menu" method="post" action="Menu.php"><input type="submit" value="<< Menu" style="width:30%; height: 40px; font-size:10pt;font-weight:60;padding: 0px 0px"></form> <div data-role="page" id="menu" data-theme="b"> <div data-role="main" class="ui-content"> <h1>==username==</h1> <h1>Zähler</h1> </div> </div> </body> </html> Soweit funktioniert auch alles. Mein Problem sind die Umlaute z.B. im Wort "Zähler". Dieses wird im Browser als "Zähler" dargestellt. Kann mir da jemand weiterhelfen? Rufe ich die html-Dateien aus dem Browser auf, so wird "Zähler" korrekt dargestellt. Wird es von IdHttpServer gesendet, dann nicht. Also muss ich vor dem Senden den Content noch irgendwie umwandelt? |
AW: TIdHTTPServer umd Umlaute
.. sollte man in html die Umlaute nicht kodieren?
ä -> ä siehe auch hier: ![]() Grüße Klaus |
AW: TIdHTTPServer umd Umlaute
Zitat:
|
AW: TIdHTTPServer umd Umlaute
Liste der Anhänge anzeigen (Anzahl: 1)
Wenn man wie versprochen UTF8 liefert, dann muss man auch nichts kodieren.
Aber was passiert, wenn man das Versprechen nicht hält? Dann kommt genau so ein Murks dabei heraus. Im Anhang eine html Datei, mit nicht kodierten Umlauten, die aber trotzdem richtig angezeigt wird. |
AW: TIdHTTPServer umd Umlaute
Zitat:
|
AW: TIdHTTPServer umd Umlaute
Und hier ein bisserl Quelltext
Delphi-Quellcode:
Die Dateien (die ich hier zum Testen verwende)
type
TForm1 = class(TForm) IdHTTPServer1: TIdHTTPServer; CheckBox1: TCheckBox; ActionList1: TActionList; Action1: TAction; procedure Action1Execute(Sender: TObject); procedure Action1Update(Sender: TObject); procedure IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); private { Private-Deklarationen } procedure AddTemplateFile(const AFilename: string; var s: string); public { Public-Deklarationen } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Action1Execute(Sender: TObject); begin IdHTTPServer1.Active := not IdHTTPServer1.Active; end; procedure TForm1.Action1Update(Sender: TObject); begin TAction(Sender).Checked := IdHTTPServer1.Active; end; procedure TForm1.AddTemplateFile(const AFilename: string; var s: string); begin // hier wird es spannend // die Dateien sind UTF8-kodiert, also geben wir das mit an s := s + TFile.ReadAllText(AFilename,TEncoding.UTF8); // Wenn man das so macht, dann gibt es eine komische Anzeige //s := s + TFile.ReadAllText(AFilename); end; procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); var s: string; begin s := ''; AddTemplateFile('header.txt', s); AddTemplateFile('zaehler.txt', s); AResponseInfo.ContentType := 'text/html'; AResponseInfo.ContentEncoding := 'utf-8'; AResponseInfo.ContentText := s; end;
Delphi-Quellcode:
und
header.txt
Delphi-Quellcode:
auf dem Datenträger sind - bei mir - UTF8 kodiert und genau das muss ich beim Einlesen der Dateien berücksichtigen.
zaehler.txt
Das was man an
Delphi-Quellcode:
übergibt wird von Indy anhand des
AResponseInfo.ContentText
Delphi-Quellcode:
entsprechend umgewandelt, aber dafür muss man auch etwas Vernünftiges übergeben.
AResponseInfo.ContentEncoding
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:09 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz