如何用代码实现下载网页 比如sina的首页,我想下载到本机,不知道有什么办法能够实现 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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;} 还有这个://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;} vc++中能否运行后执行自己编写的代码 关于MFC异常的一个问题 关于树控件 socket send时捕获的错误,是什么错了? 急,,,,,,,如何修改CTabCtrl控件的颜色, 如何设置文本框的输入焦点? 请求:异或绘图生成一个没有背景色的位图.期待高手,把里面代码粘下去,调试一遍后再回答,谢谢! ini我好像不能得到一个文件的位置后不能写入啊? 高分求教VC++6.0控制报表打印问题 如何在右键菜单里添加自己编写的功能 望指点 困惑了!同样的代码只有前三个功能能实现,最后一个无法实现。 我想调用print.exe去直接打印一个文件,可是win9x下却没有这个文件,怎么办?
//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;
}