开发者

InternetReadFile reads only 10kb

开发者 https://www.devze.com 2023-03-18 05:16 出处:网络
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 r

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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消