比如sina的首页,我想下载到本机,不知道有什么办法能够实现

解决方案 »

  1.   

    Bool GetFromWeb(LPSTR pURL, LPSTR SaveAsFilePath ) { CInternetSession session; //会话期对象)CHttpConnection* pServer = NULL; //指向服务器地址(URL)CHttpFile * pHttpFile = NULL; //HTTP文件指针CString strServerName; //服务器名CString strObject; //查询对象名(http文件)INTERNET_PORT nPort; //端口DWORD dwServiceType; //服务类型DWORD dwHttpRequestFlags = //请求标志//INTERNET_FLAG_EXISTING_CONNECT INTERNET_FLAG_NO_AUTO_REDIRECT;const TCHAR szHeaders[] = _T("Accept: text/*\r\nUser-Agent: HttpClient\r\n");BOOL OK=AfxParseURL( //词法分析pURL, //被分析URL串dwServiceType, //服务类型,ftp,http等strServerName, //服务器名strObject, //URL中被查询对象nPort ); //URL指定的端口,可能为空OK=OK && //本例只考虑http协议(dwServiceType ==INTERNET_SERVICE_HTTP);if (!OK) { AfxMessageBox("URL出错"); //报错return false; }pServer = session.GetHttpConnection(strServerName, nPort); //获得服务器名pHttpFile = pServer-> OpenRequest( CHttpConnection::HTTP_VERB_GET,strObject, NULL, 1, NULL, NULL,dwHttpRequestFlags);//向服务器发送请求,建立http连接,//建立本机上的http文件指针pHttpFile->AddRequestHeaders(szHeaders);pHttpFile->SendRequest(); //发送请求CStdioFile f; //输出文件对象if( !f.Open( //打开输出文件SaveAsFilePath, CFile::modeCreate | CFile::modeWrite | CFile::typeText ) ) { MessageBox( "Unable to open file"; return false;}//下面将检索结果保存到文件上TCHAR szBuf[1024]; //缓存while (pHttpFile->ReadString(szBuf, 1023))f.WriteString( szBuf );f.Close(); //善后工作pHttpFile ->Close();pServer ->Close();if (pHttpFile != NULL) delete pHttpFile;if (pServer != NULL) delete pServer;session.Close();return true;} 
      

  2.   

    还有这个:
    //Download http file through proxy
    //--------------------------------------------------------------------------------
    We can find details about Proxy Authentication and Sever Authentication methed in MSDN.
    Just search for the keyword "INTERNET_OPTION_USERNAME".
    Here I give a very simple example. It works in my project.
    CString GeHttptFile(const char *url)
    {
    CString szContent;
    char strProxyList[MAX_PATH], strUsername[64], strPassword[64];
    //in this case "proxya" is the proxy server name, "8080" is its port
    strcpy(strProxyList, "proxya:8080");
    strcpy(strUsername, "myusername");
    strcpy(strPassword, "mypassword");
    DWORD dwServiceType = AFX_INET_SERVICE_HTTP;
    CString szServer, szObject;
    INTERNET_PORT nPort;
    AfxParseURL(url, dwServiceType, szServer, szObject, nPort);
    CInternetSession mysession;
    CHttpConnection* pConnection;
    CHttpFile* pHttpFile;
    pConnection = mysession.GetHttpConnection(szServer,
    INTERNET_FLAG_KEEP_CONNECTION,
    INTERNET_INVALID_PORT_NUMBER,
    NULL, NULL);
    pHttpFile = pConnection->OpenRequest("GET", szObject,
     NULL, 0, NULL, NULL,
     INTERNET_FLAG_KEEP_CONNECTION);
    //here for proxy
    INTERNET_PROXY_INFO proxyinfo;
    proxyinfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
    proxyinfo.lpszProxy = strProxyList;
    proxyinfo.lpszProxyBypass = NULL;
    mysession.SetOption(INTERNET_OPTION_PROXY, (LPVOID)&proxyinfo, sizeof(INTERNET_PROXY_INFO));
    pHttpFile->SetOption(INTERNET_OPTION_PROXY_USERNAME, strUsername, strlen(strUsername)+1);
    pHttpFile->SetOption(INTERNET_OPTION_PROXY_PASSWORD, strPassword, strlen(strPassword)+1); pHttpFile->SendRequest(NULL);
    DWORD nFileSize = pHttpFile->GetLength();
    LPSTR rbuf = szContent.GetBuffer(nFileSize);
    UINT uBytesRead = pHttpFile->Read(rbuf, nFileSize);
    szContent.ReleaseBuffer();
    pHttpFile->Close();
    delete pHttpFile;
    pConnection->Close();
    delete pConnection;
    mysession.Close();
    return szContent;
    }