作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过 VBA 脚本登录到oddsportal。我已经编写了以下代码,但它不起作用。当我为 POST 请求设置 cookie 时出现错误。如果有人能指出我正确的方向,我将不胜感激。
Dim WHTTP As Object
Dim myuser, mypass, url, strAuthenticate As String
Dim out As String
url = "http://www.oddsportal.com/"
myuser = "user"
mypass = "pass"
strAuthenticate = "login-username=" & myuser & "&login-password=" & mypass & "&login-submit="
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
WHTTP.Open "POST", url, False
WHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36"
WHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
WHTTP.setRequestHeader "Connection", "keep-alive"
WHTTP.send strAuthenticate
strCookie = WHTTP.getResponseHeader("Set-Cookie") **'I am getting an error here**
strResponse = WHTTP.ResponseText
WHTTP.Open "GET", "http://www.oddsportal.com/soccer/africa/africa-cup-of-nations/results/", False
WHTTP.setRequestHeader "Connection", "keep-alive"
WHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
WHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36"
WHTTP.setRequestHeader "Cookie", strCookie
WHTTP.send
out = WHTTP.ResponseText
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection: keep-alive
Date: Tue, 27 May 2014 16:01:29 GMT
Pragma: no-cache
Transfer-Encoding: chunked
Content-Type: text/html
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Age: 0
Server: Apache
Set-Cookie: op_lang=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/
Set-Cookie: op_oddsportal=fehof62734n35crjd9vpshhf10; path=/
Set-Cookie: op_cookie-test=ok; expires=Wed, 27-May-2015 16:01:28 GMT; path=/
Set-Cookie: op_state=1
Set-Cookie: op_last_id=1; expires=Thu, 26-Jun-2014 16:01:28 GMT; path=/
Set-Cookie: op_cookie-test=ok; expires=Wed, 27-May-2015 16:01:28 GMT; path=/
Vary: Accept-Encoding
X-Deliver: Tue, 27 May 2014 16:01:29 GMT
Set-Cookie:op_user_logout=0; expires=Mon, 18-May-2015 18:44:49 GMT; path=/
Set-Cookie:op_last_id=1; expires=Thu, 26-Jun-2014 18:44:49 GMT; path=/
Set-Cookie:op_user_login_id=95774; expires=Mon, 18-May-2015 18:44:49 GMT; path=/
Set-Cookie:op_user_login_hash=73a967ad18d6a353afa12877309f4708; expires=Mon, 18-May-2015 18:44:49 GMT; path=/
Set-Cookie:op_cookie-test=ok; expires=Wed, 27-May-2015 18:44:48 GMT; path=/
Set-Cookie:op_user_time_zone=1.00; expires=Thu, 26-Jun-2014 18:44:49 GMT; path=/
Set-Cookie:op_user_full_time_zone=35; expires=Thu, 26-Jun-2014 18:44:49 GMT; path=/
Set-Cookie:op_lang=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/
最佳答案
我不确定它在这种特定情况下是否会有所帮助,但请求和响应 cookie 处理包含在我的库中 Excel-REST .下面是获取和设置登录 ID 和哈希的示例:
Dim OddsClient As New RestClient
OddsClient.BaseUrl = "http://www.oddsportal.com/"
Dim LoginRequest As New RestRequest
LoginRequest.Method = httpPOST
LoginRequest.Format = formurlencoded
LoginRequest.AddParameter "login-username", myuser
LoginRequest.AddParameter "login-password", mypass
LoginRequest.AddParameter "login-submit", ""
Dim LoginResponse As RestResponse
Set LoginResponse = OddsClient.Execute(LoginRequest)
If LoginResponse.StatusCode = 200 Then
Dim OddsRequest As New RestRequest
OddsRequest.Resource = "soccer/africa/africa-cup-of-nations/results/"
OddsRequest.AddCookie "op_user_login_id", LoginResponse.Cookies("op_user_login_id")
OddsRequest.AddCookie "op_user_login_hash", LoginResponse.Cookies("op_user_login_hash")
Dim OddsResponse As RestResponse
Set OddsResponse = OddsClient.Execute(OddsRequest)
End If
getAllResponseHeaders
变成
Collection
键值标题以便于访问,然后使用“Set-Cookie”键取出那些(参见
RestHelpers)。对于您的示例,类似于以下内容的内容可能会起作用:
Dim Headers As Collection ' of Dictionaries with key, value
Set Headers = ExtractHeadersFromResponseHeaders(WHTTP.getAllResponseHeaders) ' from RestHelpers
' Setup GET...
Dim Header As Dictionary
For Each Header In Headers
' Move all cookies from login to GET request
If Header("key") = "Set-Cookie" Then
WHTTP.setRequestHeader "Cookie", Header("value")
End If
Next Header
关于excel - VBA WinHTTP 登录到 Oddsportal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23859594/
我正在尝试通过 VBA 脚本登录到oddsportal。我已经编写了以下代码,但它不起作用。当我为 POST 请求设置 cookie 时出现错误。如果有人能指出我正确的方向,我将不胜感激。 Dim W
我一直在尝试从这个 ajax website 中提取数字上每个单元格的数据, 每个单元格的详细信息只有当鼠标指向单元格时才会弹出。 我已使用 Python selenium webdriver 和 p
这是我登录 oddsportal.com 的代码: from selenium import webdriver login_url = "http://www.oddsportal.com/logi
我是一名优秀的程序员,十分优秀!