将 URL 内容直接下载为字符串 (VB6),无需保存到磁盘 [英] Download URL Contents Directly into String (VB6) WITHOUT Saving to Disk

查看:21
本文介绍了将 URL 内容直接下载为字符串 (VB6),无需保存到磁盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想将特定 URL 的内容(基本上,只是字符串形式的 HTML 代码)下载到我的 VB6 字符串变量中.但是,有一些条件.

Basically, I want to download the contents of a particular URL (basically, just HTML codes in the form of a String) into my VB6 String variable. However, there are some conditions.

我知道 URLDownloadToFile 函数 - 但是,这要求您将下载的文件/HTML 保存到磁盘上的文件位置,然后才能将其读入字符串变量,这对我来说不是一个选项,我不想要这样做.

I know about the URLDownloadToFile Function - however, this requires that you save the downloaded file/HTML onto a file location on disk before you can read it into a String variable, this is not an option for me and I do not want to do this.

另一件事是,如果我需要使用外部库,它必须已经与 XP 及以后的所有版本的 Windows 一起提供,我不能使用我需要运送、打包和分发的控件或库,即使它是免费的,这不是一种选择,我不想这样做.因此,我不能使用 MSINET.OCX(互联网传输)控件的 .OpenURL() 函数(它只是将内容返回到字符串中),因为它不随 Windows 提供.

The other thing is, if I need to use an external library, it must already come with all versions of Windows from XP and onwards, I cannot use a control or library that I am required to ship, package and distribute even if it is free, this is not an option and I do not want to do this. So, I cannot use the MSINET.OCX (Internet Transfer) Control's .OpenURL() function (which simply returns contents into a String), as it does not come with Windows.

有没有一种方法可以使用 Windows API、URLMON 或 Windows 中预加载或附带的其他东西来做到这一点,或者完全在 VB6 (SP6) 中做到这一点?

Is there a way to be able to do this with the Windows API, URLMON or something else that is pre-loaded into or comes with Windows, or a way to do it in VB6 (SP6) entirely?

如果是这样,我会很感激指导,因为即使经过一小时的谷歌搜索,我发现的唯一例子是对 URLDownloadToFile 的引用(这需要在将 ale 放入字符串之前保存在磁盘上)和 MsInet.OpenURL(这要求我发布和分发 MSINET.OCX,而我不能也不想这样做).

If so, I would appreciate direction, because even after one hour of googling, the only examples I've found are references to URLDownloadToFile (which requires saving on disk before being ale to place into a String) and MsInet.OpenURL (which requires that I ship and distribute MSINET.OCX, which I cannot and don't want to do).

当然必须有一种优雅的方式才能做到这一点?我可以在 VB.NET 中毫无问题地做到这一点,但显然没有 VB6 中的 .NET 框架的奢华 - 有什么想法吗?

Surely there has got to be an elegant way to be able to do this? I can do it in VB.NET without an issue, but obviously don't have the luxury of the .NET framework in VB6 - any ideas?

更新:

我发现了这个:http://www.freevbcode.com/ShowCode.asp?ID=1252但是它说显示的函数可能不会返回整个页面和指向 Microsoft 错误报告或知识库文章解释的链接这个.另外,我知道这是基于 wininet.dll - 而我想知道 WinInet.dll 打包了哪些版本的 Windows和?视窗 XP &超过?是否附带 Windows 7 和/或 Windows8?

I have found this: http://www.freevbcode.com/ShowCode.asp?ID=1252 however it says that the displayed function may not return the entire page and links to a Microsoft bug report or kb article explaining this. Also, I understand this is based off wininet.dll - and I'm wondering which versions of Windows does WinInet.dll come packaged with? Windows XP & beyond? Does it come with Windows 7 and/or Windows 8?

推荐答案

如果你想用纯 VB 来做到这一点,而不是 IE,那么你可以利用 VB UserControl 的一个很少使用的功能 - 异步属性.

If you want to do this with pure VB, and no IE, then you can take advantage of a little-used features of the VB UserControl - async properties.

创建一个新的 UserControl,并将其命名为 UrlDownloader.将 InvisibleAtRuntime 属性设置为 True.添加如下代码:

Create a new UserControl, and call it something like UrlDownloader. Set the InvisibleAtRuntime property to True. Add the following code to it:

Option Explicit

Private Const m_ksProp_Data         As String = "Data"

Private m_bAsync                    As Boolean
Private m_sURL                      As String

Public Event AsyncReadProgress(ByRef the_abytData() As Byte)
Public Event AsyncReadComplete(ByRef the_abytData() As Byte)

Public Property Let Async(ByVal the_bValue As Boolean)
    m_bAsync = the_bValue
End Property

Public Property Get Async() As Boolean
    Async = m_bAsync
End Property

Public Property Let URL(ByVal the_sValue As String)
    m_sURL = the_sValue
End Property

Public Property Get URL() As String
    URL = m_sURL
End Property

Public Sub Download()

    UserControl.AsyncRead m_sURL, vbAsyncTypeByteArray, m_ksProp_Data, IIf(m_bAsync, 0&, vbAsyncReadSynchronousDownload)

End Sub

Private Sub UserControl_AsyncReadComplete(AsyncProp As AsyncProperty)

    If AsyncProp.PropertyName = m_ksProp_Data Then
        RaiseEvent AsyncReadComplete(AsyncProp.Value)
    End If

End Sub

Private Sub UserControl_AsyncReadProgress(AsyncProp As AsyncProperty)

    If AsyncProp.PropertyName = m_ksProp_Data Then
        Select Case AsyncProp.StatusCode
        Case vbAsyncStatusCodeBeginDownloadData, vbAsyncStatusCodeDownloadingData, vbAsyncStatusCodeEndDownloadData
            RaiseEvent AsyncReadProgress(AsyncProp.Value)
        End Select
    End If

End Sub

要使用此控件,请将其粘贴在表单上并使用以下代码:

To use this control, stick it on a form and use the following code:

Option Explicit

Private Sub Command1_Click()

    XDownload1.Async = False
    XDownload1.URL = "http://www.google.co.uk"
    XDownload1.Download

End Sub

Private Sub XDownload1_AsyncReadProgress(the_abytData() As Byte)

    Debug.Print StrConv(the_abytData(), vbUnicode)

End Sub

我只想说,您可以根据自己的喜好自定义此内容.它可以告诉(使用 AyncProp 对象)文件是否被缓存,以及其他有用的信息.它甚至还有一种特殊模式,您可以在其中下载 GIF、JPG 和 BMP 文件并将它们作为 StdPicture 对象返回!

Suffice to say, you can customise this to your hearts content. It can tell (using the AyncProp object) whether the file is cached, and other useful information. It even has a special mode in which you can download GIF, JPG and BMP files and return them as a StdPicture object!

这篇关于将 URL 内容直接下载为字符串 (VB6),无需保存到磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆