Basic authentication with HTTPRIO in Delphi 10.3

空扰寡人 提交于 2021-02-05 07:11:08

问题


There has been a change to the HTTPRIO.HTTPWebNode.OnBeforePost event in Delphi 10.3.

Before Delphi 10.3, the event handler was defined this way, and it worked perfectly:

procedure TForm1.HTTPRIO1HTTPWebNode1BeforePost(const HTTPReqResp: THTTPReqResp;
  Data: Pointer);
var
  auth: String;
begin
  auth := 'Authorization: Basic ' + IdEncoderMIME1.EncodeString('user:password');
  HttpAddRequestHeaders(Data, PChar(auth), Length(auth), HTTP_ADDREQ_FLAG_ADD);
end;

In Delphi 10.3, the Data parameter is gone, instead a THTTPClient is given, and I have no idea how to implement Basic authentication with it:

procedure TForm1.HTTPRIO1HTTPWebNode1BeforePost(const HTTPReqResp: THTTPReqResp;
  Client: THTTPClient);
var
  auth: String;
begin
  auth := 'Authorization: Basic ' + IdEncoderMIME1.EncodeString('user:password');
  ???
end;

Any hints?


回答1:


Try using the request's Username and Password properties, eg:

HTTPReqResp.UserName := 'user';
HTTPReqResp.Password := 'password';

If that does not work, try using the client's CustomHeaders property instead, eg:

Client.CustomHeaders['Authorization'] := 'Basic ' + IdEncoderMIME1.EncodeString('user:password');


来源:https://stackoverflow.com/questions/54833530/basic-authentication-with-httprio-in-delphi-10-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!