在WebBrowser中发送POST请求

我们要用到的也是WebBrowser的“Navigate”方法,其函数原型如下所示:

Sub Navigate(URL As String, [Flags], [TargetFrameName], [PostData], [Headers])

新建一个工程,部件中勾选中 “Microsoft Internet Controls”,添加一个WebBrowser1、一个Command1在窗体上,可以把WebBrowser1适当拉大一点,Form1中添加以下代码:

Private Sub Command1_Click()

    ReDim aByte(0) As Byte ' Array of bytes to hold data to post
   
    cPostData = "login_name=帳號&password=密碼&cookietime=0&x=42&y=10"
   
    PackBytes aByte(), cPostData
   
    Dim vPost As Variant
   
    vPost = aByte ' Assign the byte array to a VARIANT
   
    Dim vHeaders As Variant
   
    vHeaders = "Content-Type: application/x-www-form-urlencoded" + Chr(10) + Chr(13)
   
    WebBrowser1.Navigate "http://www.csdn.net/member/logon.asp", , , vPost, vHeaders

End Sub

Private Sub PackBytes(ByteArray() As Byte, ByVal PostData As String)

    iNewBytes = Len(PostData) - 1   ' Get rid of the null termination
   
    If iNewBytes < 0 Then
        Exit Sub
    End If
   
    ReDim ByteArray(iNewBytes)
   
    For i = 0 To iNewBytes
   
        ch = Mid(PostData, i + 1, 1)
        If ch = Space(1) Then
            ch = "+"
        End If
        Debug.Print ch, Asc(ch)
        ByteArray(i) = Asc(ch)
    Next
End Sub

 

Sub  getWeb()
 
     Dim  As  XMLHTTP
     tmpth =  "c:\temp.htm"
     URL =  "http://www.baidu.com/"
     Set  X =  New  XMLHTTP
     X.Open  "GET" , URL,  False
     X.send
     s = X.responseText
     ss =  "<body"
     arr = Split(s, ss)
     ss = ss & arr(1)
     If  Dir(tmpth) <>  ""  Then  Kill tmpth
 
     Open tmpth  For  Output  As  1
     Print #1, , ss
     Close 1
     WebBrowser1.Navigate2 tmpth
     Set  bd = WebBrowser1.Document.body
     Do  While  bd  Is  Nothing
         DoEvents
         Set  bd = WebBrowser1.Document.body
     Loop
     SendKeys  "c:\baidu.htm"
     SendKeys  "{ENTER}"
     WebBrowser1.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_PROMPTUSER
     Kill tmpth
End  Sub

转载于:https://www.cnblogs.com/lbnnbs/p/4784635.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值