如何从page.open请求中查看HTTP状态代码?

时间:2022-10-07 09:52:31

I have a phantomJS script that contains the following:

我有一个phantomJS脚本,其中包含以下内容:

page.open(url, function (status) {
    if (status === "fail") { /* handle failure */ }
});

The status check works sometimes, but the status will still be "success" even if the request returns 500. How can I get the actual request status code?

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

2 个解决方案

#1


You can do it something like this:

你可以这样做:

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);
};

So, if you need to check the status of the first browser's request (in this case google's html page) you should see it as the first one returned in resources[0].status. In onResourceReceived handler you can add more filters for resources you try to get http code from.

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

UPDATE: thanks to @fotijr, added a check for completed responses

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

#2


In

page.property('onResourceError', function(res) {

resources variable is undefined,

资源变量未定义,

even if I set it with

即使我设置它

var page = require('webpage').create(),
system = require('system'),
resources = [];

#1


You can do it something like this:

你可以这样做:

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);
};

So, if you need to check the status of the first browser's request (in this case google's html page) you should see it as the first one returned in resources[0].status. In onResourceReceived handler you can add more filters for resources you try to get http code from.

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

UPDATE: thanks to @fotijr, added a check for completed responses

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

#2


In

page.property('onResourceError', function(res) {

resources variable is undefined,

资源变量未定义,

even if I set it with

即使我设置它

var page = require('webpage').create(),
system = require('system'),
resources = [];