我有一些可以使用HTTP协议的设备。
连接消息必须与此类似:
GET /cgi/proc/sound?1000&1000 HTTP/1.1 Host: 10.0.1.2 User-Agent: test Authorization: Digest username="admin", realm="HTROM", nonce="5d6553aca400536fcd1dca1d467bc428", uri="/cgi/proc/sound?1000&1000", algorithm=MD5, response="7053664025903c9c1485a188023909bd", opaque="3115FB22", qop=auth, nc=00000001, cnonce="63d86b75894ca977"*
我的代码如下所示:
procedure TMainForm.FormCreate(Sender: TObject);
begin
with idHTTP do begin
HTTPOptions := HTTPOptions + [hoInProcessAuth];
Request.BasicAuthentication := False;
HandleRedirects := True;
Request.URL := '/cgi/proc/sound?1000&1000';
Request.UserAgent := 'test';
end;
//add result to memo
meInfo.Text := idHTTP.Get('http://10.0.1.2');
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
idHTTP.Free;
end;
procedure TMainForm.IdHTTPAuthorization(Sender: TObject;
Authentication: TIdAuthentication; var Handled: Boolean);
begin
Authentication.Username := 'admin';
Authentication.Password := '555555';
if Authentication is TIdDigestAuthentication then
begin
with Authentication.AuthParams do begin
AddValue('realm', 'HTROM');
AddValue('nonce', '527b004c29df30afd42c9dbf43dcb6d9');
AddValue('algorithm', 'MD5');
AddValue('response', '3f4f49f5adefafdf19ce9148103486af');
AddValue('opaque', '60DB81DD');
AddValue('qop', 'auth');
AddValue('nc', '00000001');
AddValue('cnonce', '669bcf2a9b1c9deb');
end;
TIdDigestAuthentication(IdHTTP.Request.Authentication).Uri := IdHTTP.Request.URL;
TIdDigestAuthentication(Authentication).Method := 'GET';
showmessage('onAuthorization: ' + Authentication.Authentication);
end;
Handled := True;
end;
我收到错误
HTTP /1.1 400错误请求
答案 0 :(得分:1)
您没有正确请求网址。您需要将完整网址传递给TIdHTTP.Get()
,而不是使用TIdHTTP.Request.URL
属性,例如:
procedure TMainForm.FormCreate(Sender: TObject);
begin
with idHTTP do
begin
HTTPOptions := HTTPOptions + [hoInProcessAuth];
Request.BasicAuthentication := False;
HandleRedirects := True;
Request.UserAgent := 'test';
end;
//add result to memo
meInfo.Text := idHTTP.Get('http://10.0.1.2/cgi/proc/sound?1000&1000');
end;
此外,在OnAuthorization
事件中,您根本无需手动填充摘要的AuthParams
,Uri
或Method
属性。 TIdDigestAuthentication
在收到来自服务器的身份验证请求时填充AuthParams
,TIdHTTP
在向服务器发送身份验证时填充Uri
和Method
属性。
只需提供Username
和Password
即可,例如:
procedure TMainForm.IdHTTPAuthorization(Sender: TObject; Authentication: TIdAuthentication; var Handled: Boolean);
begin
Authentication.Username := 'admin';
Authentication.Password := '555555';
Handled := True;
end;