【发布时间】:2017-05-30 13:06:39
【问题描述】:
代码如下:
var request, tmUrl;
request = require('request');
tmUrl = "http://archive.is/timemap/https://www.washingtonpost.com/news/the-fix/wp/2017/01/15/rep-john-lewiss-books-sell-out-following-donald-trumps-attacks/";
// this one works
request('http://www.google.com', function(error, response, body) {
if (!error && response.statusCode === 200) {
return console.log(body.slice(0, 301));
}
});
// this one is always 504
request(tmUrl, {
timeout: 10000, // changing to 60s made no difference,
// response comes back long before that
"User-Agent": 'curl/7.43.0'
}, function(error, response, body) {
if (!error) {
if (response.statusCode === 200 || response.statusCode === 404) {
return console.log(body.slice(0, 301));
} else {
console.error("response code ", response.statusCode);
return console.log(body.slice(0, 301));
}
} else {
return console.error(err);
}
});
问题是通过curl 运行该请求会返回预期的响应,即带有以下文本的404:TimeMap does not exists. The archive has no Mementos for the requested URI。但是当我在 Node 中运行它时,响应代码始终是 504,我不知道为什么。我最好的猜测是不允许使用用户代理,所以我让它们匹配。没用。我不知道下一步该做什么......
504 超时总是来自 cloudflare:
<span class="cf-footer-item"><span data-translate="performance_security_by">Performance
& security by</span> <a data-orig-proto="https" data-orig-ref="www.cloudflare.com/5xx-error-landing?utm_source=error_footer" id="brand_link" target="_blank">CloudFlare</a>
</span>
这就是问题所在,cloudflare 是否会以某种我不知道的方式阻止对网站的编程访问?
【问题讨论】:
-
启用 CURLOPT_VERBOSE ,并比较请求标头的差异。秘密就在这些标题中。
-
啊,我在 curl 中尝试过,但在我的代码中没有。会做的,好建议。
标签: node.js curl httprequest