此http请求位于gulpfile中。我收到错误消息:
problem with request: getaddrinfo ENOTFOUND https://service.javascriptobfuscator.com/HttpApi.ashx https://service.javascriptobfuscator.com/HttpApi.ashx:80
请求
gulp.task("obfuscate", "Obfuscate this", function() {
console.log("obfuscate this!");
var proj = {
"APIKey": "hidden",
"APIPwd": "hidden",
"Name": "Sample1",
"ReplaceNames": true,
"MoveStrings": true,
"EncodeStrings": true,
"items": [
{
"FileName": "test0.js",
"FileCode": "function hello(x,y){var z=11;return x+y+z;}"
},
{
"FileName": "test1.js",
"FileCode": "var strs=['aaaa','bbbb','cccc','dddd','eeee','abcdefghijklmnopqrstuvwxyz0123456789']"
}
]
};
var options = {
hostname: 'https://service.javascriptobfuscator.com/HttpApi.ashx',
path: '',
method: 'POST',
headers: {
'Content-Type': 'text/json'
}
};
var req = http.request(options, function(res) {
console.log('Status: ' + res.statusCode);
//console.log('Headers: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (body) {
//console.log('Body: ' + body);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
//console.log(JSON.stringify(proj));
// write data to request body
req.write(JSON.stringify(proj));
req.end();
});
有什么问题?
答案 0 :(得分:4)
hostname
应该只是托管主机的名称,你在那里放了一个完整的URL。
将该值拆分为protocol
,hostname
和path
。即:
var options = {
protocol: 'https:'
hostname: 'service.javascriptobfuscator.com',
path: '/HttpApi.ashx',
method: 'POST',
headers: {
'Content-Type': 'text/json'
}
};
解释错误消息的秘诀是要意识到“getaddrinfo”是一种用于进行DNS查找的方法(即它将主机名转换为IP地址)。如果该方法抱怨“https://service.javascriptobfuscator.com/HttpApi.ashx”,则必须传递错误的值,因为这不仅仅是主机名。
答案 1 :(得分:0)
当我将https://
作为主机名的一部分删除时,它有效。
以下编辑修复了问题:
var options = {
hostname: 'service.javascriptobfuscator.com',
path: '/HttpApi.ashx',
method: 'POST',
headers: {
'Content-Type': 'text/json'
}
};