【问题标题】:node/request keep redirecting to the original URL without return any response节点/请求继续重定向到原始 URL 而不返回任何响应
【发布时间】:2014-11-13 01:56:21
【问题描述】:

我使用request 来检查给定的 URL 是否损坏。但是,我遇到了一个奇怪的情况,一个给定的 URL 一直重定向到它自己,并且请求无法返回任何响应。但是当我用浏览器打开url时,返回状态码200。

任何人都知道为什么请求会落入重定向循环并且在浏览器中找到 url 时无法获得响应?如何处理这个问题?谢谢!

request({
        uri: 'http://testurl'
        }, function (error, response, body) {
        ......
        }
})

以下是设置“request.debug = true”后的输出

REQUEST { uri: 'http://testurl',
  callback: [Function],
  tunnel: false }
REQUEST make request http://testurl
REQUEST onResponse http://testurl 302 { 'x-cnection': 'close',
  date: 'Wed, 12 Nov 2014 23:59:22 GMT',
  'transfer-encoding': 'chunked',
  location: 'http://testurl',
  ......,
  'x-powered-by': 'Servlet/2.5 JSP/2.1' }
REQUEST redirect http://testurl
REQUEST redirect to http://testurl
REQUEST {}
REQUEST make request http://testurl
REQUEST response end http://testurl 302 { 'x-cnection': 'close',
  date: 'Wed, 12 Nov 2014 23:59:22 GMT',
  'transfer-encoding': 'chunked',
  location: 'http://testurl',
  ......,
  'x-powered-by': 'Servlet/2.5 JSP/2.1' }
REQUEST onResponse http://testurl 302 { 'x-cnection': 'close',
......

更新: 阅读请求文档后,我意识到这可能与 cookie 有关。所以我在请求中添加了选项 jar: true ,终于可以了。

【问题讨论】:

  • 感谢您的更新。这为我解决了这个问题。今天的帮助很大。

标签: node.js redirect request


【解决方案1】:

尝试使用这些请求选项,如其文档中所述: - followAllRedirects - 遵循非 GET HTTP 3xx 响应作为重定向(默认值:false) - maxRedirects - 要遵循的最大重定向数(默认值:10)

request({
    uri: 'http://testurl',
    followAllRedirects: true,
    maxRedirects: 50 // some arbitrary value greater than 10
    }, function (error, response, body) {
    ......
    }
})

示例是公共网址吗?我想看看是不是这样。

【讨论】:

  • 感谢您的回复。我已经尝试过了,并且 url 不断重定向到自身并最终超过了 maxRedirects。
猜你喜欢
  • 1970-01-01
  • 2015-08-14
  • 2017-06-01
  • 2012-04-15
  • 1970-01-01
  • 2019-12-02
  • 2017-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多