我正在嘗試在我的后端 node.js Web 應用程式中發出 https 請求。我有以下代碼:
const express = require('express');
const https = require('https');
const app = express();
app.get("/", function(req, res) {
const url = "https://www.tkmaxx.com/uk/en/women/edits/big-brand-drop/c/01240000/autoLoad?page=1"
https.get(url, function(response) {
console.log(response.statusCode);
});
res.send("running test")
});
app.listen(3000, function() {
console.log("Server started on port 3000");
});
我收到以下錯誤:
node:events:504
throw er; // Unhandled 'error' event
^
Error: socket hang up
at connResetException (node:internal/errors:691:14)
at TLSSocket.socketOnEnd (node:_http_client:466:23)
at TLSSocket.emit (node:events:538:35)
at endReadableNT (node:internal/streams/readable:1345:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
Emitted 'error' event on ClientRequest instance at:
at TLSSocket.socketOnEnd (node:_http_client:466:9)
at TLSSocket.emit (node:events:538:35)
at endReadableNT (node:internal/streams/readable:1345:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21) { code: 'ECONNRESET' }
有誰知道發生了什么?問題與請求標頭或用戶代理有關嗎?我該如何設定?
uj5u.com熱心網友回復:
僅當遠程服務器具有Accept
標頭和Connection: keep-alive
. (這些是瀏覽器通常設定的標題。)
https.get("https://www.tkmaxx.com/uk/en/women/edits/big-brand-drop/c/01240000/autoLoad?page=1", {
headers: {
accept: "text/html",
connection: "keep-alive"
}
}, function(response) {...});
(也許這是遠程服務器用來防止由瀏覽器以外的客戶端發出的請求的一種機制?)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/475477.html
標籤:javascript 节点.js 表示 https