I want to check the status of some web pages and write the following codes. I use "phantomjs.exe --web-security=false test.js" under the terminal to run the following code. If the network status is normal, then everything goes well. If the network status is abnormal, the code will stop running and get an error message: "PHANTOM ERROR: NETWORK_ERR: XMLHttpRequest Exception 101: A network error occurred in synchronous requests."
I hope that when the network status is abnormal, the code can continue to detect the next web page instead of stopping. I have been searching on the Internet for a long time and I don’t know how to deal with it. I hope someone can help me.
var urls = new Array("www.good.com", "www.bad.com","www.good.com");
var fs = require('fs');
var request;
if (window.XMLHttpRequest)
request = new XMLHttpRequest();
else
request = new ActiveXObject("Microsoft.XMLHTTP");
request.onload = function (e) {
if (request.readyState === 4) {
console.log(request.status);
}
};
phantom.onError = function(msg, trace) {
var msgStack = ['PHANTOM ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
});
}
console.log(msgStack.join('\n'));
};
for (i = 0; i < urls.length; i++) {
console.log(urls[i]);
request.open('GET', "https://" + urls[i], false);
request.send();
fs.write("result.txt", urls[i] + ',' + request.status + '\n', 'a');
}
phantom.exit();