如题!
以下代码可以从我本地的服务器上下载txt,和.ini文件,但是不能下载exe和dll等结尾的文件.
但是在同一目录下的一些,txt.ini结尾的文件却能正常下载.
请有做过此类项目的朋友给指点一下!!
我也尝试加了一些http请求头,如
CString strHeader =_T("Content-Type:application/x-www-form-urlencoded\r\n )");之类的.
CHttpConnection *pHttpConn=NULL;
CString Filestr=FileName;
INTERNET_PORT nPort=INTERNET_DEFAULT_HTTPS_PORT;
DWORD dwServiceType=AFX_INET_SERVICE_HTTP;
DWORD dwHttpRequestFlags=HSR_DOWNLOAD | INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT;
CString TmpFileStr=m_InetFilePath+Filestr;
CString strObjectName,strServerName;
BOOL =false;
CInternetSession pSession("session");
=AfxParseURL(TmpFileStr,dwServiceType,strServerName,strObjectName,nPort);
if(!)
{
AfxMessageBox("URL解析错误!");
return false;
}
CHttpFile *pFile=NULL ; pHttpConn = pSession.GetHttpConnection(strServerName, nPort,"sm4","chuangxin");
pFile = pHttpConn->OpenRequest(CHttpConnection::HTTP_VERB_GET,
strObjectName, NULL, 1, NULL, NULL, dwHttpRequestFlags);
try {
pFile->SendRequest();
}
catch(CInternetException* pEx)
{
TCHAR szError[256];
pEx->GetErrorMessage(szError,256);
AfxMessageBox(szError);
pFile->Close();
return false;
}
if(pFile)
{
CString strMsg;
pFile->QueryInfo(HTTP_QUERY_STATUS_CODE,strMsg);//当下载以exe和dll结尾的文件时候.strMsg 得到的值总是500,就是服务器端出错!
if(strMsg == "404")
MessageBox("文件不存在!");
}
CStdioFile csf;
int dRet,n;
CString appPath,BasePath,cPath;
dRet=GetModuleFileName(NULL, appPath.GetBuffer(MAX_PATH), MAX_PATH);
appPath.ReleaseBuffer();
n = appPath.ReverseFind('\\');
BasePath = appPath.Left(n);
TCHAR c=BasePath.GetAt(n-1);
if(c=='\\')
{
BasePath+="copy";
cPath=BasePath+Filestr;
}
else
{
BasePath+="\\";
BasePath+="copy";
cPath=BasePath+Filestr;
}
if(!csf.Open(cPath,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary ))
{
AfxMessageBox("连接服务器失败!");
return false;
}
char buf[1024];
int nRead;
csf.Write(lpszBuf,dwBytesRead);
csf.Close();
以下代码可以从我本地的服务器上下载txt,和.ini文件,但是不能下载exe和dll等结尾的文件.
但是在同一目录下的一些,txt.ini结尾的文件却能正常下载.
请有做过此类项目的朋友给指点一下!!
我也尝试加了一些http请求头,如
CString strHeader =_T("Content-Type:application/x-www-form-urlencoded\r\n )");之类的.
CHttpConnection *pHttpConn=NULL;
CString Filestr=FileName;
INTERNET_PORT nPort=INTERNET_DEFAULT_HTTPS_PORT;
DWORD dwServiceType=AFX_INET_SERVICE_HTTP;
DWORD dwHttpRequestFlags=HSR_DOWNLOAD | INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT;
CString TmpFileStr=m_InetFilePath+Filestr;
CString strObjectName,strServerName;
BOOL =false;
CInternetSession pSession("session");
=AfxParseURL(TmpFileStr,dwServiceType,strServerName,strObjectName,nPort);
if(!)
{
AfxMessageBox("URL解析错误!");
return false;
}
CHttpFile *pFile=NULL ; pHttpConn = pSession.GetHttpConnection(strServerName, nPort,"sm4","chuangxin");
pFile = pHttpConn->OpenRequest(CHttpConnection::HTTP_VERB_GET,
strObjectName, NULL, 1, NULL, NULL, dwHttpRequestFlags);
try {
pFile->SendRequest();
}
catch(CInternetException* pEx)
{
TCHAR szError[256];
pEx->GetErrorMessage(szError,256);
AfxMessageBox(szError);
pFile->Close();
return false;
}
if(pFile)
{
CString strMsg;
pFile->QueryInfo(HTTP_QUERY_STATUS_CODE,strMsg);//当下载以exe和dll结尾的文件时候.strMsg 得到的值总是500,就是服务器端出错!
if(strMsg == "404")
MessageBox("文件不存在!");
}
CStdioFile csf;
int dRet,n;
CString appPath,BasePath,cPath;
dRet=GetModuleFileName(NULL, appPath.GetBuffer(MAX_PATH), MAX_PATH);
appPath.ReleaseBuffer();
n = appPath.ReverseFind('\\');
BasePath = appPath.Left(n);
TCHAR c=BasePath.GetAt(n-1);
if(c=='\\')
{
BasePath+="copy";
cPath=BasePath+Filestr;
}
else
{
BasePath+="\\";
BasePath+="copy";
cPath=BasePath+Filestr;
}
if(!csf.Open(cPath,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary ))
{
AfxMessageBox("连接服务器失败!");
return false;
}
char buf[1024];
int nRead;
csf.Write(lpszBuf,dwBytesRead);
csf.Close();
我的系统是windows xp !不知道iis是在那里设置的!!
怎么能有的可以有的不可以呢,我现在的客户端和服务器端是在通一台机器上,我都是要下载的不是运行!我的账号是管理员账号!
-----------------------
你通过HTTP来访问就不是了IIS 会根据请求的文件不同而采用不同的处理方式, 但在客户端都是一样,发送 GET filename 到服务器