用VBS實現(xiàn)的發(fā)送帶Cookie的HTTP請求的代碼

字號:


    在昨天的《使用正確版本的XMLHTTP》中賣了個關(guān)子,ServerXMLHTTP的功能比XMLHTTP強大,你現(xiàn)在大概已經(jīng)猜到了吧。沒錯,用ServerXMLHTTP可以在HTTP請求頭中加入Cookie,而XMLHTTP不可以
    為了方便測試,先寫一個回顯Cookie的簡單的PHP程序:
    代碼如下:
    <?php
    foreach($_COOKIE as $key => $value)
    echo "$key => $value\r\n";
    ?>
    然后分別用ServerXMLHTTP和XMLHTTP測試:
    代碼如下:
    Dim http
    Set http = CreateObject("Msxml2.XMLHTTP")
    http.open "GET", "http://demon.tw/test/cookie.php", False
    http.SetRequestHeader "Cookie", "user=demon; passwd=123456"
    http.send
    WScript.Echo http.responseText
    用Msxml2.XMLHTTP什么都沒有返回。
    代碼如下:
    Dim http
    Set http = CreateObject("Msxml2.ServerXMLHTTP")
    http.open "GET", "http://demon.tw/test/cookie.php", False
    http.SetRequestHeader "Cookie", "user=demon; passwd=123456"
    http.send
    WScript.Echo http.responseText
    用Msxml2.ServerXMLHTTP返回
    user => demon
    passwd => 123456
    以后碰到需要Cookie的網(wǎng)頁就不用愁了。