Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用node.js中的身份验证处理重定向响应?_Javascript_Http_Node.js - Fatal编程技术网

Javascript 如何使用node.js中的身份验证处理重定向响应?

Javascript 如何使用node.js中的身份验证处理重定向响应?,javascript,http,node.js,Javascript,Http,Node.js,出于学习目的,我没有使用外部模块,所以我尝试向服务器发出身份验证请求。它与curl一起工作: curl -L -u user:password http://webpage/email [已解决,thansk…]但在node.js中我遇到了问题,这是我的代码: var http = require("http"); var options = { hostname : 'webpage', port : '80', path : '/email', method : 'GET'

出于学习目的,我没有使用外部模块,所以我尝试向服务器发出身份验证请求。它与curl一起工作:

curl -L -u user:password http://webpage/email
[已解决,thansk…]但在node.js中我遇到了问题,这是我的代码:

var http = require("http");
var options = {
  hostname : 'webpage',
  port : '80',
  path : '/email',
  method : 'GET',
  headers : {
       "Connection" : "keep-alive",
       "User-Agent" : "Mozilla/5.0 (X11; Linux x86_64)"
  },
  auth : "username:password"
}

options.agent = new http.Agent(options);

var req = http.request(options, function(res) {
 // The authentication works fine, like curl without -L parameter
 // STATUS 302
 res.setEncoding('utf8');
 res.on('data',function(chunk){
  console.log(chunk);

 // SOLVED ! 

  var opts = {
   host : 'webpage',
   port : '80',
   path : '/email/',
   location : res.headers.location,
   auth : "user:password"
 }

 var require = http.request(opts,function(resp){
  resp.setEnconding("utf8");
  resp.on('data',function(chk){
   console.log(chk);
  });
 });

 require.end();
 // --------------

  // I got the same without -L parameter in curl
  // <head><title>Document Moved</title></head>
  // <body><h1>Object Moved</h1>This document may be found <a href="http://webpage/email/">here</a></body> <-- The 'Location' is the same

 });
});

req.on('error',function(e){
 console.log('Problem with request : ' + e.message);
}

req.end()
var http=require(“http”);
变量选项={
主机名:“网页”,
端口:'80',
路径:'/email',
方法:“GET”,
标题:{
“连接”:“保持活动状态”,
“用户代理”:“Mozilla/5.0(X11;Linux x86_64)”
},
验证:“用户名:密码”
}
options.agent=新的http.agent(options);
var req=http.request(选项、函数(res){
//身份验证工作正常,就像没有-L参数的curl一样
//状态302
res.setEncoding('utf8');
res.on('data',函数(块){
console.log(块);
//解决了!
变量选项={
主持人:"网页",,
端口:'80',
路径:'/email/',
位置:res.headers.location,
验证:“用户:密码”
}
var require=http.request(选项、函数(resp){
分别设置编码(“utf8”);
响应(‘数据’、功能(chk){
控制台日志(chk);
});
});
require.end();
// --------------
//在curl中没有-L参数,我得到了相同的结果
//文件移动

//对象移动此文档可以找到curl中的-u允许您传递身份验证标头的值。在节点中,您可以手动执行此操作。基本身份验证规范(我假设您正在使用)说明以base 64编码格式传递凭据。在节点中手动执行此操作类似于此

headers = {
  'Authorization': 'Basic ' + (new Buffer(user + ':' + pass)).toString('base64')
}

我知道你说你已经解决了这个问题,但它仍然值得其他人回答。我认为头的名称是“Authorization”,当我使用节点v.0.4.7(对于heroku)执行此请求时,我出现了错误,因为auth属性在该版本中不存在,所以我尝试使用“Authorization”头,效果很好。