【问题标题】:Authorization failure TIdHTTP over HTTPS when password is russian密码为俄语时,通过 HTTPS 的 TIdHTTP 授权失败
【发布时间】:2015-03-23 07:31:44
【问题描述】:

我尝试通过以下代码使用 TIdHTTP(Indy 10.6.0 和 Delphi XE5)测试我的 web 服务:

GIdDefaultTextEncoding := encUTF8;
HTTP.IOHandler.DefStringEncoding := IndyTextEncoding_UTF8;
Http.Request.UserName := AUser;
Http.Request.Password := APass;
Http.Request.Accept := 'text/javascript';
Http.Request.ContentType := 'application/json';
Http.Request.ContentEncoding := 'utf-8';
Http.Request.URL := 'https://sameService';
Http.MaxAuthRetries := 1;
Http.Request.BasicAuthentication := True;
TIdSSLIOHandlerSocketOpenSSL(HTTP.IOHandler).SSLOptions.Method := sslvSSLv3;
HTTP.HandleRedirects := True;

UTF-8 格式的“AUser”和“APass”。当“APass”具有相同的俄语字符时,我无法登录。 通过“HTTP 分析”我看到:

...
Authorization: Basic cDh1c2VyOj8/Pz8/PzEyMw==

从 Base 64 解码(base64decode.org)我们可以看到:

p8user:??????123

为什么 DefStringEncoding 不起作用?

【问题讨论】:

    标签: delphi encoding https indy delphi-xe5


    【解决方案1】:

    TIdHTTP 的认证系统没有TIdIOHandler 或其DefStringEncoding 属性的概念。

    在内部,TIdBasicAuthentication 使用 TIdEncoderMIME.Encode(),但没有指定任何编码。 TIdEncoder.Encode() 默认为 8 位编码,因此不受 GIdDefaultTextEncoding 影响。

    如果您需要发送带有BASIC 身份验证的UTF-8 编码密码,您必须手动编码UTF-8 数据并将生成的八位字节存储到string,然后8 位编码器可以处理八位字节照原样,例如:

    Http.Request.Password := BytesToStringRaw(IndyTextEncoding_UTF8.GetBytes(APass));
    

    另一方面,Indy 的DIGEST 身份验证,例如使用TIdHashMessageDigest5.HashStringAsHex(),而TIdHash.HashString() 不默认使用任何特定编码,它依赖于GIdDefaultTextEncoding

    因此,您必须根据您使用的身份验证方式对密码进行编码。为了解决这种差异,您可以尝试不编码TIdHTTP.Request.Password 本身,而是在TIdHTTP.OnAuthorization 事件中编码密码,而不是在使用BASIC 身份验证时:

    Http.Request.Password := APass;
    ...
    procedure TMyForm.HttpAuthorization(Sender: TObject; Authentication: TIdAuthentication; var Handled: Boolean);
    var
      B: TIdBytes;
    begin
      if Authentication is TIdBasicAuthentication then
      begin
        Authentication.Password := BytesToStringRaw(IndyTextEncoding_UTF8.GetBytes(TheDeisredPasswordHere));
        Handled := True;
      end;
    end;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-29
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 2018-11-18
      • 2017-07-16
      • 1970-01-01
      相关资源
      最近更新 更多