从网页通过inet控件的Inet1.GetChunk读取其代码,然后想转换为汉字,转换代码如下
Private Sub Inet1_StateChanged(ByVal State As Integer)
Dim strRecText As String
Dim strBuffer As String
Select Case State
Case 1
StatusBar1.Panels(2) = "正在查询主机的IP"
Case 2
StatusBar1.Panels(2) = "成功找到主机的IP"
Case 3
StatusBar1.Panels(2) = "正在于主机链接"
Case 4
StatusBar1.Panels(2) = "与主机链接成功"
Case 5
StatusBar1.Panels(2) = "正在向主机传送命令"
Case 6
StatusBar1.Panels(2) = "成功向主机传送命令"
Case 7
StatusBar1.Panels(2) = "正在接收主机的响应"
Case 8
StatusBar1.Panels(2) = "成功接收主机的响应"
Case 11
StatusBar1.Panels(2) = "超时"
Case 12
Do
strBuffer = Inet1.GetChunk(10240)
strRecText = strRecText & strBuffer
Loop Until Len(strBuffer) = 0
'Print #3, strRecText
'此次开始调用转换函数(utf8编码转为汉字)
str = BytesToBstr(StrRecData, "utf-8")
’处理转换后的代码
Call WrRecToHFLex(str)
Case Else
StatusBar1.Panels(2) = "其他情况!!"
End Select
End Sub’转换函数Function BytesToBstr(strBody, CodeBase)
Dim objStream
Set objStream = CreateObject("Adodb.Stream")
objStream.Type = 1
objStream.Mode = 3
objStream.Open
'objStream.LoadFromFile strBody
If Len(strBody) <> 0 Then objStream.Write (strBody)
objStream.Position = 0
objStream.Type = 2
objStream.Charset = CodeBase
BytesToBstr = objStream.ReadText
objStream.Close
Set objStream = Nothing
End Function运行后出现实时错误:“实时错误'3001': 参数类型不正确,或不在可接受范围之内,或与其他参数冲突。”,按调试,就是红色字哪里出错(If Len(strBody) <> 0 Then objStream.Write (strBody))这里出错,请各位高手指点一下,错在哪里,我查了网上很多资料,很多都出现了这个错误,但没有被根本解决;我也尝试把这段代码改为:If Len(strBody) <> 0 Then objStream.WriteText (strBody)但出现错误为“实时错误'3219':在此环境中不允许操作;小弟刚入行,不懂这些这么高深的用法,请路过大侠帮帮我啊,在此谢过,并在线等
Private Sub Inet1_StateChanged(ByVal State As Integer)
Dim strRecText As String
Dim strBuffer As String
Select Case State
Case 1
StatusBar1.Panels(2) = "正在查询主机的IP"
Case 2
StatusBar1.Panels(2) = "成功找到主机的IP"
Case 3
StatusBar1.Panels(2) = "正在于主机链接"
Case 4
StatusBar1.Panels(2) = "与主机链接成功"
Case 5
StatusBar1.Panels(2) = "正在向主机传送命令"
Case 6
StatusBar1.Panels(2) = "成功向主机传送命令"
Case 7
StatusBar1.Panels(2) = "正在接收主机的响应"
Case 8
StatusBar1.Panels(2) = "成功接收主机的响应"
Case 11
StatusBar1.Panels(2) = "超时"
Case 12
Do
strBuffer = Inet1.GetChunk(10240)
strRecText = strRecText & strBuffer
Loop Until Len(strBuffer) = 0
'Print #3, strRecText
'此次开始调用转换函数(utf8编码转为汉字)
str = BytesToBstr(StrRecData, "utf-8")
’处理转换后的代码
Call WrRecToHFLex(str)
Case Else
StatusBar1.Panels(2) = "其他情况!!"
End Select
End Sub’转换函数Function BytesToBstr(strBody, CodeBase)
Dim objStream
Set objStream = CreateObject("Adodb.Stream")
objStream.Type = 1
objStream.Mode = 3
objStream.Open
'objStream.LoadFromFile strBody
If Len(strBody) <> 0 Then objStream.Write (strBody)
objStream.Position = 0
objStream.Type = 2
objStream.Charset = CodeBase
BytesToBstr = objStream.ReadText
objStream.Close
Set objStream = Nothing
End Function运行后出现实时错误:“实时错误'3001': 参数类型不正确,或不在可接受范围之内,或与其他参数冲突。”,按调试,就是红色字哪里出错(If Len(strBody) <> 0 Then objStream.Write (strBody))这里出错,请各位高手指点一下,错在哪里,我查了网上很多资料,很多都出现了这个错误,但没有被根本解决;我也尝试把这段代码改为:If Len(strBody) <> 0 Then objStream.WriteText (strBody)但出现错误为“实时错误'3219':在此环境中不允许操作;小弟刚入行,不懂这些这么高深的用法,请路过大侠帮帮我啊,在此谢过,并在线等
objStream.Position = 0
objStream.Type = 2
objStream.Charset = CodeBase 没写过这样的程序,感觉你这里的顺序代码顺序可能存在顺序问题。
--Function ReadTextFile (FilePath,CharSet)
dim str,stm
set stm=server.CreateObject("adodb.stream")
stm.Type=2 '以文本模式读取
stm.mode=3
stm.charset=CharSet
stm.open
stm.loadfromfile FilePath
str=stm.readtext
stm.Close
set stm=nothing
ReadTextFile=str
End functionSub WriteTextFile (FilePath,Str,CharSet)
dim stm
set stm=server.CreateObject("adodb.stream")
stm.Type=2 '以文本模式读取
stm.mode=3
stm.charset=CharSet
stm.open
stm.WriteText str
stm.SaveToFile FilePath,2
stm.flush
stm.Close
set stm=nothing
End Sub去网上看了下,你的代码顺序是有问题。
2、objStream.Type=1和objStream.Write配合使用,objStream.Type = 2和objStream.WriteText配合使用,你光把objStream.Write改成objStream.WriteText但没修改objStream.Type当然还是要报错
Option ExplicitPrivate Sub Command1_Click()
Call Inet1.Execute("http://localhost/newjygl/public/login.aspx")
End SubFunction BytesToBstr(ByRef strBody As String, ByVal CodeBase As String)
strBody = StrConv(strBody, vbFromUnicode)
Dim objStream As New ADODB.Stream objStream.Type = adTypeText
objStream.Mode = adModeReadWrite
objStream.Open If Len(strBody) <> 0 Then
objStream.WriteText strBody
End If objStream.Position = 0
objStream.Type = adTypeText
objStream.Charset = CodeBase BytesToBstr = objStream.ReadText objStream.Close
Set objStream = Nothing
End FunctionPrivate Sub Inet1_StateChanged(ByVal State As Integer)
Select Case State
Case 12
Dim strBuffer As String
Dim strRecText As String
Do
strBuffer = Inet1.GetChunk(10240)
strRecText = strRecText & strBuffer
Loop Until Len(strBuffer) = 0
strRecText = BytesToBstr(strRecText, "utf-8")
End Select
End Sub剩下的自己改改把。
Call Inet1.Execute("http://localhost/newjygl/public/login.aspx")
End SubFunction BytesToBstr(ByVal strBody As String, ByVal CodeBase As String)
strBody = StrConv(strBody, vbFromUnicode)
Dim objStream As New ADODB.Stream objStream.Type = adTypeText
objStream.Mode = adModeReadWrite
objStream.Open If Len(strBody) <> 0 Then
objStream.WriteText strBody
End If objStream.Position = 0
objStream.Type = adTypeText
objStream.Charset = CodeBase BytesToBstr = objStream.ReadText objStream.Close
Set objStream = Nothing
End FunctionPrivate Sub Inet1_StateChanged(ByVal State As Integer)
Select Case State
Case 12
Dim strBuffer As String
Dim strRecText As String
Do
strBuffer = Inet1.GetChunk(10240)
strRecText = strRecText & strBuffer
Loop Until Len(strBuffer) = 0
strRecText = BytesToBstr(strRecText, "utf-8")
End Select
End Sub
Function GetChunk(Size As Long, [DataType])
这个是Inet的GetChunk方法的定义,第二个[DataType]参数有两个值icByteArray和icString,默认是icString,要解决这个问题把第二个参数改成icByteArray就可以了。
Inet1.Execute "http://ask.koubei.com/question/1306083100901.html"
End SubFunction BytesToBstr(ByRef arrBody() As Byte, ByVal CodeBase As String) As String
'无数据
If UBound(arrBody) = 0 Then
BytesToBstr = ""
Exit Function
End If
Dim objStream As New ADODB.Stream objStream.Type = adTypeBinary
objStream.Mode = adModeReadWrite
objStream.Open
objStream.Write arrBody objStream.Position = 0
objStream.Type = adTypeText
objStream.Charset = CodeBase
BytesToBstr = objStream.ReadText
objStream.Close
Set objStream = Nothing
End FunctionPrivate Sub Inet1_StateChanged(ByVal State As Integer)
Select Case State
Case 12
Dim arrBuffer() As Byte '累计接收的数据
Dim tmpBuffer() As Byte '单次GetChunk获得的数据
Dim recCount As Long '累计接收的字节数
Dim strRecText As String
tmpBuffer = Inet1.GetChunk(1024, icByteArray)
recCount = UBound(tmpBuffer)
Do
ReDim Preserve arrBuffer(recCount + UBound(tmpBuffer))
CopyMemory arrBuffer(recCount), tmpBuffer(0), UBound(tmpBuffer)
recCount = recCount + UBound(tmpBuffer)
tmpBuffer = Inet1.GetChunk(1024, icByteArray)
Loop Until UBound(tmpBuffer) < 0
strRecText = BytesToBstr(arrBuffer, "utf-8")
End Select
End Sub参考这个改改吧,希望Yahoo的同志不要找我麻烦,我拿它们的网站做测试
Option ExplicitPrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)Private Sub Command1_Click()
Inet1.Execute "http://ask.koubei.com/question/1306083100901.html"
End SubFunction BytesToBstr(ByRef arrBody() As Byte, ByVal CodeBase As String) As String
'无数据
If UBound(arrBody) = 0 Then
BytesToBstr = ""
Exit Function
End If
Dim objStream As New ADODB.Stream objStream.Type = adTypeBinary
objStream.Mode = adModeReadWrite
objStream.Open
objStream.Write arrBody objStream.Position = 0
objStream.Type = adTypeText
objStream.Charset = CodeBase
BytesToBstr = objStream.ReadText
objStream.Close
Set objStream = Nothing
End FunctionPrivate Sub Inet1_StateChanged(ByVal State As Integer)
Select Case State
Case 12
Dim arrBuffer() As Byte '累计接收的数据
Dim tmpBuffer() As Byte '单次GetChunk获得的数据
Dim recCount As Long '累计接收的字节数
Dim strRecText As String
recCount = 0
tmpBuffer = Inet1.GetChunk(1024, icByteArray)
Do
ReDim Preserve arrBuffer(recCount + UBound(tmpBuffer))
CopyMemory arrBuffer(recCount), tmpBuffer(0), UBound(tmpBuffer)
recCount = recCount + UBound(tmpBuffer)
tmpBuffer = Inet1.GetChunk(1024, icByteArray)
Loop Until UBound(tmpBuffer) < 0
strRecText = BytesToBstr(arrBuffer, "utf-8")
End Select
End Sub