Delphi Indy IdHTTPServer(IdHTTPServer) で UTF-8 を返す
Indy の HTTP Server (TIdHttpServer)で、UTF-8 をレスポンスさせる
procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var S: string;
begin
S := '<html><head></head><body>Hello World!ハローワールド!</body></html>';
// UTF-8
AResponseInfo.ContentStream := TStringStream.Create(S, TEncoding.UTF8);
AResponseInfo.ContentType := 'text/html; charset=utf-8';
{
// Shift-JIS
AResponseInfo.ContentStream := TStringStream.Create(S, TEncoding.GetEncoding(932));
AResponseInfo.ContentType := 'text/html; charset=shift-jis';
}
end;
AResponseInfo.ResponseText へそのまま記述するとデフォルトエンコードで処理されてしまうので、ContentStream へ直接投入し、ContentType へ charset でエンコードを記載しておくcharset の記述がないと、ブラウザは正しくエンコードを判断してくれない
コメント