我自己写了一个方法从网上下载文件,遇到了一个问题。当我在本地的IIS测试时,下载都是很正常的。但是当我用真正的服务器上域名下载.exe时,就总是报404找不到文件错误。其实这个文件是肯定在的。用IE直接都可以下载的。但我下载网站上的about.htm又是可以的。不知道这是什么问题,请高手帮帮忙。谢谢了!!!本地时我直接调GetWebFile(NULL, "localhost", 80, "admin.mdb");是可以的
但是调用GetWebFile(NULL, "www.lanz.net.cn", 80, "admin.mdb");就报404找不到文件
而调用GetWebFile(NULL, "www.lanz.net.cn", 80, "about.htm");又是可以。///////////////////////////////////
DWORD CWebAccess::GetWebFile(LPCTSTR pstrAgent, LPCTSTR lpstrServer, int nPort, CString strFile)
{
CHttpConnection *pHttpConnection;
// http file pointer
CHttpFile *pHttpFile; DWORD dwServiceType = AFX_INET_SERVICE_HTTP;
CString szServer, szObject;
INTERNET_PORT Port;
AfxParseURL(lpstrServer, dwServiceType, szServer, szObject, Port); try 
{
pHttpConnection=GetHttpConnection(szServer, INTERNET_FLAG_KEEP_CONNECTION, Port ); if( NULL == pHttpConnection)
{
// no exception raised but there is an error. 
return WEB_ACCESS_UNEXPECTED_ERROR;
}
}
catch (CInternetException *pException)
{
char buffer[1023];
pException->GetErrorMessage(buffer, 1023);
CString resultString = buffer;
return pException->m_dwError;
} pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, 
strFile, NULL, 1, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION); 
// pFile->AddRequestHeaders(szHeaders); 
try
{
pHttpFile->SendRequest(); 
}
catch(CInternetException* pEx)
{
TCHAR szError[1024];
pEx->GetErrorMessage(szError,1024);
AfxMessageBox(szError);
pHttpFile->Close();
return FALSE;
} CString str;
if(pHttpFile)
{
pHttpFile->QueryInfo( HTTP_QUERY_STATUS_CODE,str); if(str=="404") /////////////这里总是返回404
{
// m_strStatus=strFile+"不存在!";
// UpdateData(FALSE);
pHttpFile->Close();
return FALSE;
} pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,str);
DWORD dwLen=atol(str);
int n=strFile.ReverseFind('/');
str=m_strAppPath+strFile.Right(strFile.GetLength()-n-1);
CStdioFile csf;
if(!csf.Open(str+".upg",CFile::modeCreate|CFile::modeWrite | CFile::typeBinary | CFile::shareDenyWrite))
{//先为*.upg文件
AfxMessageBox("写文件"+str+"错误!\n文件正在使用中,请先关闭程序!",MB_ICONSTOP);
pHttpFile->Close();
return FALSE;
} char buf[2048];
DWORD dwRead=0;
while((n=pHttpFile->Read(buf,sizeof(buf)))>0)
{
dwRead+=n;
m_pDialog->SendMessage( WM_MYPROGPOS, 0, 100*dwRead/dwLen ); MSG msg;
for(int i=0;i<10;i++)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
csf.Write(buf,n);
}
pHttpFile->Close();
}
::DeleteFile(str);
::rename(str+".upg",str); return TRUE;
}

解决方案 »

  1.   

    CWebAccess : public CInternetSession
      

  2.   

    bool SaveUrl(LPCTSTR url, LPCTSTR filename)
    {
    HINTERNET hNet = ::InternetOpen("Outlook",
    PRE_CONFIG_INTERNET_ACCESS,
    NULL,
    INTERNET_INVALID_PORT_NUMBER,
    0) ; HINTERNET hUrlFile = ::InternetOpenUrl(hNet,
    url,
    NULL,
    0,
    INTERNET_FLAG_RELOAD,
    0) ; char buffer[10*1024] ;
    DWORD dwBytesRead = 1;
    BOOL bRead=TRUE;
    CFile file;
    file.Open(filename,CFile::modeCreate|CFile::modeWrite);
    while(bRead&&dwBytesRead>0)
    {
    bRead = ::InternetReadFile(hUrlFile,
    buffer,
    sizeof(buffer),
    &dwBytesRead);
    if(dwBytesRead>0)
    file.Write(buffer,dwBytesRead);
    }
    ::InternetCloseHandle(hUrlFile) ;
    ::InternetCloseHandle(hNet) ;
    file.Close();
    AfxMessageBox("finished");
    return bRead;
    }
    void CC02021101Dlg::OnOK()
    {
    // TODO: Add extra validation here
    bool bret=SaveUrl("http://club.pchome.net/bbs2.php?topic=40&lanmuid=2","C:\\temp\\test.html");
    if(bret)
    AfxMessageBox("true");
    else
    AfxMessageBox("false");
    }
      

  3.   

    //////////////////////////////////////////////////////////////////////////
    #define WM_FERDIG WM_USER+5
    CString g_strUrl;
    CStringArray g_strArrContent;
    UINT ReadThreadProc(LPVOID pParam)
    {
        HWND hWnd = (HWND)pParam;
        g_strArrContent.RemoveAll ();
        UINT nBytesRead = 0;
        CInternetSession session;
    CStdioFile* pFile = NULL;
        try
        {
            pFile = session.OpenURL(g_strUrl, 0, INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_KEEP_CONNECTION);
            CString strLinje;
            while( pFile->ReadString (strLinje) > 0 )
                  g_strArrContent.Add(strLinje);
        }
        catch(CInternetException* e)
        {
            char buf[MAX_PATH];
            e->GetErrorMessage (buf, MAX_PATH);
            CString strErr;
            strErr = buf;
            g_strArrContent.Add(strErr);
            e->Delete();
        }
        if ( pFile ) delete pFile;
        ::PostMessage (hWnd, WM_FERDIG, 0L, 0L);
        return 0;
    }
    void CInetThreadView::OnLButtonUp(UINT nFlags, CPoint point)
    {
        CUrlDlg dlg;
        dlg.m_strUrl = g_strUrl;
        if ( dlg.DoModal() == IDCANCEL ) return;
        g_strUrl = dlg.m_strUrl;
        AfxBeginThread(ReadThreadProc, this->GetSafeHwnd());
    }
    //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;
    }
      

  4.   

    其实很简单。。你将路径指定就可以了GetWebFile(NULL, "localhost", 80, "你的目录//admin.mdb");