Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Jan : HTTP.Get() with Authorization Trouble (2nd Post)

www.cryer.info
Managed Newsgroup Archive

HTTP.Get() with Authorization Trouble (2nd Post)

Subject:HTTP.Get() with Authorization Trouble (2nd Post)
Posted by:"Slagert" (slage..@slagert.com)
Date:Wed, 3 Jan 2007 20:10:49

I'm including a the contents of the Main form's .pas & .dfm files of a small
program that generates an unexpected http 401 error code. The error occurs
when the URL/User/Pswd are changed after the 1st. run. The errors only occur
when using authorization.

I'm using Indy 10.1.5 but the same problem happend for me with Indy 9. I'm
compiling with D7. Can you see why I'm getting this error? Is there
something in the IdHTTP I'm
not resetting between calls to Get()?

-Andy

====================================
Main.pas
unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdHTTP;

type
  TFormMain = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Memo1: TMemo;
    IdHTTP1: TIdHTTP;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    InStream:   TMemoryStream;
  public
    { Public declarations }
  end;

var
  FormMain: TFormMain;

implementation

{$R *.dfm}

Uses
  IdException;

procedure TFormMain.Button1Click(Sender: TObject);
begin
  Screen.Cursor := crHourglass;
  Memo1.Clear;
  IdHTTP1.Request.Clear;
  IdHTTP1.Request.BasicAuthentication := true;
  IdHTTP1.Request.Username := Edit2.Text;
  IdHTTP1.Request.Password := Edit3.Text;
  try
    InStream := TMemoryStream.Create;
      try
        IdHTTP1.Get(Edit1.Text, InStream);
      except
        on E: EIdException do begin
        //Handle Indy exceptions here
        end;
      end;
    InStream.Position := 0;
    Memo1.Lines.LoadFromStream(InStream);
    InStream.Position := 0;
  finally
    InStream.Free;
  end;
  Screen.Cursor := crDefault;
end;

end.
===================================================
DFM File:
object FormMain: TFormMain
  Left = 263
  Top = 187
  Width = 800
  Height = 600
  Caption = 'HTTP Get() w/ Authorization Demo'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  Position = poScreenCenter
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 792
    Height = 73
    Align = alTop
    TabOrder = 0
    DesignSize = (
      792
      73)
    object Label1: TLabel
      Left = 112
      Top = 16
      Width = 25
      Height = 13
      Caption = 'URL:'
    end
    object Label2: TLabel
      Left = 112
      Top = 44
      Width = 25
      Height = 13
      Caption = 'User:'
    end
    object Label3: TLabel
      Left = 280
      Top = 44
      Width = 29
      Height = 13
      Caption = 'Pswd:'
    end
    object Button1: TButton
      Left = 16
      Top = 16
      Width = 75
      Height = 25
      Caption = 'Fetch HTML'
      TabOrder = 0
      OnClick = Button1Click
    end
    object Edit1: TEdit
      Left = 144
      Top = 16
      Width = 641
      Height = 21
      Anchors = [akLeft, akTop, akRight]
      TabOrder = 1
    end
    object Edit2: TEdit
      Left = 144
      Top = 40
      Width = 121
      Height = 21
      TabOrder = 2
    end
    object Edit3: TEdit
      Left = 312
      Top = 40
      Width = 121
      Height = 21
      TabOrder = 3
    end
  end
  object Memo1: TMemo
    Left = 0
    Top = 73
    Width = 792
    Height = 493
    Align = alClient
    ScrollBars = ssVertical
    TabOrder = 1
  end
  object IdHTTP1: TIdHTTP
    AllowCookies = True
    ProxyParams.BasicAuthentication = False
    ProxyParams.ProxyPort = 0
    Request.ContentLength = -1
    Request.Accept = 'text/html, */*'
    Request.BasicAuthentication = True
    Request.UserAgent = 'Mozilla/3.0 (compatible; Indy Library)'
    HTTPOptions = [hoForceEncodeParams]
    Left = 8
    Top = 80
  end
end

Replies:

www.cryer.info
Managed Newsgroup Archive