Javascript 发送带有https.request的POST请求,用于。。。在循环中,不面对;连接ETIMEDOUT“;一段时间后出错?
我需要向多次使用“https://”的表单发送一个post请求,因为我的脚本遍历了许多不同单词的列表(列表非常大)。它只在某一点上工作得很好,然后它停止了,我得到了错误:Javascript 发送带有https.request的POST请求,用于。。。在循环中,不面对;连接ETIMEDOUT“;一段时间后出错?,javascript,node.js,for-loop,https,Javascript,Node.js,For Loop,Https,我需要向多次使用“https://”的表单发送一个post请求,因为我的脚本遍历了许多不同单词的列表(列表非常大)。它只在某一点上工作得很好,然后它停止了,我得到了错误: events.js:291 throw er; // Unhandled 'error' event Error: connect ETIMEDOUT some.ip.addr.es:443 at tcp.connectWrap.afterConnect [as oncomplete] (net.js:1
events.js:291
throw er; // Unhandled 'error' event
Error: connect ETIMEDOUT some.ip.addr.es:443
at tcp.connectWrap.afterConnect [as oncomplete] (net.js:1145:16)
“some.ip.addr.es”不是错误中显示的内容,我只是更改了它显示的ip
我猜我得到了这个错误,因为要么我以错误的方式为。。。在循环中或循环只是做得太快太多次,导致连接超时
因为当它在一个较小的单词列表中循环时效果很好
我的代码:
这段代码可以很好地处理少量单词,并且不会抛出错误。
对于一个更大的错误,最终我会被ETIMEDOUT错误击中,我假设这是因为:
如果你能想出任何可能的解决办法,请告诉我。提前感谢。#2实际上并不相关-表单就是表单,web引擎以非常相似的方式处理请求,而不管框架如何#1似乎更可能成为问题所在。您是否尝试过添加
睡眠
等效项?看到了吗?是的,我刚才用你提供的链接中的睡眠功能又试了一次。我不知道我是否把它放错了地方,但不管我把它放在哪里,它似乎只等待一次,然后https.request本身就像一个全新的循环,再次快速处理列表中的每个单词,直到错误发生。真奇怪,我不明白。这就是为什么我想知道这是否是我向php发送请求的代码的问题。我会尝试在lineReader中调用post\u php
之前先调用sleep调用。当我这样做时,post\u php脚本根本不会执行,它只会等待一次(尝试了另一个sleep函数)然后send_php再次快速运行
var querystring = require('querystring');
var https = require('https');
const fs = require('fs');
var lineReader = require('line-reader');
// This is so that i can type the first_item manually
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
// Type my first_item here and pass it to my doWork function
readline.question('first item: ', first_parameter => {
doWork(first_parameter);
readline.close();
});
async function doWork(first_parameter) {
if (first_parameter.length == 0) {
console.log("You didn't specify the first item...")
} else {
// Read each line of my text file and pass it to post_php
lineReader.eachLine('myList.txt', function(line, last) {
post_php(line, first_parameter)
})
}
}
async function post_php(x, first_parameter) {
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
// My post data (body)
var post_data = querystring.stringify({
'first_item': first_parameter,
'second_item': x,
});
// The post request options (connection, php path etc)
var post_options = {
host: 'some.website.com',
port: '443',
path: '/directory/form.php',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(post_data)
}
};
// Connecting so that i can post to the form, there probably is a better way to do that, please let me know if so
var post_req = https.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
// Tell me which word it is passing currently to the form
console.log("Passing Parameter: " + x)
});
})
post_req.write(post_data);
post_req.end();
};