I am writing an image downloader software. But I have problem to download some images such as:
You can check it in your browser (You should see a page with 1.Introduction title. If you could not see, it is related to your region restriction. Using US proxy would solve it).
I'm using Delphi 10.3 update 2 in Win 10. My project is 32 version.
1-I used TIdHttp to download the file but I got another image (An Image with this content: "Image not available").
procedure DownloadeImage(FURL, FFileName :string);
var
HTTPClient: TIdHTTP;
FileStream: TFileStream;
begin
try
HTTPClient := TIdHTTP.Create;
HTTPClient.HandleRedirects := True;
HTTPClient.AllowCookies := True;
//HTTPClient.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
//(HTTPClient.IOHandler as TIdOpenSSLIOHandlerClient).Options.VerifyServerCertificate := False;
HTTPClient.Request.UserAgent := 'Mozilla/5.0'; //'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0';
HTTPClient.Request.Connection := 'keep-alive';
try
FileStream := TFileStream.Create(FFileName, fmCreate);
try
HTTPClient.Get(FURL, FileStream); //(TIdURI.URLEncode(FURL), FileStream);
finally
FileStream.Free;
end;
finally
HTTPClient.Free;
end;
except
// error handling ignored for this example
end;
end;
2-The same result when using TNetHttpClient
procedure DownloadeImage(FURL, FFileName :string);
var
HTTPClient: TNetHTTPClient;
FileStream: TFileStream;
begin
try
HTTPClient := TNetHTTPClient.Create(nil);
HTTPClient.HandleRedirects := True;
HTTPClient.AllowCookies := True;
//HTTPClient.SecureProtocols := [THTTPSecureProtocol.SSL2, THTTPSecureProtocol.SSL3, THTTPSecureProtocol.TLS1, //THTTPSecureProtocol.TLS11, THTTPSecureProtocol.TLS12];
//HTTPClient.Accept := 'image/png, image/gif, image/jpg, image/jpeg, image/tif, image/tiff, image/bmp, //image/x-bmp;q=0.9,*/*;q=0.8';
HTTPClient.UserAgent := 'Mozilla/5.0';
try
FileStream := TFileStream.Create(FFileName, fmCreate);
try
HTTPClient.Get(FURL, FileStream); //(TIdURI.URLEncode(FURL), FileStream);
finally
FileStream.Free;
end;
finally
HTTPClient.Free;
end;
except
// error handling ignored for this example
end;
end;
3-But I get the goal using the following code:
URLMon.URLDownloadToFile(nil, PChar(FURL), PChar(FFileName), 0, nil)
Why?!!! I think it is related to some default value of TIdhttp and TNetHttpClient or about some SSL protocol.
Can anyone guide me?
According to AmigoJack comment:
It's related to the cookies. UrlMon support cookies by default, but for TIdHttp we should force it to use those.