Delphi XE8 idHttp错误消息文本编码错误

Delphi XE8 idHttp错误消息文本编码错误,delphi,text,put,idhttp,Delphi,Text,Put,Idhttp,我正在使用Delphi XE8,并通过idHttp发送PUT消息 Http.Request.CustomHeaders.Clear; Http.Request.BasicAuthentication := false; http.Request.Method := 'PUT'; Http.Request.Accept := '*/*'; Http.Request.ContentType := 'application/json'; http.Request.CustomH

我正在使用Delphi XE8,并通过idHttp发送PUT消息

  Http.Request.CustomHeaders.Clear;
  Http.Request.BasicAuthentication := false;
  http.Request.Method := 'PUT';
  Http.Request.Accept := '*/*';
  Http.Request.ContentType := 'application/json';
  http.Request.CustomHeaders.AddValue('apiKey','T_API23207_169');
  http.Request.CustomHeaders.AddValue('transactionId','20200924_015');
  http.Request.CustomHeaders.AddValue('usziID','1');
  Http.Request.AcceptEncoding := '*';
  http.Request.CharSet := 'utf-8';

  kuldes_header.Text := http.Request.CustomHeaders.Text;

  http.Intercept := IdLogEvent1;
  IdLogEvent1.Active := true;

  jsonToSend := TStringStream.create(json_adat.Text,system.sysUtils.TEncoding.UTF8);

  kuldes_body.Lines.LoadFromStream(jsonToSend);

  try
    try
        send_text := http.Put('http://10.109.132.24:8090/rest/usziIroda/1',jsonToSend);
        resp := http.ResponseText;
        code := http.ResponseCode;
        jsonToSend.Position := 0;
    except
        on E: EIdHTTPProtocolException do
          begin
          code := e.ErrorCode;
          error_message := e.ErrorMessage;
          end;
    end;

    hiba_kod.Lines.Add(IntToStr(code));
    valasz_uzenet.Text := send_text;
    hiba_uzenet.Text := error_message;   enter code here
返回的错误消息包含奇怪的字符: “Megadott tranzakcióazonosÃtÃval mÃr tÃtÃtÃtÃtÃtÃtÃtÃval mÃrÃtÃtÃtÃtÃtÃ

但应该是这样的: “Megadott tranzakcióazonosítóval már történt API hívás”

如何将返回的消息转换为普通字符串


谢谢大家!

您显示的结果-
Megadott tranzakciÃazonosÃtÃval mÃr tÃrtÃnt API hÃvÃs
-是被拉丁语1/ISO-8859-1误解的
Megadott tranzakcióazonosÃval már történt API hívás的UTF-8编码形式。这很可能意味着响应没有在其
内容类型
标题中指定UTF-8字符集(因为您分配了
截取
,您可以自己轻松验证),因此Indy将返回默认字符集

在访问
send\u text
error\u message
中的响应数据之前,原始UTF-8字节已解码并丢失。但是,由于ISO-8859-1基本上在字节值和Unicode码点值之间有1:1的关系,因此在这种特定情况下,您可以尝试将
ErrorMessage
Char
值复制到
RawByteString(65001)
UTF8String
,然后让RTL将其解码为UTF-8,返回到正确的UTF-16
(Unicode)字符串中,例如:

函数DecodeISO88591AsUTF8(常数S:string):string;
变量
utf8:utf8字符串;
I:整数;
开始
设置长度(utf8,长度(S));
对于I:=低至高do
utf8[I]:=AnsiChar(S[I]);
结果:=字符串(utf8);
结束;
...
错误消息:=e.ErrorMessage;
//如果不是textisame(http.Response.CharSet,'utf-8'),那么
如果textisame(http.Response.CharSet,'ISO-8859-1'),则
错误消息:=解码ISO88591ASUTF8(错误消息);
或者,您可以调用重载版本的
TIdHTTP.Put()
,该版本填充响应
TStream
,而不是返回解码的
字符串
,然后您可以根据需要解码原始字节。只需确保在
TIdHTTP.HTTPOptions
属性中启用
hoNoProtocolErrorException
hoWantProtocolErrorContent
标志,以便任何错误响应都存储在
TStream
中,那么您就不需要
try/except
来单独处理
eidttprotocolexception

http.HTTPOptions:=http.HTTPOptions+[hoNoProtocolErrorException,hoWantProtocolErrorContent];
...
RespStrm:=TMemoryStream.Create;
尝试
http.Put('http://10.109.132.24:8090/rest/usziIroda/1",jsonToSend,RespStrm),;
resp:=http.ResponseText;
代码:=http.ResponseCode;
jsonToSend.Position:=0;
响应位置:=0;
如果(代码div 100)=2,则
开始
发送文本:=根据需要解码响应。。。;
结束其他
开始
错误消息:=根据需要解码响应。。。;
结束;
最后
免费响应;
结束;
...

如果将
Http.Request.AcceptEncoding:='*'
改为
'utf-8'
,问题是否得到解决?