URLDownloadToFile 失败,返回 0x800c0008 (INET_E_DOWNLOAD_FAILURE),具体取决于 URL 的长度

URLDownloadToFile fails with 0x800c0008 (INET_E_DOWNLOAD_FAILURE) depending on length of URL

我知道有一个类似的问题 here. However my symptoms differ. There seems to be some kind of URL length restriction in place that I couldn't find documented。 URL 中的限制是 2084(原文如此!不是 2048!)个字符。最简单的重现只是一个控制台应用程序,代码如下:

#include "stdafx.h"
#include "urlmon.h"
#pragma comment(lib, "urlmon")


int main()
{
    HRESULT hr = URLDownloadToFile(NULL,L"http://www.google.de/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png?oids=1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890",
        L"c:\temp\x.png",
        0,
        NULL);

    return 0;
}

当然,URL 没有实际意义,在我的真实示例中,这些是重要的参数。一旦我将 URL 缩短到 2084 个字符以下,我就会返回 S_OK 并且一切正常。

我使用 Fiddler 检查请求,它告诉我我看到下载的 http 状态代码 200(正常),甚至服务器响应也很完美:

有什么方法可以使用 URLDownloadToFile 下载超过 2084 个字符的 URLs 吗?

WinInet 的 INTERNET_MAX_URL_LENGTH 常数恰好是 2084,包括空终止符。

另请参阅 的答案以了解其他各种 URL-related 限制。

如果您需要下载更长的 URL,您将不得不使用另一个 HTTP 客户端库。