亲爱的同学们!我需要你的帮助。
首先,这不是广告。我尝试在火车票网站http://booking.uz.gov.ua/en/上发出 POST 请求,以了解当前日期是否有票。但是……有问题。我正在使用 VBScript 发出请求。为了知道在网站上发送什么 HTTP Header 和 POST 请求,我使用了 Chrome 内置开发工具。
这是我的脚本:
Dim URL
Dim URL2
Dim URL3
Dim sRequest
Dim sCookies
'This is web page where I need to enter information.
URL = "http://booking.uz.gov.ua/en/"
'This is path that Chrome shows to send POST request.
URL2 = "http://booking.uz.gov.ua/en/purchase/search/"
'Optional URL, Chrome shows this link near of URL2. I think this is .js that works on info I enter on web site (URL).
'URL3 = "http://booking.uz.gov.ua/i/js/common.138.js"
'POST request that Chrome shows to send.
sRequest ="station_id_from=2200001&station_id_till=2208001&station_from=Kyiv&station_till=Odesa&date_ dep=09.19.2013&time_dep=00%3A00&search="
'Here I'm using GET request to retrieve Set-Cookie Header (SessionID first of all) to reuse in my second POST request.
sCookies = GetSetHeader(URL)
'Here I'm calling function to make POST request.
Result = HTTPPost(URL2, sRequest)
Function GetSetHeader(URL)
Set objhttp = CreateObject("Microsoft.XmlHttp")
objhttp.open "GET", URL, FALSE
objhttp.Send
'I'm getting only SessionID + other cookies that Chrome shows.
GetSetHeader = Left (objhttp.getResponseHeader("Set-Cookie"), 38) & " " & "HTTPSERVERID=server1; _gv_lang=en; __utma=31515437.675496133.1376934004.1376934004.1376934004.1; __utmb=31515437.2.10.1376934004; __utmc=31515437; __utmz=31515437.1376934004.1.1.utmcsr= (direct)|utmccn=(direct)|utmcmd=(none)"
End Function
Function HTTPPost(URL2, sRequest)
'Header I just took from Chrome.
Set objhttp = CreateObject("Microsoft.XmlHttp")
objHTTP.open "POST", URL2, false
objHTTP.setRequestHeader "Connection", "keep-alive"
objHTTP.setRequestHeader "Host", "booking.uz.gov.ua"
objHTTP.setRequestHeader "Connection", "keep-alive"
objHTTP.setRequestHeader "Content-Length", "Len(Request)"
objHTTP.setRequestHeader "GV-Token", "64214392f178b9f91e3b61a069915cd1"
objHTTP.setRequestHeader "Origin", "http://booking.uz.gov.ua"
objHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.setRequestHeader "GV-Unique-Host", "1"
objHTTP.setRequestHeader "GV-Ajax", "1"
objHTTP.setRequestHeader "GV-Screen", "1366x768"
objHTTP.setRequestHeader "GV-Referer", "http://booking.uz.gov.ua/en/"
objHTTP.setRequestHeader "Accept", "*/*"
objHTTP.setRequestHeader "Referer", "http://booking.uz.gov.ua/en/"
objHTTP.setRequestHeader "Accept-Encoding", "gzip,deflate,sdch"
objHTTP.setRequestHeader "Accept-Language", "ru-RU,ru;q=0.8,en- US;q=0.6,en;q=0.4"
'Here I use cookies retrieved with first GET request.
objHTTP.setRequestHeader "Cookie", "sCookies"
objHTTP.send sRequest
'I use this msg to check that right cookies send with POST request.
WScript.Echo sCookies
HTTPPost = objHttp.responseText
'Write answer to TXT file.
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFile = FSO.OpenTextFile("D:\Results.txt", 2, True)
oFile.Write(objHttp.responseText)
oFile.Close
Set oFile = Nothing
Set FSO = Nothing
end Function
我不能让我的脚本工作。如果我使用 URL2 发送请求,我会得到空的 TXT 文件。如果我使用 URL3(Chrome 显示给我的脚本路径)发送请求,我只会在我的 TXT 文件中收到 common.138.js 的内容。但我希望收到 JSON 类型的信息,如 Chrome 响应中所示。
我注意到,首先,如果刷新网站,并尝试重新发送旧请求,我给我一个错误:
NetworkError:400 错误请求 - http://booking.uz.gov.ua/en/purchase/search/ “
也许是因为 SessionID 改变了。
其次,我不能简单地写站名,我需要从下拉列表中选择它(在 UI 模式下使用此站点时)。或者我收到一个错误 - 从下拉列表中选择一个出发点。
第三,如果尝试通过单击站点上的按钮进行搜索来发送请求,我会收到错误状态代码:400 错误请求。我认为 SessionID 的时间已过期。
有一个使用 InternetExplorer.Application 系统对象的工作脚本,但它没有决定。我想通过发送请求使其工作。将来想尝试在php上做(作为学习过程的一部分)。
也许这是对像我这样的人的某种防御???有办法让我的脚本工作???也许 GET 和 POST 请求之间的 SessionID 会发生变化?或者也许 VBScript 无法解决它,我需要 PHP,例如???
我不知道如何解决这个问题。请帮帮我。睡不着。不能吃。非常非常感谢。