- 檔案系統模組
- PhantomJS - 屬性
- PhantomJS - 方法
- 系統模組
- PhantomJS - 屬性
- Web 伺服器模組
- PhantomJS - 屬性
- PhantomJS - 方法
- 其他
- 命令列介面
- PhantomJS - 螢幕截圖
- PhantomJS - 頁面自動化
- PhantomJS - 網路監視
- PhantomJS - 測試
- PhantomJS - REPL
- PhantomJS - 示例
- PhantomJS 有用資源
- PhantomJS - 快速指南
- PhantomJS - 有用資源
- PhantomJS - 討論
PhantomJS - customHeaders 屬性
customHeaders 函式指定頁面發出的每個請求將傳送給伺服器的其他 HTTP 請求頭。預設值為一個空物件“{}”。在傳送到伺服器之前,標頭名稱和值將以 US-ASCII 編碼。
語法
其語法如下所示 −
var wpage = require('webpage').create();
wpage.customHeaders = {
//specify the headers
};
示例
以下示例顯示了使用 customHeaders 屬性。
var page = require('webpage').create();
var server = require('webserver').create();
var port = 8080;
var listening = server.listen(8080, function (request, response) {
console.log("GOT HTTP REQUEST");
console.log(JSON.stringify(request, null, 4));
});
var url = "https://:" + port + "/foo/response.php";
console.log("sending request to :" +url);
page.customHeaders = {'Accept-Language' : 'en-GB,en-US;q = 0.8,en;q = 0.6'};
//additional headers are mentioned here
page.open(url, function (status) {
if (status !== 'success') {
console.log('page not opening');
} else {
console.log("Getting response from the server:");
}
phantom.exit();
});
帶有 customheader 的輸出
以上程式生成以下輸出。
sending request to :https://:8080/foo/response.php
GOT HTTP REQUEST {
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q = 0.9,*/*;q = 0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-GB,en-US;q = 0.8,en;q = 0.6",
"Connection": "Keep-Alive",
"Host": "localhost:8080",
"User-Agent": "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1"
},
"httpVersion": "1.1",
"method": "GET",
"url": "/foo/response.php"
}
在上面的示例中,我們使用 customheader 設定 Accept-Language,並且在 http 標頭中顯示了相同的設定。
不帶 customheader 的輸出
以上程式生成以下不帶 customheader 的輸出。
sending request to :https://:8080/foo/response.php
GOT HTTP REQUEST {
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q = 0.9,*/*;q = 0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-IN,*",
"Connection": "Keep-Alive",
"Host": "localhost:8080",
"User-Agent": "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1"
},
"httpVersion": "1.1",
"method": "GET",
"url": "/foo/response.php"
}
沒有 custom 標頭,accept 語言將如以上輸出中所示。使用 customheader 屬性,可以更改所需的 http 標頭。
phantomjs_webpage_module_properties.htm
廣告