vba 将 URL 内容直接下载到字符串 (VB6) 中,无需保存到磁盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10968946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:26:44  来源:igfitidea点击:

Download URL Contents Directly into String (VB6) WITHOUT Saving to Disk

apiwinapivbavb6wininet

提问by Erx_VB.NExT.Coder

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.

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

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.

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

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.

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

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?

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

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

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

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?

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

Update:

更新:

I have found this: http://www.freevbcode.com/ShowCode.asp?ID=1252however 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?

我发现了这个:http: //www.freevbcode.com/ShowCode.asp?ID =1252但是它说显示的函数可能不会返回整个页面和指向 Microsoft 错误报告或解释这一点的知识库文章的链接。另外,我知道这是基于 wininet.dll - 我想知道 WinInet.dll 与哪些版本的 Windows 一起打包?Windows XP 及更高版本?它是否随 Windows 7 和/或 Windows 8 一起提供?

回答by Antagony

This is how I did it with VB6 a few years ago:

几年前我是这样用 VB6 做的:

Private Function GetHTMLSource(ByVal sURL As String) As String
Dim xmlHttp As Object
    Set xmlHttp = CreateObject("MSXML2.XmlHttp")
    xmlHttp.Open "GET", sURL, False
    xmlHttp.send
    GetHTMLSource = xmlHttp.responseText
    Set xmlHttp = Nothing
End Function

回答by Jean-Fran?ois Corbett

One alternative is using Internet Explorer.

一种替代方法是使用 Internet Explorer。

Dim ex As InternetExplorer
Dim hd As HTMLDocument
Dim s As String

Set ex = New InternetExplorer

With ex
    .Navigate "http://donttrack.us/"
    .Visible = 1
    Set hd = .Document
    s = hd.body.innerText ' assuming you just want the text
    's = hd.body.innerHTML ' if you want the HTML
End With

EDIT: For the above early binding to work you need to set references to "Microsoft Internet Controls" and "Microsoft HTML Object Library" (Tools > References). You could also use late binding, but to be honest, I forget what the proper class names are; maybe someone smart will edit this answer :-)

编辑:要使上述早期绑定工作,您需要设置对“Microsoft Internet 控件”和“Microsoft HTML 对象库”(工具 > 参考)的引用。你也可以使用后期绑定,但老实说,我忘记了正确的类名是什么;也许聪明的人会编辑这个答案:-)

回答by Mark Bertenshaw

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.

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

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

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

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

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!

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