JavaScript   发布时间:2022-04-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了phantomjs – 如何从page.open请求中查看HTTP状态代码?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个phantomJS脚本,其中包含以下内容:
page.open(url,function (status) {
    if (status === "fail") { /* handle failure */ }
});

状态检查有时会起作用,但即使请求返回500,状态仍将是“成功”.如何获取实际的请求状态代码?

解决方法

你可以这样做:
var page = require('webpage').create(),system = require('system'),resources = [];

page.open('http://google.com',function (status) {
    console.log('Loaded with http status:',resources[0].status);
    phantom.exit();
});

page.onresourceReceived = function(responsE) {
    // check if the resource is done downloading 
    if (response.stage !== "end") return;
    // apply resource filter if needed:
    if (response.headers.filter(function(header) {
        if (header.name == 'Content-Type' && header.value.indexOf('text/html') == 0) {
            return true;
        }
        return false;
    }).length > 0)
        resources.push(responsE);
};

因此,如果您需要检查第一个浏览器请求的状态(在本例中为google的html页面),您应该将其视为资源[0] .status中返回的第一个请求.在onresourceReceived处理程序中,您可以为尝试从中获取http代码的资源添加更多过滤器.

更新:感谢@fotijr,添加了对已完成回复的检查

大佬总结

以上是大佬教程为你收集整理的phantomjs – 如何从page.open请求中查看HTTP状态代码?全部内容,希望文章能够帮你解决phantomjs – 如何从page.open请求中查看HTTP状态代码?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。