vb xmlhttp登陆阿里妈妈的问题 等高手啊!!!!1
这个是用淘宝联盟帐号登陆阿里妈妈的程序,试验了好多次都不成功,因为这个页面有跳转所以用Microsoft.XMLHTTP总提示拒绝访问,所以改用msxml2.serverxmlhttp ,可以正确得到token这个随机数 注:用户名密码用的是别人的,不好公布,所以下面的是瞎编的.
抓包获取到的数据如下:
--------------------------------------------------
POST //union/minilogin.htm?style=mini&proxy=http://www.alimama.com/union/proxy.htm HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, application/QVOD, application/vnd.ms-xpsdocument, */*
Referer: http://www.alimama.com//union/minilogin.htm?style=mini&proxy=http://www.alimama.com/union/proxy.htm
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: www.alimama.com
Content-Length: 225
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: cookie1=a0a557191bd60f486b067c0e877b5acf; lzstat_ss=2561147527_21_1338409765_700373|3578922704_10_1338409236_390770|3555532821_10_1338409236_359586; cna=hqbwB14TF3kCAY4gVtpW5h/j; lzstat_uv=8175108793566037616|700373@390770@359586; wwwtaobaocomsupport=123; UniProc1316050783=123383752910156769; v=0; _tb_token_=d84e37eabe30; JSESSIONID=357EC26BB318D6758C93E02BFEE5C2FF
_tb_token_=d84e37eabe30&transdata=&action=UnionMemberAction&event_submit_do_mini_login=true&redirectURL=&query_string=&_fmu.l._0.l=ren101&originalLogpasswd=987654&_fmu.l._0.lo=3c0500693f5229191f72c9b789226ebf
--------------------------------------------------
以下是程序源码:
----------------------------------------------------
Dim xPost, sGet As Object
Dim iRemote, formhash, sendstr, posttime, veri, temp, hh As String
iRemote = "http://www.alimama.com//union/minilogin.htm?style=mini&proxy=http://www.alimama.com/union/proxy.htm"
Set xPost = CreateObject("msxml2.serverxmlhttp")
Set sGet = CreateObject("ADODB.Stream")
xPost.Open "GET", iRemote, False
xPost.setRequestHeader "referer", iRemote
xPost.send
sGet.Mode = 3
sGet.Type = 1
sGet.Open
sGet.Write (xPost.responseBody)
sGet.Position = 0
sGet.Type = 2
sGet.charset = "gb2312" ' "gb2312"
formhash = sGet.ReadText
sGet.Close
formhash = getText(formhash, "name='_tb_token_' type='hidden' value='", "'>")'获取token
iRemote = "http://www.alimama.com//union/minilogin.htm?style=mini&proxy=http://www.alimama.com/union/proxy.htm"
sendstr = "_tb_token_=" & formhash & "&transdata=&action=UnionMemberAction&event_submit_do_mini_login=true&redirectURL=&query_string=&_fmu.l._0.l=ren101&originalLogpasswd=987654&_fmu.l._0.lo=3c0500693f5229191f72c9b789226ebf"
xPost.Open "post", iRemote, False
xPost.setRequestHeader "Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, */*"
xPost.setRequestHeader "Referer", "http://www.alimama.com//union/minilogin.htm?style=mini&proxy=http://www.alimama.com/union/proxy.htm"
xPost.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xPost.setRequestHeader "Accept-Language", "zh-cn"
xPost.setRequestHeader "Accept-Encoding", "gzip, deflate"
xPost.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; SV1; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
xPost.send (sendstr)
sGet.Mode = 3
sGet.Type = 1
sGet.Open
sGet.Write (xPost.responseBody)
sGet.Position = 0
sGet.Type = 2
sGet.charset = "gb2312" ' "gb2312"
formhash = sGet.ReadText
sGet.Close
Clipboard.Clear
Clipboard.SetText (formhash)
结果总是转到阿里妈妈的404页面(http://www.alimama.com/errorpage/inc.html),而且是未登陆的,难道阿里妈妈有验证cookies?如果是,怎么编制这个cookies,没理解那些参数.
用xPost.getAllResponseHeaders()所得到 的参数很简单,如下:
Date: Wed, 30 May 2012 13:19:50 GMT
Set-Cookie: JSESSIONID=30C035CDA14E4F8460547F2A29345EC4; Path=/
Content-Type: text/html;charset=GBK
Content-Language: zh-CN
Vary: Accept-Encoding
Content-Encoding: gzip
Connection: close
根本就与抓包得到的cookies不同,而且这个jsessionid是随机变化的,每post一次分配一个sessionid,头都大了,求高手赐教啊!!!!!!!
[解决办法]
Option Explicit
Dim GetUrl$, PostUrl$, Referer$
Dim tb_token$, FirstCookie$
Dim PostData$, ReturnCode$
Private Sub Command1_Click() '邮箱中的"@"要替换成"%40"
GetUrl = "http://www.alimama.com//union/minilogin.htm"
FirstCookie = GetByWinHttp(GetUrl)
tb_token = Split(FirstCookie, "; ")(1)
PostUrl = "http://www.alimama.com//union/minilogin.htm"
Referer = "http://www.alimama.com//union/minilogin.htm"
PostData = tb_token & "&transdata=&action=" & _
"UnionMemberAction&event_submit_do_mini_login=" & _
"true&redirectURL=&query_string=&_fmu.l._0.l=" & _
"邮箱" & "&originalLogpasswd=" & "密码" & _
"&_fmu.l._0.lo=" & hex_md5("密码")
ReturnCode = PostByWinHttp(PostUrl, PostData, Referer, FirstCookie)
Debug.Print ReturnCode
End Sub
Function PostByWinHttp$(ByVal PostUrl$, ByVal PostData$, ByVal Referer$, ByVal Cookie$)
On Error Resume Next
Dim XmlHttp As Object
Set XmlHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
With XmlHttp
.Open "POST", PostUrl, True
.SetRequestHeader "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
If Referer <> "" Then .SetRequestHeader "Referer", Referer
If Cookie <> "" Then .SetRequestHeader "Cookie", Cookie
.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.SetRequestHeader "Content-Length", Len(PostData)
.Send (PostData)
.WaitForResponse
PostByWinHttp = BytesToBstr(.ResponseBody, "GBK")
End With
Set XmlHttp = Nothing
End Function
Function GetByWinHttp$(ByVal GetUrl$)
On Error Resume Next
Dim XmlHttp As Object
Set XmlHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
With XmlHttp
.Open "GET", GetUrl, True
.SetRequestHeader "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
.SetRequestHeader "Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7"
.SetRequestHeader "Keep-Alive", "115"
.Send
.WaitForResponse
GetByWinHttp = ControlCookies(.GetAllResponseHeaders)
End With
Set XmlHttp = Nothing
End Function
Function ControlCookies$(ByVal Source$)
Dim tem$(), temCookie$, i&, MyCookie$
Source = Replace(Source, ";", "; ")
tem = Split(Source, vbCrLf)
For i = LBound(tem) To UBound(tem)
If InStr(tem(i), "Set-Cookie") <> 0 Then
If Right(tem(i), 2) <> "; " Then tem(i) = tem(i) & "; "
temCookie = Left(tem(i), InStr(tem(i), "; ") + 1)
temCookie = Right(temCookie, Len(temCookie) - 12)
ControlCookies = ControlCookies & temCookie
End If
Next i
End Function
Function BytesToBstr$(ByVal strBody As Variant, ByVal CodeBase$)
Dim objStream
Dim server
Set objStream = CreateObject("Adodb.Stream")
objStream.Type = 1
objStream.Mode = 3
objStream.Open
objStream.write strBody
objStream.Position = 0
objStream.Type = 2
objStream.Charset = CodeBase
BytesToBstr = objStream.ReadText
objStream.Close
Set objStream = Nothing
End Function