학습 목적 외부 모듈을 사용하지 않아서 서버에 인증 요청을하려고합니다. '위치'다시 내가 요청하려고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()
:
이curl -L -u user:password http://webpage/email
[SOLVED, thansk ...]하지만 문제가 Node.js를,이 내 코드입니다 : 그것은 컬와 함께 작동 헤더에 있지만 같은 결과가 나타납니다.
도움 주셔서 감사합니다.
나는 당신이 이미 이것을 풀 었다고 알고 있지만, 여전히 다른 사람들에게 대답 할 가치가있다. – Marco