InternetReadFile 仅读取 10kb

发布于 2024-11-19 08:17:26 字数 1371 浏览 4 评论 0原文

我正在尝试使用 WinINet 库调用从 http 服务器下载文件。它在我的本地网络服务器上运行得很好。但是当我尝试从互联网下载某些内容时,InternetReadFile 只能读取约 10kb 的任何文件(文本或二进制)。在下面的示例中,TRANSFER_SIZE = 4096,因此有两次 4kb 读取和一次 2kb 读取。每个下一个 InternetReadFile 返回 true 并读取 0 个字节。

    hInternet = InternetOpen(L"Agent", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    DWORD options = INTERNET_FLAG_NEED_FILE|INTERNET_FLAG_HYPERLINK|INTERNET_FLAG_RESYNCHRONIZE|INTERNET_FLAG_RELOAD;
    HINTERNET hSession = InternetOpenUrl(hInternet, URL, NULL, NULL, options, 0);

    hFile = CreateFile(...);
    ...
    do {
         DWORD dwWriteSize, dwNumWritten;
         BOOL bRead = InternetReadFile(hSession, pBuf, TRANSFER_SIZE, &dwReadSizeOut);
         dwWriteSize = dwReadSizeOut;

         if (bRead && dwReadSizeOut > 0) {
             dwTotalReadSize += dwReadSizeOut;
             WriteFile(hFile, pBuf, dwWriteSize, &dwNumWritten, NULL); 
             // File write error
             if (dwWriteSize != dwNumWritten) {
                 CloseHandle(hFile);                    
                 return false;
             }
         }
         else {
             if (!bRead)
             {
                 // Error
                 CloseHandle(hFile);    
                 return false;
             }          
             break;
         }
     } while(1);

如何使用 WinINet 库下载整个文件?

i'm trying to download file from http server using WinINet library calls. It works perfectly fine on my local web server. but when i'm trying download something from the internet, InternetReadFile reads only ~10kb of any file (text or binary). TRANSFER_SIZE = 4096 in the example below, thus there are two reads by 4kb and one by 2kb. Every next InternetReadFile returns true and 0 bytes read.

    hInternet = InternetOpen(L"Agent", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    DWORD options = INTERNET_FLAG_NEED_FILE|INTERNET_FLAG_HYPERLINK|INTERNET_FLAG_RESYNCHRONIZE|INTERNET_FLAG_RELOAD;
    HINTERNET hSession = InternetOpenUrl(hInternet, URL, NULL, NULL, options, 0);

    hFile = CreateFile(...);
    ...
    do {
         DWORD dwWriteSize, dwNumWritten;
         BOOL bRead = InternetReadFile(hSession, pBuf, TRANSFER_SIZE, &dwReadSizeOut);
         dwWriteSize = dwReadSizeOut;

         if (bRead && dwReadSizeOut > 0) {
             dwTotalReadSize += dwReadSizeOut;
             WriteFile(hFile, pBuf, dwWriteSize, &dwNumWritten, NULL); 
             // File write error
             if (dwWriteSize != dwNumWritten) {
                 CloseHandle(hFile);                    
                 return false;
             }
         }
         else {
             if (!bRead)
             {
                 // Error
                 CloseHandle(hFile);    
                 return false;
             }          
             break;
         }
     } while(1);

How can i download the entire file using WinINet library?

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

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

发布评论

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

评论(2

囍孤女 2024-11-26 08:17:27

尝试在 InternetOpenURl 上设置 INTERNET_FLAG_KEEP_CONNECTION。

打开后,您还应该至少在句柄上执行 HttpQueryInfo(HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER) 操作。

我建议查看一些现有的 C++ 包装类来处理这些事情,
http://www.google .com/codesearch#search/&q=INTERNET_FLAG_KEEP_CONNECTION%20lang:c%2B%2B&type=cs

Try setting INTERNET_FLAG_KEEP_CONNECTION on InternetOpenURl.

You should also be doing at least HttpQueryInfo(HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER) on the handle after opening.

I would suggest looking at some existing wrapper C++ classes to these things,
http://www.google.com/codesearch#search/&q=INTERNET_FLAG_KEEP_CONNECTION%20lang:c%2B%2B&type=cs

薄荷→糖丶微凉 2024-11-26 08:17:27

我以文本形式读取响应,这是来自网络服务器的“404 错误”响应 - 文件丢失。因此,阅读回复很有用;)

libcurl 看起来像是 WinINet 库的不错替代品 - 更容易上手,有很多选项。

I read the response as text and it was the "404 error" respond from the web server - file was missing. So it's useful to read the responses ;)

And libcurl looks like nice replacement of the WinINet library - easier to start with, a lot of options.

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