无法在http请求上发送cookie

时间:2016-02-25 04:49:54

标签: php cookies vbscript

我有使用MSXML2.XMLHTTP对象向服务器发送ajax请求的vbscript代码。我读过this bug and workaround。通过两次调用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正确发送到服务器?

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案...简单..只需使用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