VB POST数据

来源:互联网 发布:江苏微盛网络 怎么样 编辑:程序博客网 时间:2024/06/05 11:13

WEBBROWSE:一般用在简单网页数据的获取,封装了COOKIES,使用方便,但是效率很低,因为要加载大量无用的LJ
INET:高效的获取网页源码控件,可以自己定义POST和GET方式,使用灵活,推荐使用
WSOCK:效率最高,但是所有东西都要自己定义

一、用Webbrowser
代码:Webbrowser.navigate (http://xxx/1.htm?name=apple&id=001)


二、用Inet (注意数据接收方式是POST还是GET ,具体的方式是在1.htm的<form>...</form>中
代码:(post方式)
1、设定数据头:
strSendHeader = "POST /hotbuy/myhotbuy.php HTTP/1.1" & vbCrLf
strSendHeader = strSendHeader & "Content-Type: application/x-www-form-urlencoded " & vbCrLf
strSendHeader = strSendHeader & "Content-Length: " & iSendLength & vbCrLf
2、设定要发的数据:
strsenddata= "name=apple&id=001"
3、发送地址:
strsendadd="http://xxx/1.htm
3、发送数据:
Inet.execute strsendadd,"POST",strsenddata,strsendHead
Inet (Get方式) GET方式比POST简单的多
代码:
strsenddata = "http://xxx/1.htm?apple&id=001"
Inet.execute strsenddata,"GET"


三、用winsock
1、设定数据头:
strSendHeader = "POST /hotbuy/myhotbuy.php HTTP/1.1" & vbCrLf
strSendHeader = strSendHeader & "Content-Type: application/x-www-form-urlencoded " & vbCrLf
strSendHeader = strSendHeader & "Content-Length: " & iSendLength & vbCrLf
2、设定要发的数据:
strsenddata= "name=apple&id=001"
strSend = strSendHeader & vbCrLf & strSendData
3、发送地址:
strsendadd="http://xxx/1.htm
4、建立连接
Winsock.RemoteHost = "http:/xxx/"
Winsock.RemotePort = 80
Winsock.Protocol = sckTCPProtocol
winsock.connect
Winsock1.SendData strsend
Winsock.close

VB Post方式提取网页数据

Public Function getDataAsHtml(ByVal url As String, ByVal postData As String, ByVal modifyCook As Boolean)
        '构造请求头
        Try
            Dim httpUrl As New System.Uri(url) '生成URL类
            Dim req As HttpWebRequest '创建HTTP请求实例
            req = CType(WebRequest.Create(httpUrl), HttpWebRequest)
            req.CookieContainer = currentCookies
            req.Timeout = 600000

            '构造POST数据
            req.Method = "POST" '设置请求方式
            req.ContentType = "application/x-www-form-urlencoded"
            Dim ParameterEncoding As Encoding = System.Text.Encoding.GetEncoding("GB2312") '设置编码方式
            Dim bytesData As Byte() = ParameterEncoding.GetBytes(postData) '获取POST数据
            req.ContentLength = bytesData.Length '获取数据长度
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Maxthon; SV1; .NET CLR 1.1.4322)"
            req.Accept = "*.*"
            req.ProtocolVersion = HttpVersion.Version11


            '发送HTTP请求
            Dim postStream As Stream = req.GetRequestStream()
            postStream.Write(bytesData, 0, bytesData.Length)   '以上向服务器post信息。
            postStream.Close()

            '读取返回信息

            Dim res As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse) '以下获取服务器返回信息
            Dim reader As StreamReader = _
            New StreamReader(res.GetResponseStream, System.Text.Encoding.GetEncoding("GB2312"))
            currentHTML = reader.ReadToEnd()

            '测试返回信息
            'MsgBox(currentHTML)
            If modifyCook = True Then
                Dim cook As Cookie
                For Each cook In res.Cookies
                    currentCookies.Add(cook)
                    If cook.Name = "JSESSIONID" Then
                        sessionID = cook.Value.Substring(4, 23)
                    End If
                    If cook.Name = "EBankNetBank2001" Then
                        usera = cook.Value
                    End If
                Next cook
            End If

            '关闭
            res.Close()
            Return currentHTML
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Function

参数说明,url---网址,postData---所要执行的动作及参数,modifyCook---是否改变本地的cookies。

获取Cookie信息

使用webbrowser获取某一个页面上的cookies信息。方法有两个。这里我们来获取百度页面上的cookies信息。
Private Sub Command1_Click()
MsgBox WebBrowser1.Document.cookie '用webbrowser的document对象获取
End Sub

Private Sub Command2_Click()
WebBrowser1.Navigate "javascript:alert(document.cookie)"   '利用执行脚本获取
End Sub

Private Sub Form_Load()
WebBrowser1.Navigate "http://www.baidu.com"

End Sub

0 0