 |
在工程中加入internet tracefer control控件。
Dim strURL As String, strFormData As String
strURL = "http://www.mmailto.com/SendMessage.asp"
strFormData = "name=yournamr&no=yourno"
Inet1.Execute strURL, "Post", strFormData, Content
YunHai的意见:
看看下面的文章或许对您会有所帮助
http://yunhai.blogchina.com/blog/article_21034.94393.html
VB使用Inet Post数据
作者:李云海 E-Mail:yun_hai@hotmail.com
欢迎转载,转载请保证文章完成性和注明出处 谢谢您的合作。
好久不用VB写东西了,最近在网络公司作web程序,刚作完两个工程在想起了好久没去过的小辉论坛,看到有人问关于vb post数据的问题。增经我也是那里的斑竹的^_^好了废话少说咱们转入正题。
曾经我用VB的时候也曾用Internet Transfer 控件 Post过数据但是失败了,在论坛发帖子也得不到正确的答案,那时郁闷了好久。最近自己想搞这方面的程序开发所以作了一点研究。
我想post失败的原因是因为execute的第四个参数requestHeaders需要重新建立,默认的参数可能不全面。我作了个例子可以成功Post数据
frm文件代码如下(要在工程中添加MSINET.OCX控件):
VERSION 5.00
Object = "{48E59290-9880-11CF-9754-00AA00C00908}#1.0#0"; "MSINET.OCX"
Begin VB.Form frmMain
Caption = "PostTest"
ClientHeight = 5430
ClientLeft = 60
ClientTop = 345
ClientWidth = 9810
LinkTopic = "Form1"
ScaleHeight = 5430
ScaleWidth = 9810
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox txtCookie
Height = 300
Left = 840
MaxLength = 255
TabIndex = 7
Top = 1080
Width = 7215
End
Begin InetCtlsObjects.Inet InetHttp
Left = 8640
Top = 720
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
Protocol = 4
URL = " target=_blank class="a1">http://"
End
Begin VB.TextBox txtContent
Height = 3735
Left = 120
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 5
Top = 1560
Width = 9495
End
Begin VB.TextBox txtPost
Height = 300
Left = 840
MaxLength = 255
TabIndex = 4
Top = 600
Width = 7215
End
Begin VB.CommandButton cmdPost
Caption = "Post"
Height = 375
Left = 8280
TabIndex = 2
Top = 120
Width = 1335
End
Begin VB.TextBox txtUrl
Height = 300
Left = 840
MaxLength = 255
TabIndex = 0
Text = "http://lan/aa.asp"
Top = 120
Width = 7215
End
Begin VB.Label Label3
AutoSize = -1 'True
Caption = "CookIe:"
Height = 255
Left = 120
TabIndex = 6
Top = 1080
Width = 735
End
Begin VB.Label Label2
AutoSize = -1 'True
Caption = "Post:"
Height = 255
Left = 120
TabIndex = 3
Top = 600
Width = 615
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "Url:"
Height = 255
Left = 120
TabIndex = 1
Top = 120
Width = 495
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub cmdPost_Click()
Dim strUrl As String '链接
Dim strHost As String '主机
Dim strCookie As String 'cookie
Dim intPos As Integer
Dim strHead As String '头
'重新构造请求头
strUrl = txtUrl.Text
If LCase(Left(strUrl, 7)) <> " target=_blank class="a1">http://" Then strUrl = " target=_blank class="a1">http://" & strUrl
intpost = InStr(8, strUrl, "/")
If intpost > 0 Then
strHost = Mid(strUrl, 8, intpost - 8)
Else
strHost = Mid(strUrl, 8, Len(strUrl) - 8)
End If
If txtCookie.Text = "" Then
strCookie = ""
Else
strCookie = "Cookie: " & txtCookie.Text
End If
strHead = "POST " & strUrl & " HTTP/1.0" & vbCrLf _
& "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*" & vbCrLf _
& "Referer: http://yunhai.blogchina.com/" & vbCrLf _
& "Accept -Language: zh -cn" & vbCrLf _
& "Content-Type: application/x-www-form-urlencoded" & vbCrLf _
& "Proxy -Connection: Keep -Alive" & vbCrLf _
& "User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Maxthon; .NET CLR 1.1.4322)" & vbCrLf _
& "Host: " & strHost & vbCrLf _
& "Content-Length: " & Len(txtPost.Text) & vbCrLf _
& "Pragma: no -cache" & vbCrLf _
& txtCookie.Text & vbCrLf & vbCrLf
InetHttp.Execute txtUrl.Text, "post", txtPost.Text, strHead
End Sub
Private Sub InetHttp_StateChanged(ByVal State As Integer)
Dim Buff As String
Dim Content As String
Select Case State
Case icNone '无状态可报告。
Debug.Print "无状态可报告。"
Case icHostResolvingHost '该控件正在查询所指定的主机的 IP 地址。
Debug.Print "该控件正在查询所指定的主机的 IP 地址。"
Case icHostResolved '该控件已成功地找到所指定的主机的 IP 地址。
Debug.Print "该控件已成功地找到所指定的主机的 IP 地址。"
Case icConnecting '该控件正在与主机连接。
Debug.Print "该控件正在与主机连接。"
Case icConnected '该控件已与主机连接成功。
Debug.Print "该控件已与主机连接成功。"
Case icRequesting '该控件正在向主机发送请求。
Debug.Print "该控件正在向主机发送请求。"
Case icRequestSent '该控件发送请求已成功。
Debug.Print "该控件发送请求已成功。"
Case icReceivingResponse '该控件正在接收主机的响应。
Debug.Print "该控件正在接收主机的响应。"
Case icResponseReceived '该控件已成功地接收到主机的响应。
Debug.Print "该控件已成功地接收到主机的响应。"
Case icDisconnecting '该控件正在解除与主机的连接。
Debug.Print "该控件正在解除与主机的连接。"
Case icDisconnected '该控件已成功地与主机解除了连接。
Debug.Print "该控件已成功地与主机解除了连接。"
Case icError '与主机通讯时出现了错误。
Debug.Print "与主机通讯时出现了错误。"
Case icResponseCompleted '该请求已经完成,并且所有数据均已接收到。
Debug.Print "该请求已经完成,并且所有数据均已接收到。"
Do
Buff = InetHttp.GetChunk(1024)
Content = Content & Buff
Loop While Len(Buff) > 0
txtContent.Text = Content
End Select
End Sub
此问题由s_jmail回答。
附加关键字:编程, 源程序, programming, source code, Visual Basic, VB, Internet编程, INET, www, internet, ie。
|