我有 vbscript 代码,可以使用 cookie 向服务器发送 ajax 请求MSXML2.XMLHTTP
对象。我已经阅读了有关此错误和解决方法的信息。通过调用setRequestHeader
两次,cookie 应该正确发送。但这并没有发生..
vbscript代码:
Dim http
set http = WScript.CreateObject("MSXML2.XMLHTTP")
http.open "POST", "http://localhost/echo", false
http.setRequestHeader "X-Requested-With", "XMLHttpRequest"
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.setRequestHeader "Cookie", "SESSID=f3rds19k7qu58pvmb80561dc76" '1st invoke '
http.setRequestHeader "Cookie", "SESSID=f3rds19k7qu58pvmb80561dc76" '2nd invoke '
http.send sReq
msgbox http.responseText
在服务器端http://localhost/echo/index.php
包含代码
<?php print_r(getallheaders());?>
该 PHP 代码基本上只将请求标头回显到客户端,甚至我调用了两倍我得到的:
Array
(
[Accept] => */*
[X-Requested-With] => XMLHttpRequest
[Accept-Language] => id
[Accept-Encoding] => gzip, deflate
[User-Agent] => Mozilla/4.0 (bla..bla..bla...)
[Host] => localhost
[Connection] => Keep-Alive
[Cache-Control] => no-cache
)
正如您在标题上看不到 cookie 一样。如何使cookie正确发送到服务器?
终于找到了解决方案...简单。。只需使用WinHTTP.WinHTTPRequest
而不是MSXML2.XMLHTTP
Dim http
set http = WScript.CreateObject("WinHTTP.WinHTTPRequest.5.1")
http.open "POST", "http://localhost/echo", false
http.setRequestHeader "X-Requested-With", "XMLHttpRequest"
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.setRequestHeader "Cookie", "SESSID=f3rds19k7qu58pvmb80561dc76"
msgbox http.responseText