最近公司要做一个版本自动升级的东西,所以要到网站上下载最新版本文件
那在VC下怎么写,不用特殊的控件,,谢谢!!!!!!!

解决方案 »

  1.   

    try{
    CInternetSession session(_T("xxx"));
    session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,32000);
    session.SetOption(INTERNET_OPTION_CONNECT_RETRIES,10);
    session.SetOption(INTERNET_OPTION_CONNECT_BACKOFF,1000);
    CHttpConnection *pConn = session.GetHttpConnection("www.xxx.com");
    CHttpFile *hFile = NULL;
    hFile = pConn->OpenRequest("GET", "filename"); if(hFile)
    {
    hFile->AddRequestHeaders(L"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, */*");
    hFile->AddRequestHeaders(L"Accept-Language: zh-cn");
    hFile->AddRequestHeaders(L"Content-Type: application/x-www-form-urlencoded");
    hFile->AddRequestHeaders(L"Accept-Encoding: gzip, deflate");
    hFile->AddRequestHeaders(L"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)");
    hFile->AddRequestHeaders(L"Connection: Keep-Alive");
    hFile->AddRequestHeaders(L"Cache-Control: no-cache");
    hFile->AddRequestHeaders(L"Content-Type: application/x-www-form-urlencoded"); hFile->SendRequest();

                               char m_pBuffer[MAX_LENGTH];  // m_pBuffer中保存下载文件的内容
    memset (m_pBuffer, 0, MAX_LENGTH);
    int m_iBuffer = 0;

    int iBytes = 0;
    while((iBytes = hFile->Read(m_pBuffer+m_iBuffer, MAX_LENGTH -m_iBuffer)) )
    {
    m_iBuffer += iBytes;
    }
    hFile->Close();
    pConn->Close();

    session.Close();

    }
    else
    {
    return FALSE;
    }
    }
    catch(CInternetException *pEx)
    {
                       return FALSE;
    }
      

  2.   

    但是还是不行,,m_pBuffer里面好象没内容啊
      

  3.   

    不用这么复杂吧!
    unsigned char myData[10000];
    CInternetSession mySession(NULL,0);
    CHttpFile* myHttpFile=NULL; //myHttpFile=(CHttpFile*)mySession.OpenURL(urlstr,1,INTERNET_FLAG_TRANSFER_BINARY);
    try
    {
    myHttpFile=(CHttpFile*)mySession.OpenURL("www.sina.com.cn",1,INTERNET_FLAG_TRANSFER_BINARY);
    }
    catch(CInternetException *e)
    {
    AfxMessageBox("不能打开指定网页");
    mySession.Close();
    return ;
    }

    unsigned long code;
    myHttpFile->QueryInfoStatusCode(code);
    long length = myHttpFile->GetLength();
    CString title;
    CString name;
    title = myHttpFile->GetFileTitle();
    name = myHttpFile->GetFileName();
    CFile f;
    CFileException e;
    char* pFileName = "test.txt";
    if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, &e ) )
    {
    return ;
    } UINT nReturnCount;
    while(nReturnCount=myHttpFile->Read(myData,10000))
    {
    f.Write(myData,nReturnCount);
    }
    myHttpFile->Close();
    mySession.Close();
    f.Close();
    AfxMessageBox("读取网页完成");
      

  4.   

    可以用一个很简单的函数!URLDownloadToFile就可以了!!!!!!!!!
      

  5.   

    long length = myHttpFile->GetLength();
      

  6.   

    VC中实现在线版本检测
    http://www.xiaozhou.net/ReadNews.asp?NewsID=242
      

  7.   

    在本地比较文件的时间繁琐了一些,因为服务器上文件只有一个,如何更方便一点比较,升级?
    可否直接在http请求头中加入:if-Modified-Since这项
    然后根据服务器返回响应:200或(304)来判断 服务器上的文件 已经(没有)修改,从而(不)下载?