直接将URL内容下载到字符串(VB6)中,而无需保存到磁盘 [英] Download URL Contents Directly into String (VB6) WITHOUT Saving to Disk
问题描述
基本上,我想将特定URL的内容(基本上只是String形式的HTML代码)下载到我的VB6 String变量中.但是,有一些条件.
我了解URLDownloadToFile函数-但是,这要求您将下载的文件/HTML保存到磁盘上的文件位置,然后才能将其读入String变量,这对我来说不是一个选择,我也不想为此.
另一件事是,如果我需要使用外部库,则它必须已经从XP及更高版本的所有Windows版本附带,我不能使用需要运送,打包和分发的控件或库,即使它是免费的,这不是一个选择,我不想这样做.因此,我不能使用MSINET.OCX(Internet传输)控件的.OpenURL()函数(该函数只是将内容返回到String中),因为Windows并未提供它.
是否有办法使用Windows API,URLMON或Windows预装的或随Windows附带的其他东西,或者完全在VB6(SP6)中做到这一点?
如果是这样,我将对方向表示赞赏,因为即使经过一小时的搜索,我发现的唯一示例仍是对URLDownloadToFile(必须先保存到磁盘上才能放入字符串)和MsInet.OpenURL的引用(这要求我运送和分发MSINET.OCX,而我不能也不希望这样做.)
肯定有一种优雅的方法可以做到这一点吗?我可以在VB.NET中做到这一点而没有任何问题,但是显然在VB6中没有.NET框架的奢华-有任何想法吗?
更新:
我发现了这一点: http://www.freevbcode.com/ShowCode. asp?ID = 1252 但是它说显示的功能可能不会返回整个 页面以及指向Microsoft错误报告或kb文章的链接,这些文章或解释 这.另外,我知道这是基于wininet.dll的,而我 想知道WinInet.dll打包在Windows的哪个版本中 和? Windows XP和超过? Windows 7和/或Windows随附吗 8?
如果要使用纯VB而不使用IE,则可以利用VB UserControl的少量使用功能-异步属性.
创建一个新的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
要使用此控件,请将其粘贴在表单上并使用以下代码:
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对象返回!
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.
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.
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.
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?
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).
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?
Update:
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?
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.
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
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屋!