除非注明,文章均为 战战如疯 原创,转载请保留链接: http://www./cat3/487.html,VBA交流群273624828。
在提交数据的时候我们常常需要模拟cookie,有些cookie长期有效,那只要把抓包得到的cookie写上好了,而有的cookie生存期很短,需要在运行程序的时候从返回的内容中抓取。取cookie一般的方法是用getResponseHeader("Set-Cookie")直接取或者getAllResponseHeaders取出所有头文件然后再提取出来,推荐第2种方法,这样取的比较完全。下面来看个例子,比较有代表性。
原网站http://www./data/trustdata/,需要登陆,登陆之后会产生一个cookie,我们现在要将这个cookie提取出来
![cookie](http://image82.360doc.com/DownloadImg/2015/02/1608/50230516_1.png)
看下代码:
Sub test() Dim URL, postdata, s, winhttp Set winhttp = CreateObject("WinHttp.WinHttpRequest.5.1") 'Set winhttp = CreateObject("Microsoft.XMLHTTP") With winhttp '登陆 URL = "http://www./2008/gogoal/inc/login/HwCheck.aspx?IsCheck=false" postdata = "__VIEWSTATE=%2FwEPDwUJOTM4MDkzNTI2D2QWAgIBD2QWAgIDDxYCHgdWaXNpYmxlaGRkB4zeddlBcJF5cGAGGloW3wUukrA%3D&__EVENTVALIDATION=%2FwEWBALmwZWIDwKJ77vKAwK4zqqrCgKC3IeGDFnc0Fsy074jPlU4QgXUxiaAcmkQ&login_name=******&login_password=******&btnLogin=" .Open "POST", URL, False .Option(6) = False .setRequestHeader "Connection", "Keep-Alive" .setRequestHeader "Cache-Control", "no-cache" '.setRequestHeader "Cookie", "Hm_lvt_29860f5735fc4987b5f8ef3ee2767847=1414509489; Hm_lpvt_29860f5735fc4987b5f8ef3ee2767847=1414509489" .setRequestHeader "Accept", "text/html, application/xhtml+xml, */*" .setRequestHeader "Accept-Encoding", "gzip, deflate" .setRequestHeader "DNT", "1" .setRequestHeader "Host", "www." .setRequestHeader "Content-Type", "application/x-www-form-urlencoded" .setRequestHeader "Referer", "http://www./2008/gogoal/inc/login/HwCheck.aspx?IsCheck=false" '.setRequestHeader "User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" .send (postdata) 's = .getAllResponseHeaders '获取所有的回应头信息 s = .getResponseHeader("Set-Cookie") MsgBox s URL = "http://www./2008/gogoal/inc/login/HwCheck.aspx?IsCheck=true" .Open "GET", URL, False .send s = .responsetext Debug.Print s End With End Sub
上面postdata里*的地方是用户名和密码,感兴趣的自己注册个账号试下,这里需要强调下的是.Option(6) = False这句是不能少的,禁止网页重定向,否则的话登陆之后网页会跳转,那就得不到我们需要的cookie了。
|