C语言怎么实现http下载文件功能

C语言怎么实现http下载文件功能,第1张

用c语言实现文件自动下载并且解压1.自动下载 http://abc.com/test.rar 也可以下载FTP://abc.com/test.rar2.下载完成后自动解压到本路径下相同的文件夹里面睁渣,激早李如:test.rar就是test文件夹3.如果test文明迟件夹已经存在并且里面有内容就直接覆盖4.软件运行的时候提示用户输入存储路径5.下载过程中显示下载进度6.一个好的编程会在适当的地方加上适当的注释以上功能我已经用wget+winrar的做成批处理可以运行了,但是希望用C语言来实现先送50分,如果实现了在加送50分,不希望吧分数留给系统

希望采纳

URLDownLoadToFile或URLDownloadToCacheFile,可以将文件下载到闷丛文件

例子:

URLDownloadToFile(NULL,/*URL*/, "C:\\1.dat", 0, NULL)

URLDownloadToCacheFile(NULL,

/*URL*/,

/*用档好于保存文件位置的字符数组*/,

/*上一蚂蠢樱个参数的大小*/,NULL,NULL)

用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

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存