none
How to get request data from IDownLoadManager::Download RRS feed

  • Question

  • hi,

    i'm working on a custom download,When the download target is not a fileWhen the download target is not a file,I need not only a URL, but also information such as input parameters and request types.

    I check MSDN, which only contains the example of getting URL.Please help me.

    HRESULT DownLoadManager::Download(IMoniker* pmk,
                                      IBindCtx* pbc,
                                      DWORD bind_verb,
                                      LONG grf_bindf,
                                      BINDINFO* bind_info,
                                      LPCOLESTR headers,
                                      LPCOLESTR redir,
                                      UINT ui_cp) {
      LPOLESTR name = NULL;
      name = (LPOLESTR)CoTaskMemAlloc(sizeof(wchar_t) * 10240);
      pmk->GetDisplayName(pbc, NULL, &name);
      download_url = name;
      CoTaskMemFree(name);

      variant_t var;
      WebBrowser::GetWebBrowser()->DownLoadFile(download_url,
                                                0, var);
      return S_OK;

    }

    Thursday, November 7, 2019 7:22 AM

All replies

  • Please refer to this sample, it will create an HttpWebRequest to download the file, you could add the parameters and set the request type:

            /// <summary> 
            /// Download the data using HttpWebRequest. It will read a buffer of bytes from the 
            /// response stream, and store the buffer to a MemoryStream cache first. 
            /// If the cache is full, or the download is paused, canceled or completed, write 
            /// the data in cache to local file. 
            /// </summary> 
            void Download() 
            { 
                HttpWebRequest webRequest = null; 
                HttpWebResponse webResponse = null; 
                Stream responseStream = null; 
                MemoryStream downloadCache = null; 
                lastStartTime = DateTime.Now; 
     
                // Set the status. 
                this.Status = HttpDownloadClientStatus.Downloading; 
     
                try 
                { 
     
                    // Create a request to the file to be  downloaded. 
                    webRequest = (HttpWebRequest)WebRequest.Create(Url); 
                    webRequest.Method = "GET"; 
                    webRequest.Credentials = CredentialCache.DefaultCredentials; 
     
     
                    // Specify the block to download. 
                    if (EndPoint != int.MaxValue) 
                    { 
                        webRequest.AddRange(StartPoint + DownloadedSize, EndPoint); 
                    } 
                    else 
                    { 
                        webRequest.AddRange(StartPoint + DownloadedSize); 
                    } 
     
                    // Retrieve the response from the server and get the response stream. 
                    webResponse = (HttpWebResponse)webRequest.GetResponse(); 
     
                    responseStream = webResponse.GetResponseStream(); 
     
     
                    // Cache data in memory. 
                    downloadCache = new MemoryStream(this.MaxCacheSize); 
     
                    byte[] downloadBuffer = new byte[this.BufferSize]; 
     
                    int bytesSize = 0; 
                    int cachedSize = 0; 
                    int receivedBufferCount = 0; 
     
                    // Download the file until the download is paused, canceled or completed. 
                    while (true) 
                    { 
     
                        // Read a buffer of data from the stream. 
                        bytesSize = responseStream.Read(downloadBuffer, 0, downloadBuffer.Length); 
     
                        // If the cache is full, or the download is paused, canceled or  
                        // completed, write the data in cache to local file. 
                        if (this.Status != HttpDownloadClientStatus.Downloading 
                            || bytesSize == 0 
                            || this.MaxCacheSize < cachedSize + bytesSize) 
                        { 
     
                            try 
                            { 
                                // Write the data in cache to local file. 
                                WriteCacheToFile(downloadCache, cachedSize); 
     
                                this.DownloadedSize += cachedSize; 
     
                                // Stop downloading the file if the download is paused,  
                                // canceled or completed.  
                                if (this.Status != HttpDownloadClientStatus.Downloading 
                                    || bytesSize == 0) 
                                { 
                                    break; 
                                } 
     
                                // Reset cache. 
                                downloadCache.Seek(0, SeekOrigin.Begin); 
                                cachedSize = 0; 
                            } 
                            catch (Exception ex) 
                            { 
                                // Fire the DownloadCompleted event with the error. 
                                this.OnDownloadCompleted(new HttpDownloadCompletedEventArgs( 
                                                   this.DownloadedSize, this.TotalSize, 
                                                   this.TotalUsedTime, ex)); 
                                return; 
                            } 
     
                        } 
     
     
                        // Write the data from the buffer to the cache in memory. 
                        downloadCache.Write(downloadBuffer, 0, bytesSize); 
     
                        cachedSize += bytesSize; 
     
                        receivedBufferCount++; 
     
                        // Fire the DownloadProgressChanged event. 
                        if (receivedBufferCount == this.BufferCountPerNotification) 
                        { 
                            InternalDownloadProgressChanged(cachedSize); 
                            receivedBufferCount = 0; 
                        } 
                    } 
     

    Thursday, November 7, 2019 11:51 AM
  • I am not sure where your input parameters and request types are stored. But you can get post data and headers via IBindStatusCallback and IHttpNegotiate. See https://www.codeproject.com/Articles/10368/An-ATL-control-for-hosting-and-customization-of-mu


    Visual C++ MVP


    Thursday, November 7, 2019 7:55 PM
  • I've got it. Thank you!

    variant_t var = headers;
      DWORD request_type = 0;
      std::wstring request_body;
      if (bind_info) {
        request_type = bind_info->dwBindVerb;
        int data_size = bind_info->cbstgmedData;
        if (data_size > 0) {
          if (bind_info->stgmedData.tymed & TYMED_HGLOBAL) {
            char* body_c = (char *)::GlobalLock(bind_info->stgmedData.hGlobal);
            wchar_t* body_wc = new wchar_t[data_size + 1];
            MultiByteToWideChar(CP_ACP, 0, body_c, -1, body_wc, data_size);
            body_wc[data_size] = '\0';
            request_body = body_wc;
            delete[] body_wc;
          }
        }
      }

    Friday, November 29, 2019 3:47 AM