DigestAuthentication in idHttp

Giganews Newsgroups
Subject: DigestAuthentication in idHttp
Posted by:  Christian Fried (cfri…@caigos.de)
Date: Tue, 28 Jun 2011

I try to post a Request to a httpserver which needs digestauthentication. In
internet Explorer the communication works fine.

But post the request with idhttp causes an autentication faild (401)
response.

I add my sample code. has anyone an idea whats wrong?

Best regards

Christian

unit mainSimple;

interface

uses
  SysUtils,
  Forms,
  Classes,
  Controls,
  StdCtrls,
  dialogs,

  IdAuthentication,
  IdAuthenticationDigest,
  IdHTTP;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure http1Authorization(Sender: TObject;
      Authentication: TIdAuthentication; var Handled: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private-Deklarationen }
    http1: TIdHTTP;
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  s: string;
  params: tstringlist;
begin
  params := tStringlist.create;
  http1.ReadTimeout := 0;

  http1.Request.BasicAuthentication := false;
//  http1.Request.Username := 'CFried';
//  http1.Request.Password := 'cf';
  s := http1.post('http://swbb-cf5:8080/digest/index.html', params);

  if http1.ResponseCode=200 then begin
    Showmessage(s);
  end else begin
    Showmessage(IntToStr(http1.ResponseCode));
  end; {endif}
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  http1 := TIdHTTP.Create(self);
  http1.AllowCookies := True;
  http1.HandleRedirects := True ;
  http1.ProxyParams.BasicAuthentication := False;
  http1.ProxyParams.ProxyPort := 0;
  http1.Request.ContentLength := -1;
  http1.Request.Accept := 'text/html, */*';
  http1.Request.BasicAuthentication := False;
  http1.Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)';
  http1.HTTPOptions := [];
  http1.OnAuthorization := http1Authorization;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  http1.Free;
end;

procedure TForm1.http1Authorization(Sender: TObject; Authentication:
TIdAuthentication; var Handled: Boolean);
begin
  Authentication.Username := 'CFried';
  Authentication.Password := 'cf';
  Handled := true;
end;

end.

Replies