Delphi中下载图片的问题

发布于 2025-01-12 09:38:40 字数 2722 浏览 0 评论 0 原文

我正在编写一个图像下载软件。但我在下载一些图像时遇到问题,例如:

https://books.google.com/books/content?id=8_pCYmpCu6UC&pg=PT4&img=1&zoom=3&hl=en&bul=1&sig=ACfU3U10JF_yd0n7gvCllBts88O_ufTWxA&w=1280

您可以在浏览器中查看(你应该看到一个标题为1.Introduction的页面。如果你看不到,这与你的区域限制有关。使用美国代理可以解决这个问题)。

我在Win 10中使用Delphi 10.3 update 2。我的项目是32版本。

1-我使用 TIdHttp 下载文件,但我得到了另一个图像(包含以下内容的图像:“图像不可用”)。

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-使用 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-但是我使用以下代码达到了目标:

URLMon.URLDownloadToFile(nil, PChar(FURL), PChar(FFileName), 0, nil)

为什么?!!! 我认为这与 TIdhttp 和 TNetHttpClient 的某些默认值或某些 SSL 协议有关。

有人可以指导我吗?

I am writing an image downloader software. But I have problem to download some images such as:

https://books.google.com/books/content?id=8_pCYmpCu6UC&pg=PT4&img=1&zoom=3&hl=en&bul=1&sig=ACfU3U10JF_yd0n7gvCllBts88O_ufTWxA&w=1280

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§对你不离不弃 2025-01-19 09:38:40

根据AmigoJack的评论:

这与cookies有关。
UrlMon 默认支持 cookies,但对于 TIdHttp 我们应该强制它使用这些 cookies。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文