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

字號(hào):


    在昨天的《使用正確版本的XMLHTTP》中賣了個(gè)關(guān)子,ServerXMLHTTP的功能比XMLHTTP強(qiáng)大,你現(xiàn)在大概已經(jīng)猜到了吧。沒錯(cuò),用ServerXMLHTTP可以在HTTP請(qǐng)求頭中加入Cookie,而XMLHTTP不可以
    為了方便測(cè)試,先寫一個(gè)回顯Cookie的簡(jiǎn)單的PHP程序:
    代碼如下:
    <?php
    foreach($_COOKIE as $key => $value)
    echo "$key => $value\r\n";
    ?>
    然后分別用ServerXMLHTTP和XMLHTTP測(cè)試:
    代碼如下:
    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)頁就不用愁了。