0
votes

I have some device that works with the HTTP protocol.

The connection message must be like this one:

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"*

My code looks like this:

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;

I get an error

HTTP /1.1 400 Bad Request

1

1 Answers

1
votes

You are not requesting the URL correctly. You need to pass the complete URL to TIdHTTP.Get(), not use the TIdHTTP.Request.URL property, eg:

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;

Also, in the OnAuthorization event, you do not need to populate the digest's AuthParams, Uri, or Method properties manually at all. TIdDigestAuthentication populates the AuthParams when it receives an authentication request from the server, and TIdHTTP populates the Uri and Method properties when it sends authentication to the server.

Just provide the Username and Password, that is all you need, eg:

procedure TMainForm.IdHTTPAuthorization(Sender: TObject; Authentication: TIdAuthentication; var Handled: Boolean);
begin
  Authentication.Username := 'admin';
  Authentication.Password := '555555';
  Handled := True;
end;