下载文件的代码

下载文件的代码,第1张

用MFC的CInternetFile,CInternetSession,CHttpConnection等类,已知服务器名及文件路径,代码越简单越好。

以下是代码片段:

//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

}

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")

}

下载事件说明:

OnDownloadBegin <--开始下载

OnDownloadComplete <--下载结束

OnDocumentComplete <--页面下载完毕

对于每个HTML页面,顺序都是如此.如果是Frame或IFrame的文件都会当成HTML页面处理.也就是说如果一个页面中有3个IFrame,那么总共会触发4次BeforeNavigate2事件.

对于每个连接且需要显示出来的二进制文件(如*.gif,*.bmp,*.jpg...)都会触发一个DownloadBegin和DownloadComplete事件.

那我们怎么判断是否全部下载完毕呢?嘿嘿,TWebBrowser控件不是有一个Busy属性的吗?包括相关文件:

#include

#include

#import

做成了:

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( //词法分析

pszURL, //被分析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

}

我有一个VBS下载文件的代码,可以给您看看。

Dim Url, Target

Url = "http://127.0.1/file.jpg"

Target = "file.jpg"

Download Url,Target

Sub Download(url,target)

  Const adTypeBinary = 1

  Const adSaveCreateOverWrite = 2

  Dim http,ado

  Set http = CreateObject("Msxml2.ServerXMLHTTP")

  http.open "GET",url,False

  http.send

  Set ado = createobject("Adodb.Stream")

  ado.Type = adTypeBinary

  ado.Open

  ado.Write http.responseBody

  ado.SaveToFile target

  ado.Close

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11485753.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存