Inet 控件支持 HTTP 与 FTP 两种通讯协议。利用这个控件可以完成许多功能。
我们通过例子来看看。
环境 VB6+WINXP
打开 VB6, 新建工程
添加部件 Microsoft Internet Transfer Controls.
在 form 中添加 2 个按钮, 2 个文本框和 Inet 控件
代码如下 :
Option Explicit
' 这段代码使用了 GetHeader 来返回页面信息,比较准确一些
' 可以得到文件最后修改日期 , 文件大小等等
' 用这个办法还可以判断一个文件是否存在
Private Sub Command1_Click()
Dim a As String
Dim str As String
Dim RetCode As Long
Inet1.OpenURL "http://localhost/xml/tt.htm"
If Inet1.StillExecuting Then
DoEvents
End If
' 可以看到所有的项目
MsgBox Inet1.GetHeader
' 得到修改日期时间是格林时间 , 将它转换北京时间
str = Inet1.GetHeader("Last-modified")
str = Replace(Right(str, Len(str) - InStr(1, str, ",") - 1), "GMT", "")
Text1.Text = CDate(Format(str, "yyyy/mm/dd hh:mm:ss"))
MsgBox Inet1.GetHeader("content-length")
RetCode = Val(Mid(Trim(Inet1.GetHeader), 10, 3))
Select Case RetCode
Case 200
MsgBox " 成功 "
Case 404
MsgBox " 没有发现 "
Case Else
MsgBox "Error"
End Select
End Sub
' 这段代码简单的判断了是否与 internet 连接
' 如果连接,得到网页源码并且保存
Private Sub Command2_Click()
Inet1.Cancel
If Len(Inet1.OpenURL("http://localhost/xml\tt.htm")) <> 0 Then
MsgBox " 已经连接 "
Text2.Text = Inet1.OpenURL("http://localhost/xml\tt.htm")
If Inet1.StillExecuting Then
DoEvents
End If
' 保存到文件
Open App.Path & "\index.htm" For Output As #1
Print #1, Text2.Text
Close #1
Else
MsgBox " 没有连接 "
End If
End Sub.