1简介编辑
指从指定URL地址读取内容并将读取到的内容保存到特定的文件里,下面列举了几种常见编程语言的实现方法。
2Delphi编辑
⒈uses UrlMon;
⒉//-------------------------------------------------------------------------------
⒊
functionDownloadFile(SourceFile, DestFile: string): Boolean;
⒋// 参数 下载文件路径, 保存文件路径
⒌// 返回 下载结果
⒍
begin
⒎
try
⒏ Result := UrlDownloadToFile(nil, PChar(SourceFile), PChar(DestFile), 0, nil) = 0;
⒐
except
⒑ Result := False;
⒒
end;
⒓
end;
函数调用:
⒈
var
⒉ url,destFile:
string;
⒊url := 'http****filename.rar'
⒋destFile := 'E:\filename.rar'
⒌
ifDownloadFile(url, DestFile)
then
⒍
begin
⒎ ShowMessage('保存成功!');
⒏
end
⒐
else
⒑
begin
⒒ ShowMessage('保存不成功!');
⒓
end;
3C++编辑
Syntax:
1
2
3
4
5
6
7
|
HRESULT
URLDownloadToFile(
LPUNKNOWN pCaller,
LPCTSTR
szURL,
LPCTSTR
szFileName,
DWORD
dwReserved,
LPBINDSTATUSCALLBACK lpfnCB
);
|
Parameters:
pCallerPointer to the controlling IUnknown interface of the calling Microsoft ActiveX component (if the caller is an ActiveX component). //控件的接口,如果不是控件则为0.
szURL
Pointer to a string value containing the URL to be downloaded. Cannot be set to NULL
//要下载的url地址,不能为空.
szFileName
Pointer to a string value containing the name of the file to create for bits that come from the download.
//下载后保存的文件名.
dwReserved
Reserved. Must be set to 0.
//保留字段,必需为0
lpfnCB
Pointer to the caller's IBindStatusCallback interface. URLDownloadToFile calls this interface's IBindStatusCallback::OnProgress method on a connection activity, including the arrival of data. IBindStatusCallback::OnDataAvailable is never called.
//下载进度状态回调
Return Value
Returns one of the following values.
S_OK : The download started successfully.
E_OUTOFMEMORY: The buffer length is invalid, or there is insufficient memory to complete the operation.
INET_E_DOWNLOAD_FAILURE:The specified resource or callback interface was invalid.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include "stdafx.h"
#include <iostream>
#include <UrlMon.h>
#pragma comment(lib, "urlmon.lib")
using
namespace
std;
int
_tmain(
int
argc, _TCHAR* argv[])
{
HRESULT
hr = URLDownloadToFile(0,_T(
"***ui.the9.**/wshell/WShell.exe"
),
_T (
"D:\\WShell.exe"
), 0,NULL);
if
(hr== S_OK)
{
cout<<
"ok"
<< endl;
}
return
0;
}
|
4vb编辑
该函数是VB的一个用来调用API进行网络下载的函数,过程如下:
后台下载
声明:
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
'//调用
然后在代码里直接调用:
Call URLDownloadToFile(0, "网页地址", "保存到本地文件名和地址", 0, 0)
如
Call URLDownloadToFile(0, "*****.baidu**/", "c:\1.htm", 0, 0)
相当于另存为
声明:
Private Declare Function DoFileDownload Lib "shdocvw.dll"(ByVal lpszFile As String) As Long
'//调用
然后在代码里直接调用:
Dim gourl As String
gourl = StrConv("网页地址", vbUnicode)
Call DoFileDownload(gourl)