Soon Santos
Soon Santos

Reputation: 2219

How to connect to speacial links using TIdHttp in delphi?

Browser Example

I have the following link to connect to my IP Camera using user and password in the url.

http://admin:[email protected]/ISAPI/Streaming/channels/102/picture

If I open this link in Internet Explorer it show me the following message and can not open the link.

Windows can not locate <link>. Check your spelling and try again

When I open the same link in Chrome of Firefox it works fine and displays the desired jpg picture.

Delphi

In delphi I am using TIdHttp to get the picture from this link.

var
  Ms: TMemoryStream;
  PictureUrl: string;
begin
  try
    PictureUrl := 'http://admin:[email protected]/ISAPI/Streaming/channels/102/picture';
    IdHTTP1.get(PictureUrl, MS);
    Ms.Seek(0,soFromBeginning);
    MS.SaveToFile('C:\Users\karat\Documents\hikivision1.jpg');
  finally
    FreeAndNil(MS);
  end;
end;

The code works with another jpg image links, the problem is that this code gets a 178byts MemoryStream, this means that the picture is not being loaded.

I put a TWebBrowser in the form and I discovered that it is giving me the same error as in Internet Explorer browser, the browser does not support loading this type of url.

Error in webbrowser

Translation: Not possible to access this page. Check if your link is correct.

Solution?

I assume that if I find a way that TIdHttp behave as Chrome and Firefox, it will load correctly the desired picture. Any way to do that or another possible solution?

Upvotes: 1

Views: 485

Answers (2)

Powerful Maimu
Powerful Maimu

Reputation: 1

If it is not running then try to aditionaly set the following:

Request.BasicAuthentication := False; HTTPOptions := [hoInProcessAuth, hoForceEncodeParams];

Upvotes: 0

Remy Lebeau
Remy Lebeau

Reputation: 596592

Internet Explorer does not support user names and passwords in Web site addresses (HTTP or HTTPS URLs). Delphi's TWebBrowser component in VCL is just a wrapper for IE.

The code you have shown is not creating the TMemoryStream object before calling TIdHTTP.Get() with it. Is that just a typo here, or is your real code actually forgetting to do that?

Though Indy does parse username/password from URLs, and does support the @ character in passwords, since you already know ahead of time what the values are then you should put them in the UserName/Password properties of TIdHTTP.Request explicitly rather than in the URL, eg:

var
  MS: TMemoryStream;
begin
  MS := TMemoryStream.Create;
  try
    IdHTTP1.Request.UserName := 'admin';
    IdHTTP1.Request.Password := 'poi@0413477';
    IdHTTP1.Get('http://192.168.1.13/ISAPI/Streaming/channels/102/picture', MS);
    MS.Position := 0;
    MS.SaveToFile('C:\Users\karat\Documents\hikivision1.jpg');
  finally
    MS.Free;
  end;
end;

If that still does not work, it is possible that the web server in question is UserAgent-sensitive, delivering different content for different web browsers. Some web servers do not like Indy's default value for the User-Agent request header ('Mozilla/3.0 (compatible; Indy Library)', which can be changed globally via the GIdDefaultUserAgent variable in the IdHTTPHeaderInfo unit), so try changing the UserAgent to mimic a real web browser of your choosing, like Chrome or FireFox, eg:

var
  MS: TMemoryStream;
begin
  MS := TMemoryStream.Create;
  try
    IdHTTP1.Request.UserName := 'admin';
    IdHTTP1.Request.Password := 'poi@0413477';
    IdHTTP1.Request.UserAgent := ...; // ie: 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0'
    IdHTTP1.Get('http://192.168.1.13/ISAPI/Streaming/channels/102/picture', MS);
    MS.Position := 0;
    MS.SaveToFile('C:\Users\karat\Documents\hikivision1.jpg');
  finally
    MS.Free;
  end;
end;

Upvotes: 2

Related Questions