使用 .NET FrameWork 的 System.Net.WebClient 实现下载

2024-02-27 08:13:21 浏览数 (1)

关于异步下载,使用 XMLHttp也好,WINHttp也好,都有成功案例。这里介绍另一种技术:使用 .NET FrameWork 的 System.Net.WebClient 实现下载。

这里借助的第三方为开源的 wwDotNetBridge。需要中文版详细文档的可以直接联系本文作者/译者,收费提供。

VFP可以调用的.NET--加菲猫注

示例1:同步下载:

代码语言:javascript复制

CLEAR
DO wwDotNetBridge
Public loBridge as wwDotNetBridge
loBridge = CreateObject("wwDotNetBridge","V4")
loClient = loBridge.CreateInstance("System.Net.WebClient")
lcUrl = "下载的 URL 地址"
lcLocalPath = "带有完整路径的文件名"
loClient.DownloadFile(lcUrl, lcLocalPath)

示例2:“伪”异步下载

代码语言:javascript复制
CLEAR
DO wwDotNetBridge
Public loBridge as wwDotNetBridge
loBridge = CreateObject("wwDotNetBridge","V4)
loClient = loBridge.CreateInstance("System.Net.WebClient")
PUBLIC loCallback
loCallback = CREATEOBJECT("MyCallback")
lcUrl = "下载的 URL 地址"
lcLocalPath = "带有完整路径的文件名"
loBridge.InvokeMethodAsync(loCallback,loClient,"DownloadFile",lcUrl, lcLocalPath)
DOEVENTS
Read EVENTS
loClient.Dispose()

DEFINE CLASS MyCallback as AsyncCallbackEvents
    FUNCTION OnCompleted(lvResult,lcMethod)
        ? "Success: "   lcMethod,lvResult
        Clear Events
    ENDFUNC
    FUNCTION OnError(lcMessage,loException,lcMethod)
        ? "Error: "   lcMethod,lcMessage
        Clear Events
    ENDFUNC
ENDDEFINE

示例3:异步下载

代码语言:javascript复制
CLEAR
DO wwDotNetBridge
Public loBridge as wwDotNetBridge
loBridge = CreateObject("wwDotNetBridge","V4")  
loClient = loBridge.CreateInstance("System.Net.WebClient")
loHandler = CREATEOBJECT("DownloadEventHandler")
loSubscription = loBridge.SubscribeToEvents(loClient, loHandler)  
lcUrl = "下载的 URL 地址"
loUrl = loBridge.CreateInstance("System.Uri", lcUrl)
lcLocalPath = "带有完整路径的文件名"
loClient.DownloadFileAsync(loUrl, lcLocalPath)
DOEVENTS
Read EVENTS
loSubscription.Unsubscribe()
loClient.Dispose()

DEFINE CLASS DownloadEventHandler as Custom
    FUNCTION OnDownloadProgressChanged(sender, e)
        ? "下载进度:"   TRANS(loBridge.GetProperty(e,"ProgressPercentage"))   "%"
    ENDFUNC
    FUNCTION OnDownloadFileCompleted(sender, e)
         ? "下载成功" 
        Clear Events
    ENDFUNC
ENDDEFINE

0 人点赞