问题
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