冰楓論壇

標題: 別再用MSINET.OCX了,用這個模組簡單多了 [打印本頁]

作者: Inndy    時間: 2013-4-15 11:29
標題: 別再用MSINET.OCX了,用這個模組簡單多了
  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
複製代碼

作者: 小神仙    時間: 2013-4-15 12:27
這是拿來做什麼的?
作者: zack2213330    時間: 2013-4-15 17:56
謝謝Inndy大大^^
作者: a0926206677    時間: 2013-4-15 22:16
MSINET.OCX感覺很眼熟
可是還是看不懂
作者: marianne980    時間: 2013-4-16 21:15
我只知道 有些人的電腦開掛 它會告訴你缺少ocx
我只會把補丁給它們去執行
不知道這個東西該丟進去哪裡?
或許又是不懂程式語言無法理解的東西@@
還是感謝i大的分享歐~~
作者: zxc199450    時間: 2013-4-17 00:18
MSINET.OCX 是什麼?
這模組又該怎麼用?
作者: david19970722    時間: 2014-4-24 18:09
剛接觸 上面寫的有點難懂 不過感恩




歡迎光臨 冰楓論壇 (https://bingfong.com/) Powered by 冰楓