- 文件系统模块
- 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
广告