VBWininet实现文件下载与网页获取函数 vb编程实现菱形的输出

'Form'写入文件Private Sub WriteFile(ByteArray() As Byte, Path AsString) Open Path For Binary As#1 Put #1, ,ByteArray() Close #1End Sub
'网页编码转换Public Function BytesToBstr(Bytes, Optional Charset AsString) Dim objstream AsObject Set objstream =CreateObject("ADODB.Stream") With objstream .Type = 1 .Mode = 3 .Open .Write Bytes .Position = 0 .Type = 2 .Charset = Charset BytesToBstr = .ReadText .Close End WithEnd Function
Private Sub Command1_Click() '下载文件 CallWriteFile(HttpDownload("http://www.baidu.com/img/baidu_jgylogo3.gif"),"c:123.gif") '获取网页源码 Debug.PrintBytesToBstr(HttpDownload("http://www.soso.com/"), "GB2312")End Sub

VBWininet实现文件下载与网页获取函数 vb编程实现菱形的输出
'Mode--------------------------Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0Private Const INTERNET_OPEN_TYPE_DIRECT = 1Private Const INTERNET_OPEN_TYPE_PROXY = 3Private Const scUserAgent = "Microsoft Internet Explorer6.0"Private Const INTERNET_FLAG_RELOAD = &H80000000Private Declare Function InternetOpen Lib "wininet.dll" Alias"InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long,ByVal sProxyName As String, ByVal sProxyBypass As String, ByVallFlags As Long) As LongPrivate Declare Function InternetOpenUrl Lib "wininet.dll"Alias "InternetOpenUrlA" (ByVal hOpen As Long, ByVal sUrl AsString, ByVal sHeaders As String, ByVal lLength As Long, ByVallFlags As Long, ByVal lContext As Long) As LongPrivate Declare Function InternetReadFileByte Lib"wininet.dll" Alias "InternetReadFile" (ByVal hFile As Long, ByRefsBuffer As Byte, ByVal lNumBytesToRead As Long, lNumberOfBytesReadAs Long) As IntegerPrivate Declare Function InternetCloseHandle Lib "wininet.dll"(ByVal hInet As Long) As IntegerPrivate Declare Function HttpQueryInfo Lib "wininet.dll" Alias"HttpQueryInfoA" (ByVal hHttpRequest As Long, ByVal lInfoLevel AsLong, ByVal sBuffer As Any, ByRef lBufferLength As Long, ByReflIndex As Long) As IntegerConst HTTP_QUERY_CONTENT_LENGTH = 5Const HTTP_QUERY_FLAG_NUMBER = &H20000000Private Declare Function InternetReadFile Lib "wininet.dll"(ByVal hFile As Long, ByVal sBuffer As String, ByVallNumBytesToRead As Long, lNumberOfBytesRead As Long) AsIntegerPublic Declare Sub CopyMemory Lib "kernel32" Alias"RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length AsLong)Private Function IsNullBytes(ByRef sBytes() As Byte) AsBoolean On Error ResumeNext Dim N As Long N =UBound(sBytes()) If Err Then IsNullBytes = True End IfEnd FunctionPublic Function HttpDownload(ByVal sUrl As String, OptionalByVal lNewBufferSize As Long = -1) As Byte() Dim bBuffer() AsByte Dim lBufferSize AsLong Dim retBytes() AsByte Dim hOpen As Long Dim hOpenUrl AsLong Dim hQuery As Long Dim lFileSize AsLong Dim sQuery AsString Dim i As Long Dim lBufferNumber AsLong Dim lRealFileLen AsLong Dim bDoLoop AsBoolean Dim lNumberOfBytesReadAs Long Dim BSize As Long On Error GoToFindErr If lNewBufferSize = -1Then lBufferSize = 2048 Else lBufferSize = lNewBufferSize If lBufferSize < 1024 Then lBufferSize =1024 End If ReDimbBuffer(lBufferSize - 1) As Byte hOpen =InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG,vbNullString, vbNullString, 0) hOpenUrl =InternetOpenUrl(hOpen, sUrl, vbNullString, 0, INTERNET_FLAG_RELOAD,0) sQuery = String$(1024, "") hQuery =HttpQueryInfo(hOpenUrl, HTTP_QUERY_CONTENT_LENGTH, ByVal sQuery,Len(sQuery), 0) If hQuery Then lFileSize = CLng(Trim(sQuery)) Else lFileSize = -1 End If If lFileSize <> -1Then bDoLoop = True lBufferNumber = Fix(lFileSize /lBufferSize) If lFileSize Mod lBufferSize <> 0 ThenlBufferNumber = lBufferNumber + 1 lRealFileLen = 0 For i = 1 To lBufferNumber If i <lBufferNumber Then bDoLoop =InternetReadFileByte(hOpenUrl, bBuffer(0), lBufferSize,lNumberOfBytesRead) Else lBufferSize = lFileSize -lBufferSize * (i - 1) ReDim bBuffer(lBufferSize -1) As Byte bDoLoop =InternetReadFileByte(hOpenUrl, bBuffer(0), lBufferSize,lNumberOfBytesRead) EndIf IfIsNullBytes(retBytes) Then ReDimretBytes(UBound(bBuffer)) retBytes = bBuffer Else BSize =UBound(retBytes) ReDim Preserve retBytes(BSize+ UBound(bBuffer) + 1) CallCopyMemory(retBytes(BSize + 1), bBuffer(0), UBound(bBuffer) +1) EndIf lRealFileLen = lRealFileLen + lNumberOfBytesRead If NotCBool(lNumberOfBytesRead) Then Exit For Next i Else i = 0 Do i = i +1 bDoLoop =InternetReadFileByte(hOpenUrl, bBuffer(0), lBufferSize,lNumberOfBytesRead) IflBufferSize <> lNumberOfBytesRead Then If lNumberOfBytesRead = 0 OrbDoLoop = 0 Then Exit Do Else lBufferSize = lNumberOfBytesRead ReDim Preserve bBuffer(lBufferSize - 1) AsByte End If EndIf IfIsNullBytes(retBytes) Then ReDimretBytes(UBound(bBuffer)) retBytes = bBuffer Else BSize =UBound(retBytes) ReDim Preserve retBytes(BSize+ UBound(bBuffer) + 1) CallCopyMemory(retBytes(BSize + 1), bBuffer(0), UBound(bBuffer) +1) EndIf lRealFileLen = lRealFileLen + lNumberOfBytesRead Loop End If If hOpenUrl <> 0Then InternetCloseHandle (hOpenUrl) If hOpen <> 0 ThenInternetCloseHandle (hOpen) HttpDownload =retBytes Exit FunctionFindErr: HttpDownload =VBA.vbNullCharEnd Function

  

爱华网本文地址 » http://www.aihuau.com/a/25101010/37547.html

更多阅读

BT文件下载文件名中包含违规内容怎么办? 文件名包含违规

BT文件下载文件名中包含违规内容怎么办?——简介最近在网上找到一些种子想用同学给我的VIP迅雷号来下载,可是老是提示“文件名中包含违规内容...”,怎么办呢?我这就教你怎么解决。BT文件下载文件名中包含违规内容怎么办?——工具/原料

转载 与神对话txt下载 与神对话pdf下载

原文地址:与神对话txt下载作者:成功无止境内容简介《与神对话》以“我”和虚构的“神”展开对话的形式出现,沃尔什很巧妙地安排“我”提出了许多令人困扰的问题,并通过“神”来予以回答。作者在书中提出了各种关于生活的真知灼见,以及

声明:《VBWininet实现文件下载与网页获取函数 vb编程实现菱形的输出》为网友少年狂妄分享!如侵犯到您的合法权益请联系我们删除