我找不到激活 Basic Auth on 的選項THTTPClient。
我的主要“問題”是第一個帶有 a 的請求回應,HTTP/1.1 401 Unauthorized它觸發了一個 auth 事件。我想在第一次“嘗試”時做到這一點。
這是我的代碼:
uses
System.Net.HTTPClient, System.Net.URLClient, System.NetEncoding;
procedure DoRequest(const url, username, password, filename: string);
var
http: THTTPClient;
ss: TStringStream;
base64: TBase64Encoding;
begin
http := THTTPClient.Create;
ss := TStringStream.Create('', TEncoding.UTF8);
base64 := TBase64Encoding.Create(0);
try
// option #1 - build in credential storage
http.CredentialsStorage.AddCredential(
TCredentialsStorage.TCredential.Create(
TAuthTargetType.Server, '', url, username, password
));
// option #2 - custom "Authorization"
http.CustomHeaders['Authorization'] := 'Basic ' base64.Encode(username ':' password);
http.ProxySettings.Create('127.0.0.1', 8888); // for fiddler
http.Get(url, ss);
ss.SaveToFile(filename);
finally
base64.Free;
ss.Free;
http.Free;
end;
end;
選項 #2 在第一個請求中包含授權。選項#1 沒有。

我怎樣才能做到這一點?
更新 - 西雅圖不存在 PreemptiveAuthentication

uj5u.com熱心網友回復:
在 Delphi 11 及更高版本中,使用THTTPClient.CredentialsStorage應該可以作業,您只需設定THTTPClient.PreemptiveAuthentication為 true:
屬性控制搶先式身份驗證。設定為 True 時,將在服務器給出未經授權的回應之前提供基本身份驗證。
但是,PreemptiveAuthentication在包括西雅圖在內的 Delphi 10.x 中不存在,因此您必須堅持使用您的CustomHeaders解決方案,直到您可以升級到現代 Delphi 版本。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/477380.html
標籤:德尔福 delphi-10-西雅图
