IdHTTP basic authentication access violation
Asked Answered
K

2

8

I'm trying to do a HTTP Authentication with Indy HTTP class. But for some unknown reason I'm getting an Access Violation error in this line: IdHTTP1.Request.Authentication.Username := Username;

The code stretch is:

  IdHTTP1:= TIdHttp.Create(Application);
  IdHTTP1.ConnectTimeout:= 10000;
  IdHTTP1.Request.Clear;
  IdHTTP1.Request.BasicAuthentication:= true;
  IdHTTP1.Request.Authentication.Username := Username;
  IdHTTP1.Request.Authentication.Password := Password;
      try
        IdHTTP1.Get(PbxURL);
        HttpCode := IdHTTP1.ResponseCode;
      except
        on E: EIdHTTPProtocolException do
          HttpCode := IdHTTP1.ResponseCode;

I'm using Delphi 2010, and already tried to do something like: IdHTTP1.Request.Authentication.Username := 'admin'; but didn't solved the problem...

Kiushu answered 15/9, 2012 at 20:47 Comment(3)
When you say "access violation" or "error" or "exception", you need to tell us what the error or AV or exception is (the exact error message, including any memory addresses or error codes). We can't see your screen from where we are, so if you don't tell us, we don't know. (Based on the limited info you've provided, you're getting an error trying to write to 00000000, which means there's no object instance created for IdHTTP1.Request.Authentication.)Haswell
Okay, first thing is: when i'm running the .exe outside the compiler/debugger, it don't shows me any error, just don't work... but when i'm debugging, I get this error: Project Project1.exe raised exception class EAccessViolation with message 'Access violation at address 0051AC50 in module 'Project1.exe'. Read of address 00000000'.Kiushu
Just as I said; there's no Request.Authentication object instance created. See my answer.Haswell
H
22

From a quick check, it appears that there is no IdHTTP.Request.Authentication needed (and therefore none is created) when the Request.BasicAuthentication is true. You should be using Request.UserName and Request.Password instead.

IdHTTP1:= TIdHttp.Create(Application);
IdHTTP1.ConnectTimeout:= 10000;
IdHTTP1.Request.Clear;
IdHTTP1.Request.BasicAuthentication:= true;
IdHTTP1.Request.UserName := UserName;
IdHTTP1.Request.Password := Password;
Haswell answered 15/9, 2012 at 20:56 Comment(2)
It's looking like it working now, but that's something strange, in other application, I have IdHTTP1.Request.Authentication.Username and it works properly. And I'm creating same objects in same order than in this project...Kiushu
It can't be working with Request.Authentication.Username if you're using basic authentication in the other app, if everything else is the same. Are the versions of Indy the same in both apps?Haswell
H
3

By default, the Request.Authentication object is not allocated until a request has been sent and an authentication response has been received, then the OnSelectAuthorization event is triggered to determine which class type to allocate for the object for subsequent requests..

The only other way the Request.Authentication object can be allocated is if you do it manually in your own code before sending a request, such as if you know ahead of time which auth scheme the server uses without sending a request to discover that dynamically.

Husk answered 16/9, 2012 at 3:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.