.网上有用其他api函数实现文件的下载。。但是我现在主要是现在想利用WinHTTP来实现......

解决方案 »

  1.   

    MSDN:    BOOL  bResults = FALSE;
        HINTERNET hSession = NULL,
                  hConnect = NULL,
                  hRequest = NULL;    // Use WinHttpOpen to obtain a session handle.
        hSession = WinHttpOpen(  L"A WinHTTP Example Program/1.0", 
                                 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                                 WINHTTP_NO_PROXY_NAME, 
                                 WINHTTP_NO_PROXY_BYPASS, 0);    // Specify an HTTP server.
        if (hSession)
            hConnect = WinHttpConnect( hSession, L"www.wingtiptoys.com",
                                       INTERNET_DEFAULT_HTTP_PORT, 0);    // Create an HTTP Request handle.
        if (hConnect)
            hRequest = WinHttpOpenRequest( hConnect, L"PUT", L"/writetst.txt", 
                                           NULL, WINHTTP_NO_REFERER, 
                                           WINHTTP_DEFAULT_ACCEPT_TYPES, 0);    // Send a Request.
        if (hRequest) 
            bResults = WinHttpSendRequest( hRequest, 
                                           WINHTTP_NO_ADDITIONAL_HEADERS, 0,
                                           WINHTTP_NO_REQUEST_DATA, 0, 
                                           0, 0);    // PLACE ADDITIONAL CODE HERE.    // Report any errors.
        if (!bResults)
            printf("Error %d has occurred.\n",GetLastError());    // Close any open handles.
        if (hRequest) WinHttpCloseHandle(hRequest);
        if (hConnect) WinHttpCloseHandle(hConnect);
        if (hSession) WinHttpCloseHandle(hSession);
      

  2.   

        DWORD dwSize = 0;
        DWORD dwDownloaded = 0;
        LPSTR pszOutBuffer;
        BOOL  bResults = FALSE;
        HINTERNET  hSession = NULL, 
                   hConnect = NULL,
                   hRequest = NULL;    // Use WinHttpOpen to obtain a session handle.
        hSession = WinHttpOpen( L"WinHTTP Example/1.0",  
                                WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                                WINHTTP_NO_PROXY_NAME, 
                                WINHTTP_NO_PROXY_BYPASS, 0);    // Specify an HTTP server.
        if (hSession)
            hConnect = WinHttpConnect( hSession, L"www.microsoft.com",
                                       INTERNET_DEFAULT_HTTPS_PORT, 0);    // Create an HTTP request handle.
        if (hConnect)
            hRequest = WinHttpOpenRequest( hConnect, L"GET", NULL,
                                           NULL, WINHTTP_NO_REFERER, 
                                           WINHTTP_DEFAULT_ACCEPT_TYPES, 
                                           WINHTTP_FLAG_SECURE);    // Send a request.
        if (hRequest)
            bResults = WinHttpSendRequest( hRequest,
                                           WINHTTP_NO_ADDITIONAL_HEADERS, 0,
                                           WINHTTP_NO_REQUEST_DATA, 0, 
                                           0, 0); 
        // End the request.
        if (bResults)
            bResults = WinHttpReceiveResponse( hRequest, NULL);    // Keep checking for data until there is nothing left.
        if (bResults)
            do 
            {            // Check for available data.
                dwSize = 0;
                if (!WinHttpQueryDataAvailable( hRequest, &dwSize))
                    printf("Error %u in WinHttpQueryDataAvailable.\n",GetLastError());            // Allocate space for the buffer.
                pszOutBuffer = new char[dwSize+1];
                if (!pszOutBuffer)
                {
                    printf("Out of memory\n");
                    dwSize=0;
                }
                else
                {
                    // Read the Data.
                    ZeroMemory(pszOutBuffer, dwSize+1);                if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, 
                                          dwSize, &dwDownloaded))
                        printf("Error %u in WinHttpReadData.\n", GetLastError());
                    else
                        cout << pszOutBuffer;
                
                    // Free the memory allocated to the buffer.
                    delete [] pszOutBuffer;
                }        } while (dwSize>0);
        // Report any errors.
        if (!bResults)
            printf("Error %d has occurred.\n",GetLastError());    // Close any open handles.
        if (hRequest) WinHttpCloseHandle(hRequest);
        if (hConnect) WinHttpCloseHandle(hConnect);
        if (hSession) WinHttpCloseHandle(hSession);
      

  3.   


    #include<windows.h>
    #include<wininet.h>
    #include<iostream.h>
    #pragma comment(lib,"wininet.lib")
    int main(void)
    {
    DWORD byteread=0;
    char buffer[100];
    memset(buffer,0,100);
    HINTERNET internetopen;
    internetopen=InternetOpen("Testing",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
    if (internetopen==NULL){
    cout<<"Internet open failed!"<<endl;
    return;
    }
    HINTERNET internetopenurl;
    internetopenurl=InternetOpenUrl(internetopen,"http://hi.baidu.com/Tr0j4n/1.exe",NULL,0,INTERNET_FLAG_RELOAD,0);
    if (internetopenurl==NULL){
       cout<<"Internet open url failed!"<<endl;
       InternetCloseHandle(internetopen);
    }BOOL hwrite;
    DWORD written;
    HANDLE createfile;
    createfile=CreateFile("c:\\Down.exe",GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
    if (createfile==INVALID_HANDLE_VALUE){
       cout<<"Create File failed!"<<endl;
       InternetCloseHandle(internetopenurl);
    }
    BOOL internetreadfile;
    while(1){
       internetreadfile=InternetReadFile(internetopenurl,buffer,sizeof(buffer),&byteread);
       if(byteread==0)
        break;
       hwrite=WriteFile(createfile,buffer,sizeof(buffer),&written,NULL);
       if (hwrite==0){
        cout<<"Write to file failed!"<<endl;
        CloseHandle(createfile);
       }
    }
    cout<<"Finished downloading!"<<endl;
    }
      

  4.   

    楼上的代码能下载文件,但是不是利用WinHTTP实现的啊。。