double check your codes, it should works. someone has asked the same question before, but at last it is not related with WinInet.

解决方案 »

  1.   

    thanks,
        but, if i change the original file to another type ,for example, the original file is index.php, and when i change it to the file ,index.html,it works.
        I don't know why.
        can u give me some tips.
        thx.
      

  2.   

    _strServer = "localhost";
    m_strPath = "/xxxx.php";       xxxx为你要的php文件名
    HINTERNET hSession=::InternetOpen("Mozilla 4.0",INTERNET_OPEN_TYPE_PRECONFIG ,
                                  "",INTERNET_INVALID_PORT_NUMBER,0); if(hSession == NULL){
    AfxMessageBox("Session Initialization failed!",MB_OK,0);
    VERIFY(::InternetCloseHandle(hSession));
    return;
    } HINTERNET hConnect=::InternetConnect(hSession,//当前internet会话句柄
                                       m_strServer,//server name
       INTERNET_INVALID_PORT_NUMBER,
       NULL,//"",//user name
       "",//password
       INTERNET_SERVICE_HTTP,//Type of service to access
       0,
       0); if(hConnect == NULL){
    AfxMessageBox("Connect Initialization failed!",MB_OK,0);
    VERIFY(::InternetCloseHandle(hSession));
    } LPCTSTR sType = "*/*";
    LPCTSTR *pFileType = &sType;
    HINTERNET hHttpFile=::HttpOpenRequest(hConnect,
      "GET",
      m_strPath,
      "HTTP/1.1",
      NULL,
      pFileType,
      INTERNET_FLAG_DONT_CACHE,
      0); if(hHttpFile == NULL){
    AfxMessageBox("HttpFile Initialization failed!",MB_OK,0);
    VERIFY(::InternetCloseHandle(hConnect));
    VERIFY(::InternetCloseHandle(hSession));
    } BOOL bSendRequest = ::HttpSendRequest(hHttpFile,
    NULL,
    0,
    0,
    0); if(bSendRequest){
    char chQueryBuf[16];
    DWORD dwFileSize;
    for(int i = 0; i < 16; i++)
    chQueryBuf[i] = '\0';
    dwFileSize = 0;
    DWORD dwQueryBufLen = sizeof(chQueryBuf);
    BOOL bQuery = ::HttpQueryInfo(hHttpFile,
                            HTTP_QUERY_CONTENT_LENGTH,
        chQueryBuf,
    &dwQueryBufLen,
    NULL); if(bQuery){
    dwFileSize = (DWORD)atol(chQueryBuf);
    }
    else{
    dwFileSize = 10*1024;
    }

    char *lpszBuf = new char[dwFileSize + 1]; DWORD dwBytesRead;
    BOOL bRead = ::InternetReadFile(hHttpFile,
    lpszBuf,
    dwFileSize+1,
    &dwBytesRead);
    char *lpszTemp = lpszBuf;
    lpszTemp += dwFileSize;
    *lpszTemp = '\0';
    lpszTemp = NULL; CFile file;
    if(file.Open("d:\\xxxx.php",CFile::modeCreate|CFile::modeWrite))
    {
    file.Write(lpszBuf,dwBytesRead);
    int i;
    i = strlen(lpszBuf);
    void flush();
    file.Close();
    }
    else
    {
    MessageBox("dlfj");
    }
      

  3.   

    LPCTSTR sType = "*/*";
    LPCTSTR *pFileType = &sType;
    HINTERNET hHttpFile=::HttpOpenRequest(hConnect, "GET", m_strPath,   "HTTP/1.1", NULL, pFileType, INTERNET_FLAG_DONT_CACHE,
     0);
    关键就是那个文件类型的参数LPCTSTR *lplpszAcceptTypes
    我上面的程序是可以下载任意类型文件的,已测试通过