如何使用Request和NodeJS到达redirect的页面
我正在使用Request,Jsdom和NodeJSlogin到网页。 根据请求API ,
followRedirect
– 遵循HTTP 3xx响应作为redirect(默认值: true
)。 这个属性也可以被实现为函数,它将响应对象作为单个参数获取,如果redirect应该继续,则返回true;否则返回false。
鉴于这是默认的,我会认为这个请求会给我redirect页面,而不是中间的redirect页面。
我的代码:
request(loginPageURL, function(error, response, body) { /*Error handling removed for brevity*/ jsdom.env({ html: body, scripts: [ 'http://code.jquery.com/jquery-1.7.1.min.js' ], done: function(err, window) { //Set login fields var form = $('#authorizationForm'); var data = form.serialize(); var url = form.attr('action') || 'get'; var type = form.attr('enctype') || 'application/x-www-form-urlencoded'; var method = form.attr('method'); request({ url: url, method: method.toUpperCase(), body: data, headers: { 'Content-type': type } }, function(error, response, body) { if (!error) { console.log("Response.statusCode:" + response.statusCode); console.log("Body:" + body);
login后不再给我主页,输出是:
Response.statusCode: 302 Body: To access this site, go to <a href="...">
虽然从redirect页面获取代码可能会很有用,但是如何跳转到将其redirect到的页面呢?
这是一个非GET请求? 那么你需要明确地设置followAllRedirects: true
。
followAllRedirects
– 按照非GET HTTP 3xx响应作为redirect(默认:false
)
https://github.com/request/request
( followRedirect
的文档令人困惑,因为它们似乎并不表示followRedirect
只适用于GET。)