目标多字节代码页

时间:2016-11-08 09:58:39

标签: delphi utf-8 urlencode delphi-10.1-berlin webbroker

我用Delphi 2007制作肥皂,它有效!

在Delphi 10.1柏林转换后,我有很多例外:

  

目标多字节代码页中不存在Unicode字符的映射

原因似乎是,当webbroker HttpApp解析请求参数时,如果请求在查询字符串中有一些url编码的字符,则会引发此异常,例如:

http://localhost/soap/soap.dll/action?param=%E0

实际上如果我使用URL.Decode()%E0 url encoded)致电à

TNetEncoding.URL.Decode('%E0');

它提出了同样的例外:

  

目标多字节代码页中不存在Unicode字符的映射

问题似乎是方法System.NetEncoding上的单位TURLEncoding.DoDecode(const Input: string): string。此方法尝试仅在UTF-8中转换url编码的字符,而不在Windows-1252上进行任何回退。字符串%E0à的Windows-1252编码,但delphi仅转换UTF-8版本:%C3%A0

一个小的(不完美,不优雅)修复是添加一个后备:

try
  Result := TEncoding.UTF8.GetString(Bytes); // original Delphi 10.1 line
except
  on E: EEncodingError do Result := string(PChar(Bytes)); // fallback
end;

完整代码:

function TURLEncoding.DoDecode(const Input: string): string;

  function DecodeHexChar(const C: Char): Byte;
  begin
    case C of
       '0'..'9': Result := Ord(C) - Ord('0');
       'A'..'F': Result := Ord(C) - Ord('A') + 10;
       'a'..'f': Result := Ord(C) - Ord('a') + 10;
    else
      raise EConvertError.Create('');
    end;
  end;

  function DecodeHexPair(const C1, C2: Char): Byte; inline;
  begin
    Result := DecodeHexChar(C1) shl 4 + DecodeHexChar(C2)
  end;

var
  Sp, Cp: PChar;
  I: Integer;
  Bytes: TBytes;

begin
  SetLength(Bytes, Length(Input) * 4);
  I := 0;
  Sp := PChar(Input);
  Cp := Sp;
  try
    while Sp^ <> #0 do
    begin
      case Sp^ of
        '+':
          Bytes[I] := Byte(' ');
        '%':
          begin
            Inc(Sp);
            // Look for an escaped % (%%)
            if (Sp)^ = '%' then
              Bytes[I] := Byte('%')
            else
            begin
              // Get an encoded byte, may is a single byte (%<hex>)
              // or part of multi byte (%<hex>%<hex>...) character
              Cp := Sp;
              Inc(Sp);
              if ((Cp^ = #0) or (Sp^ = #0)) then
                raise EHTTPException.CreateFmt(sErrorDecodingURLText, [Cp - PChar(Input)]);
              Bytes[I] := DecodeHexPair(Cp^, Sp^)
            end;
          end;
      else
        // Accept single and multi byte characters
        if Ord(Sp^) < 128 then
          Bytes[I] := Byte(Sp^)
        else
          I := I + TEncoding.UTF8.GetBytes([Sp^], 0, 1, Bytes, I) - 1

      end;
      Inc(I);
      Inc(Sp);
    end;
  except
    on E: EConvertError do
      raise EConvertError.CreateFmt(sInvalidURLEncodedChar, [Char('%') + Cp^ + Sp^, Cp - PChar(Input)])
  end;
  SetLength(Bytes, I);

  // ------> MY FIX <------
  try
     Result := TEncoding.UTF8.GetString(Bytes); // Original line
  except
     on E: EEncodingError do Result := string(PChar(Bytes));
  end;
  // END FIX
end;

经过大量搜索后,我发现了Bug fix list for RAD Studio 10.1 Berlin并且它说明了这个错误:

  

Webbroker HttpApp解析请求参数并获取错误“目标多字节代码页中不存在Unicode字符的映射”

但不适合我...

2 个答案:

答案 0 :(得分:1)

尝试使用WEB.ReqMulti;

当我使用WebBroker在表单中存在多字节字符时从网页处理POST方法时,我遇到了同样的异常。

在我添加了WebBroker项目中的WEB.ReqMulti之后,这个例外就消失了。

答案 1 :(得分:0)

在东京(10.2.1)IDE中,当我尝试关闭IDE时,出现了“目标多字节代码页中不存在Unicode字符的映射”异常。 要解决:从项目目录中删除文件。$$$。