我一直在嘗試使用Delphi 中的NetHTTPRequest&NetHTTPClient組件來發出基本的身份驗證請求,但是我一直在使用該NetHTTPRequest.Get方法時遇到問題...
基本上,該方法需要三個引數,即URL(String)記憶體流和型別的標頭TNetHeader。
我不知道為什么,但是當我嘗試傳遞我新創建的標題時,我收到標題中提到的錯誤,即
Incompatible types: 'System.TArray<System.Net.URLClient.TNameValuePair>' and 'TNameValuePair'
我不知道我是否必須投射,如果必須,將它投射到什么?
當然,如果我不傳遞基本身份驗證標頭,服務器只會回傳 401,因為它看不到任何要解碼的憑據或標頭...
這是代碼:
procedure TForm1.Button1Click(Sender: TObject);
var
HTTPResponseString: String;
begin
if IsLoggedIn = False then
begin
if Edit2.Text = '' then
begin
ShowMessage('Password cannot be empty');
exit;
end;
BasicAuth := username ':' Edit2.Text; // '£';
AuthentificationPacket := EncodeBase64(UTF8Bytes(utf8string(BasicAuth)));
HTTPHeader := TNetHeader.Create('Authorization: Basic', AuthentificationPacket);
try
HTTPResponseLogin := NetHTTPRequest1.Get(HTTPLoginRequest, nil, HTTPHeader);
ShowMessage(HTTPResponselogin.ContentAsString());
IsLoggedIn := True;
except
on E: Exception do
ShowMessage(E.Message);
end;
Button1.Text := 'LOG OFF';
end
else if IsLoggedIn then
begin
Edit1.Text := '';
Edit2.Text := '';
username := '';
password := '';
tagid := '';
Button1.Enabled := False;
Button1.Text := 'LOG IN';
IsLoggedIn := False;
Timer1.Enabled := False;
IdleTimer := 0;
Application.OnIdle := nil;
end;
end;
有問題的線路當然是 NetHTTPRequest1.Get(HTTPLoginRequest, nil, HTTPHeader);
哪里HTTPHeader被標記為不兼容...
任何幫助表示贊賞!
uj5u.com熱心網友回復:
看看宣告的TNetHTTPRequest.Get()更仔細:
function Get(const AURL: string; const AResponseContent: TStream = nil; const AHeaders: TNetHeaders = nil): IHTTPResponse;
第三個引數采用TNetHeaders(復數),但您傳遞給它的是TNetHeader(單數)。它需要一個名稱/值對陣列。
此外,Basic不是Authorization標題名稱的一部分,而是其值的一部分。
嘗試更像這樣的事情:
procedure TForm1.Button1Click(Sender: TObject);
var
BasicAuth, AuthentificationPacket: string;
HTTPHeader: TNetHeader;
begin
if not IsLoggedIn then
begin
if Edit2.Text = '' then
begin
ShowMessage('Password cannot be empty');
Exit;
end;
BasicAuth := username ':' Edit2.Text; // '£';
AuthentificationPacket := 'Basic ' EncodeBase64(UTF8Bytes(UTF8String(BasicAuth)));
HTTPHeader := TNetHeader.Create('Authorization', AuthentificationPacket);
try
ShowMessage(NetHTTPRequest1.Get(HTTPLoginRequest, nil, [HTTPHeader]).ContentAsString());
except
on E: Exception do begin
ShowMessage(E.Message);
Exit;
end;
end;
IsLoggedIn := True;
Button1.Text := 'LOG OFF';
end
else
begin
Edit1.Text := '';
Edit2.Text := '';
username := '';
password := '';
tagid := '';
Button1.Enabled := False;
Button1.Text := 'LOG IN';
IsLoggedIn := False;
Timer1.Enabled := False;
IdleTimer := 0;
Application.OnIdle := nil;
end;
end;
或者,您可以使用TNetHTTPRequest.CustomHeaders屬性而不是AHeaders引數:
procedure TForm1.Button1Click(Sender: TObject);
var
BasicAuth: string;
begin
if not IsLoggedIn then
begin
if Edit2.Text = '' then
begin
ShowMessage('Password cannot be empty');
Exit;
end;
BasicAuth := username ':' Edit2.Text; // '£';
NetHTTPRequest1.CustomHeaders['Authorization'] := 'Basic ' EncodeBase64(UTF8Bytes(UTF8String(BasicAuth));
try
ShowMessage(NetHTTPRequest1.Get(HTTPLoginRequest).ContentAsString());
except
on E: Exception do begin
ShowMessage(E.Message);
Exit;
end;
end;
IsLoggedIn := True;
Button1.Text := 'LOG OFF';
end
else
begin
Edit1.Text := '';
Edit2.Text := '';
username := '';
password := '';
tagid := '';
Button1.Enabled := False;
Button1.Text := 'LOG IN';
IsLoggedIn := False;
Timer1.Enabled := False;
IdleTimer := 0;
Application.OnIdle := nil;
end;
end;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/400667.html
