BOOL CAsdDlg::DownLoad(LPSTR pURL, LPSTR SaveAsFilePath)
{
CInternetSession session; //会话期对象)
CHttpConnection* pServer = NULL; // 指向服务器地址(URL)
CHttpFile * pHttpFile = NULL;//HTTP文件指针
CString strServerName; //服务器名
CString strObject; //查询对象名(http文件)
INTERNET_PORT nPort=0; //端口
DWORD dwServiceType=AFX_INET_SERVICE_HTTP; //服务类型
DWORD dwHttpRequestFlags = INTERNET_FLAG_NO_AUTO_REDIRECT | INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_RELOAD;//请求标志
const TCHAR szHeaders[]=_T("Accept: */*\r\nUser-Agent:HttpClient\r\n");
//if(!m_sitename->Find("http://"))
//m_sitename="http://"+m_sitename;
BOOL OK=AfxParseURL(pURL,dwServiceType,strServerName,strObject,nPort);
//OK=OK&&(dwServiceType==AFX_INET_SERVICE_HTTP);//本例只考虑http协议 if(!OK)
{
AfxMessageBox("URL ERROR!"); //报错
return false;
}
else
{
return FALSE;
}
pServer = session.GetHttpConnection(strServerName,nPort); //获得服务器名
pHttpFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,strObject,NULL,NULL,"HTTP/1.1",dwHttpRequestFlags);
//向服务器发送请求,建立http连接,
//建立本机上的http文件指针
pHttpFile->AddRequestHeaders(szHeaders);
pHttpFile->SendRequest(); //发送请求
CFile f; //输出文件对象
//打开输出文件
//下面将检索结果保存到文件上
TCHAR szBuf[1024]; //缓存
int length=0;
//UINT a=pHttpFile->GetLength();
//pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,a);
//CString strRangeQuest; /*pHttpFile =(CHttpFile *)session.OpenURL(pURL, 1, INTERNET_FLAG_TRANSFER_BINARY
|INTERNET_FLAG_DONT_CACHE
|INTERNET_FLAG_PRAGMA_NOCACHE
,strRangeQuest,sizeof(strRangeQuest));*///获得文件的句柄 DWORD dwHttpFileSize = 0;
if (pHttpFile)
{
//BYTE buffer[BUFFER_SIZE+1] = {0};//缓存 pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,dwHttpFileSize);//通过查询 http 返回的信息获得网页文件大小
if(!f.Open(SaveAsFilePath,CFile::modeCreate | CFile::modeWrite | CFile::typeBinary))
{
MessageBox("Unable to open file");
return false;
}
else
{
f.Open(f.GetFileName(),CFile::modeWrite);
f.SeekToEnd();
pHttpFile->Seek(length,CFile::begin); }
while(length<=dwHttpFileSize)
{
pHttpFile->Read(szBuf, 1023);
f.Write(szBuf,length);
f.Close();
pHttpFile->Close();
pServer->Close();
if (pHttpFile != NULL)
delete pHttpFile;
if (pServer != NULL)
delete pServer;
session.Close();
return true;
}
每次输入http://www.baidu.com或其他的网址 一直报错“URL ERROR!” 快疯了 求高手帮帮忙 zheng_wei99(少走弯路)好象也提过类似的问题 在 请帮忙
{
CInternetSession session; //会话期对象)
CHttpConnection* pServer = NULL; // 指向服务器地址(URL)
CHttpFile * pHttpFile = NULL;//HTTP文件指针
CString strServerName; //服务器名
CString strObject; //查询对象名(http文件)
INTERNET_PORT nPort=0; //端口
DWORD dwServiceType=AFX_INET_SERVICE_HTTP; //服务类型
DWORD dwHttpRequestFlags = INTERNET_FLAG_NO_AUTO_REDIRECT | INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_RELOAD;//请求标志
const TCHAR szHeaders[]=_T("Accept: */*\r\nUser-Agent:HttpClient\r\n");
//if(!m_sitename->Find("http://"))
//m_sitename="http://"+m_sitename;
BOOL OK=AfxParseURL(pURL,dwServiceType,strServerName,strObject,nPort);
//OK=OK&&(dwServiceType==AFX_INET_SERVICE_HTTP);//本例只考虑http协议 if(!OK)
{
AfxMessageBox("URL ERROR!"); //报错
return false;
}
else
{
return FALSE;
}
pServer = session.GetHttpConnection(strServerName,nPort); //获得服务器名
pHttpFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,strObject,NULL,NULL,"HTTP/1.1",dwHttpRequestFlags);
//向服务器发送请求,建立http连接,
//建立本机上的http文件指针
pHttpFile->AddRequestHeaders(szHeaders);
pHttpFile->SendRequest(); //发送请求
CFile f; //输出文件对象
//打开输出文件
//下面将检索结果保存到文件上
TCHAR szBuf[1024]; //缓存
int length=0;
//UINT a=pHttpFile->GetLength();
//pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,a);
//CString strRangeQuest; /*pHttpFile =(CHttpFile *)session.OpenURL(pURL, 1, INTERNET_FLAG_TRANSFER_BINARY
|INTERNET_FLAG_DONT_CACHE
|INTERNET_FLAG_PRAGMA_NOCACHE
,strRangeQuest,sizeof(strRangeQuest));*///获得文件的句柄 DWORD dwHttpFileSize = 0;
if (pHttpFile)
{
//BYTE buffer[BUFFER_SIZE+1] = {0};//缓存 pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,dwHttpFileSize);//通过查询 http 返回的信息获得网页文件大小
if(!f.Open(SaveAsFilePath,CFile::modeCreate | CFile::modeWrite | CFile::typeBinary))
{
MessageBox("Unable to open file");
return false;
}
else
{
f.Open(f.GetFileName(),CFile::modeWrite);
f.SeekToEnd();
pHttpFile->Seek(length,CFile::begin); }
while(length<=dwHttpFileSize)
{
pHttpFile->Read(szBuf, 1023);
f.Write(szBuf,length);
f.Close();
pHttpFile->Close();
pServer->Close();
if (pHttpFile != NULL)
delete pHttpFile;
if (pServer != NULL)
delete pServer;
session.Close();
return true;
}
每次输入http://www.baidu.com或其他的网址 一直报错“URL ERROR!” 快疯了 求高手帮帮忙 zheng_wei99(少走弯路)好象也提过类似的问题 在 请帮忙
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货