You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Indy HTTP Server ARequestInfo.Params文本编码错误求助

Indy HTTP Server POST请求字符编码乱码解决(Delphi 10.4.2 + Indy 10.6.2.0)

你的问题是典型的字符集解码不匹配导致的乱码:Web应用发送的UTF-8编码参数被Indy用默认的Latin1(ISO-8859-1)解码,所以出现ü、ß这类错误字符(对应正确的ü、ß)。

解决方案:强制用UTF-8解码POST参数

替换原代码中的参数解析逻辑,手动指定UTF-8字符集处理请求内容:

procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  LParams: TIdStrings;
  LContent: string;
begin
  if ARequestInfo.CommandType = hcPOST then
  begin
    // 从PostStream中读取UTF-8编码的原始内容
    LContent := TIdTextEncoding.UTF8.GetString(ARequestInfo.PostStream.Memory, ARequestInfo.PostStream.Size);
    
    // 解析URL编码的参数,指定UTF-8解码
    LParams := TStringList.Create;
    try
      TIdURI.URLDecode(LContent, LParams, TIdTextEncoding.UTF8);
      Memo1.Lines.Add(LParams.Text);
      
      // 若需将解析后的参数赋值回ARequestInfo.Params,可添加:
      // ARequestInfo.Params.Assign(LParams);
    finally
      LParams.Free;
    end;
  end;
end;

关键注意事项

  • 确认Web应用发送的POST请求参数确实是UTF-8编码的application/x-www-form-urlencoded格式
  • 如果请求的ContentType头包含charset=utf-8,Indy理论上能自动识别,但如果头信息缺失,必须手动指定UTF-8解码
  • 确保Memo1使用的字体支持德语变音字符(如Arial Unicode MS),避免因字体问题导致的显示异常

内容的提问来源于stack exchange,提问作者user1937012

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 07:42:36