以下是本人的文件下载代码
如果strURL格式为 http://www.showqq.com/download/Soft_down.asp?SoftID=821
则无法正确下载文件BOOL DownFile(CString strURL,CString strSave)
{
CInternetSession lInternetSession(NULL,1,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
CHttpFile* pHttpFile=NULL;
try
{
DWORD dwRet;
pHttpFile = (CHttpFile*)lInternetSession.OpenURL(strURL);
pHttpFile->QueryInfoStatusCode(dwRet);
}
catch(CInternetException* e)
{
char szCause[255];
e->GetErrorMessage(szCause, 255);
AfxMessageBox(szCause);
e->Delete();
pHttpFile=NULL;
return FALSE;
}
if(pHttpFile)
{
CStdioFile lStdioFile;
lStdioFile.Open(strSave,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
char chrBuffer[2048];
DWORD dwBytesRead;
while((dwBytesRead=pHttpFile->Read(chrBuffer,2048))>0)
{
lStdioFile.Write(chrBuffer,dwBytesRead);
}
lStdioFile.Close();
pHttpFile->Close();
delete pHttpFile;
pHttpFile=NULL;
}
}
如果strURL格式为 http://www.showqq.com/download/Soft_down.asp?SoftID=821
则无法正确下载文件BOOL DownFile(CString strURL,CString strSave)
{
CInternetSession lInternetSession(NULL,1,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
CHttpFile* pHttpFile=NULL;
try
{
DWORD dwRet;
pHttpFile = (CHttpFile*)lInternetSession.OpenURL(strURL);
pHttpFile->QueryInfoStatusCode(dwRet);
}
catch(CInternetException* e)
{
char szCause[255];
e->GetErrorMessage(szCause, 255);
AfxMessageBox(szCause);
e->Delete();
pHttpFile=NULL;
return FALSE;
}
if(pHttpFile)
{
CStdioFile lStdioFile;
lStdioFile.Open(strSave,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
char chrBuffer[2048];
DWORD dwBytesRead;
while((dwBytesRead=pHttpFile->Read(chrBuffer,2048))>0)
{
lStdioFile.Write(chrBuffer,dwBytesRead);
}
lStdioFile.Close();
pHttpFile->Close();
delete pHttpFile;
pHttpFile=NULL;
}
}
解决方案 »
- 请教CRichEditCtrl中如何实现按钮(多个)
- 帮我想想这个算法!!
- 高手帮忙看看是怎么回事!
- 关于查询注册表键值出现的问题,总是ERROR_MORE_DATA!?
- 请问ChildWindowFromPoint为什么总返回的不是子窗口的句柄呢?
- 如何创建有自己风格的按钮?
- 用_inp()函数如何实时得得到单片机发送给串口的数据?
- 高分求书
- 为什么VC遍写的对话框程序,执行的时候,一按回车键就消失了,怎么解决?
- unsorted_map,unsorted_set如何操作
- 请问如何用程序得到当前目录下文件名中包涵"log"字符的所有文件名?
- 再问:关于CTIME与SQL类型datetime之间转换的问题
我以前也编过这种程序,用asp编的都是下不下来的, 因为你不知道文件存储所在位置的真正URL
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
try
{
if(!OnInitSession(session))
{
return FALSE;
}
if (!AfxParseURL( (LPCTSTR)sURL, dwServiceType, strServerName, strObject, nPort) ||
dwServiceType != INTERNET_SERVICE_HTTP)
{
str_Error = "非法的URL";
return FALSE;
}
pServer = session.GetHttpConnection(strServerName, nPort);
if(pServer==NULL)
{
str_Error = "无法与服务器建立连接";
return FALSE;
}
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,strObject, NULL, 1, NULL, NULL, dwHttpRequestFlags); if(pFile==NULL)
{
str_Error = "无法与服务器建立连接";
return FALSE;
}
//////////////////////////////////////////////////////////////////////////
try
{
if(!pFile->AddRequestHeaders(szHeaders)|| !pFile->SendRequest())
{
str_Error ="网络错误-无法发送请求报头";
return FALSE;
}
}
catch (CInternetException* ex)
{
fileContent.Empty();
str_Error="无法发送http报头,可能网络状况有问题";
ex->Delete();
return FALSE;
}
//////////////////////////////////////////////////////////////////////////
if(!pFile->QueryInfoStatusCode(dwRetcode))
{
str_Error ="网络错误-无法查询反馈代码";
return FALSE;
} if(dwRetcode>=200&&dwRetcode<300)
{
if(!OnQuerLastMendTime(m_site_LSTime,news_Time,pFile))
{
return TRUE;
}
else
{
news_Time = odNowtime;
return OnGetData(session,pServer,pFile,fileContent);
}
}
else
{
//重新定向
if( dwRetcode >= 300 && dwRetcode <= 306 )
{
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation); int nPlace = strNewLocation.Find(_T("Location: "));
if (nPlace == -1)
{
str_Error ="Error: Site redirects with no new location";
if (pFile != NULL)
delete pFile;
if (pServer != NULL)
delete pServer;
session.Close();
return FALSE;
} strNewLocation = strNewLocation.Mid(nPlace + 10);
nPlace = strNewLocation.Find('\n');
if (nPlace > 0)
{
strNewLocation = strNewLocation.Left(nPlace);
}
if(strNewLocation.Find("http://",0)<0)
{
sURL = OnConversionURL(sURL,strNewLocation);
}
else
{
sURL = strNewLocation;
}
fileContent.Empty();
pFile->Close();
delete pFile;
pServer->Close();
delete pServer;
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation); int nPlace = strNewLocation.Find(_T("Location: "));
if (nPlace == -1)
{
str_Error ="Error: Site redirects with no new location";
if (pFile != NULL)
delete pFile;
if (pServer != NULL)
delete pServer;
session.Close();
return FALSE;
} strNewLocation = strNewLocation.Mid(nPlace + 10);
nPlace = strNewLocation.Find('\n');
if (nPlace > 0)
{
strNewLocation = strNewLocation.Left(nPlace);
}
szHeaders = ?
dwHttpRequestFlags=?
我不知道自己用词是否正确,就是这个意思了
对头,ASP是浏览器负责解释的,显示到客户端就成了htm,你用另存为b保存的是htm格式的。
网址:http://www.showqq.com/download/Soft_down.asp?SoftID=821
引用:http://www.showqq.com/download/qqdown_352.htm
重命名:Soft_down.asp不知是否是先将Soft_down.asp文件下载下来,再分析此文件
then
0)currentpage:http://127.0.0.1/homepage/a.asp1)Response.Redirect "homepage.asp"
means
http://127.0.0.1/homepage/homepage.asp2)Response.Redirect "anotherpath/homepage.asp"
means
http://127.0.0.1/homepage/anotherpath/homepage.asp3)Response.Redirect "anotherpath/download.zip"
the same as 2)4)Response.Redirect "../homepage.asp"
means
http://127.0.0.1/homepage.asp../ means 上一级目录
不可能下到的
ASP是根据你的请求来生成给你看的页面
给你看的页面就是HTML形式的如果那么容易就下到了
那网站就没有一点安全性可言了包括JSP和其他的语言都是这个原理的
Sat Nov 06 19:14:05 2004 GET /download/Soft_down.asp?SoftID=821 HTTP/1.1
Sat Nov 06 19:14:05 2004 Host: www.showqq.com
Sat Nov 06 19:14:05 2004 Accept: */*
Sat Nov 06 19:14:05 2004 Referer: http://www.showqq.com/download/qqdown_352.htm
Sat Nov 06 19:14:05 2004 User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)
Sat Nov 06 19:14:05 2004 Pragma: no-cache
Sat Nov 06 19:14:05 2004 Cache-Control: no-cache
Sat Nov 06 19:14:05 2004 Connection: close
Sat Nov 06 19:14:05 2004 HTTP/1.1 302 Object moved
Sat Nov 06 19:14:05 2004 Server: Microsoft-IIS/5.0
Sat Nov 06 19:14:05 2004 Date: Sat, 06 Nov 2004 11:14:44 GMT
Sat Nov 06 19:14:05 2004 Connection: close
Sat Nov 06 19:14:05 2004 Location: http://dl.showqq.com/showqqdl/CoralQQ_showqq.com.rar
Sat Nov 06 19:14:05 2004 Content-Length: 121
Sat Nov 06 19:14:05 2004 Content-Type: text/html
Sat Nov 06 19:14:05 2004 Set-Cookie: ASPSESSIONIDSQBATCAS=PLLBGKJBIGGBIKBNKJBEDDJO; path=/
Sat Nov 06 19:14:05 2004 Cache-control: private
Sat Nov 06 19:14:05 2004 重定向到 http://dl.showqq.com/showqqdl/CoralQQ_showqq.com.rar
Sat Nov 06 19:14:05 2004 正在连接 dl.showqq.com:80
Sat Nov 06 19:14:05 2004 正在连接 dl.showqq.com [IP=61.152.251.66:80]
Sat Nov 06 19:14:05 2004 已连接.
Sat Nov 06 19:14:05 2004 GET /showqqdl/CoralQQ_showqq.com.rar HTTP/1.1
Sat Nov 06 19:14:05 2004 Host: dl.showqq.com
Sat Nov 06 19:14:05 2004 Accept: */*
Sat Nov 06 19:14:05 2004 Referer: http://www.showqq.com/download/qqdown_352.htm
Sat Nov 06 19:14:05 2004 Cookie: ASPSESSIONIDSQBATCAS=PLLBGKJBIGGBIKBNKJBEDDJO
Sat Nov 06 19:14:05 2004 User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)
Sat Nov 06 19:14:05 2004 Pragma: no-cache
Sat Nov 06 19:14:05 2004 Cache-Control: no-cache
Sat Nov 06 19:14:05 2004 Connection: close
Sat Nov 06 19:14:05 2004 HTTP/1.1 200 OK
应该先查看返回信息,如果是302就表示对象重定向,查看服务器返回的
Location: 信息,最终位置就在里面
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation); int nPlace = strNewLocation.Find(_T("Location: "));
if (nPlace == -1)
{
str_Error ="Error: Site redirects with no new location";
if (pFile != NULL)
delete pFile;
if (pServer != NULL)
delete pServer;
session.Close();
return FALSE;
} strNewLocation = strNewLocation.Mid(nPlace + 10);
nPlace = strNewLocation.Find('\n');
if (nPlace > 0)
{
strNewLocation = strNewLocation.Left(nPlace);
}
{
CInternetSession session(NULL,1,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL; CString strServer;
CString strObject;
DWORD dwServiceType;
INTERNET_PORT nServerPort;
if(!AfxParseURL(strURL, dwServiceType, strServer, strObject, nServerPort))
{
TRACE("非法的URL");
return FALSE;
}
if(dwServiceType != INTERNET_SERVICE_HTTP)
{
TRACE("非法的URL");
return FALSE;
}
pServer = session.GetHttpConnection(strServer, nServerPort);
if(pServer==NULL)
{
TRACE("无法与服务器建立连接");
return FALSE;
}
DWORD dwHttpRequestFlags = INTERNET_FLAG_FROM_CACHE;
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject, NULL, 1, NULL, NULL, dwHttpRequestFlags);
if(pFile==NULL)
{
TRACE("无法与服务器建立连接");
return FALSE;
}
try
{
CHAR szHeaders[] = "Accept: */*\r\n\r\n";
if(!pFile->AddRequestHeaders(szHeaders)|| !pFile->SendRequest())
{
TRACE("网络错误-无法发送请求报头");
return FALSE;
}
}
catch (CInternetException* ex)
{
TRACE("无法发送http报头,可能网络状况有问题");
ex->Delete();
return FALSE;
}
//////////////////////////////////////////////////////////////////////////
DWORD dwRetcode = 0;
if(!pFile->QueryInfoStatusCode(dwRetcode))
{
TRACE("网络错误-无法查询反馈代码");
return FALSE;
}
if(dwRetcode >=200 && dwRetcode < 300)
{
TRACE("正常文件");
}
else if(dwRetcode >= 300 && dwRetcode <= 306)
{
CString strNewLocation;
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);
int nPlace = strNewLocation.Find(_T("Location: "));
if (nPlace == -1)
{
TRACE("Error: Site redirects with no new location");
if (pFile != NULL)
delete pFile;
if (pServer != NULL)
delete pServer;
session.Close();
return FALSE;
}
strNewLocation = strNewLocation.Mid(nPlace + 10);
nPlace = strNewLocation.Find('\n');
if (nPlace > 0)
{
strNewLocation = strNewLocation.Left(nPlace);
}
if(strNewLocation.Find("http://",0)<0)
{
// strURL = OnConversionURL(strURL, strNewLocation);
}
else
{
strURL = strNewLocation;
}
pFile->Close();
delete pFile;
pServer->Close();
delete pServer;
}
return true;
}这是上次你的代码,我试了,查询反馈的状态码总是200
是asp重定向,还是html 的自动转向等等 模拟ie就行了