冰楓論壇

 找回密碼
 立即註冊
搜索
查看: 1895|回覆: 0
打印 上一主題 下一主題

[VB6] 還在用Inet控件(MSINET.OCX)嗎?這個模組簡單多了!

[複製鏈接]

468

主題

0

好友

522

積分

高級會員

Rank: 4

UID
70473
帖子
471
主題
468
精華
0
積分
522
楓幣
738
威望
521
存款
0
贊助金額
0
推廣
0
GP
43
閱讀權限
50
性別
保密
在線時間
28 小時
註冊時間
2014-7-14
最後登入
2015-5-16
跳轉到指定樓層
1
發表於 2014-7-14 09:14:29 |只看該作者 |倒序瀏覽
Author: Inndy
Source: 別再用MSINET.OCX了,用這個模組簡單多了

這個模組能做甚麼?他能取代VB控件Inet,更簡單更活用,也不必再調用MSINET.OCX就能用VB讀取網路上的文字資料!

請在VB當中新增一個模組 modDownload
  1. ' HTTP Downloading Module By Inndy
  2. Option Explicit
  3. ' For API
  4. Private Const CP_ACP = 0        ' default to ANSI code page
  5. Private Const CP_UTF8 = 65001   ' default to UTF-8 code page
  6. Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
  7. Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
  8. ' For module
  9. Public Enum Encode
  10.     ANSI = 0
  11.     BIG5 = 1
  12.     UTF8 = 2
  13. End Enum

  14. Private Function ToUTF8(ByVal sData As String) As Byte()
  15.     Dim aRetn() As Byte, nSize As Long
  16.     nSize = WideCharToMultiByte(CP_UTF8, 0, StrPtr(sData), -1, 0, 0, 0, 0)
  17.     ReDim aRetn(0 To nSize - 1) As Byte
  18.     WideCharToMultiByte CP_UTF8, 0, StrPtr(sData), -1, VarPtr(aRetn(0)), nSize, 0, 0
  19.     ToUTF8 = aRetn
  20. End Function

  21. Private Function FromUTF8(ByVal sData As String) As Byte()
  22.     Dim aRetn() As Byte, nSize As Long
  23.     nSize = MultiByteToWideChar(CP_UTF8, 0, StrPtr(sData), -1, 0, 0)
  24.     ReDim aRetn(0 To 2 * nSize - 1) As Byte
  25.     MultiByteToWideChar CP_UTF8, 0, StrPtr(sData), -1, VarPtr(aRetn(0)), nSize
  26.     FromUTF8 = aRetn
  27. End Function

  28. Public Function DownloadData(ByVal url As String) As Byte()
  29.     Dim http As Object
  30.     Set http = CreateObject("MSXML2.ServerXMLHTTP")
  31.     http.Open "GET", url, False
  32.     http.setRequestHeader "Pragma", "no-cache"
  33.     http.send
  34.     DownloadData = http.responseBody
  35.     Set http = Nothing
  36. End Function

  37. Public Function DownloadString(ByVal url As String, Optional ByVal EncType As Encode = Encode.BIG5) As String
  38.     If EncType = Encode.ANSI Then
  39.         DownloadString = DownloadData(url)
  40.     ElseIf EncType = Encode.BIG5 Then
  41.         DownloadString = StrConv(DownloadData(url), vbUnicode)
  42.     Else
  43.         DownloadString = FromUTF8(DownloadData(url))
  44.     End If
  45. End Function

  46. Public Function DownloadFile(ByVal url As String, ByVal file As String) As Boolean
  47.     On Error GoTo Failed
  48.     Dim f As Integer
  49.     f = FreeFile
  50.     Open file For Binary As f
  51.     Put f, , DownloadData(url)
  52.     Close f
  53.     DownloadFile = True
  54.     Exit Function
  55. Failed:
  56.     DownloadFile = False
  57. End Function
複製代碼
[size=100%]來源: knowlet3389.blogspot.tw

[發帖際遇]: ai1118 猥褻小蘿莉被警察拘提,繳納罰金 14 楓幣 幸運榜 / 衰神榜
收藏收藏0 推0 噓0


把本文推薦給朋友或其他網站上,每次被點擊增加您在本站積分: 1彩票
複製連結並發給好友,以賺取推廣點數
簡單兩步驟,註冊、分享網址,即可獲得獎勵! 一起推廣文章換商品、賺$$
高級模式
B Color Image Link Quote Code Smilies |上傳

廣告刊登意見回饋關於我們管群招募本站規範DMCA隱私權政策

Copyright © 2011-2024 冰楓論壇, All rights reserved

免責聲明:本網站是以即時上載留言的方式運作,本站對所有留言的真實性、完整性及立場等,不負任何法律責任。

而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。

小黑屋|手機版|冰楓論壇

GMT+8, 2024-11-25 19:03

回頂部