You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js Request模块请求politi.dk域名页面失败问题排查

Troubleshooting the ECONNRESET Error with Request Module on politi.dk

Hey there, let's break down what's going on here and how to fix this frustrating issue.

First off, that ECONNRESET error means the politi.dk server is abruptly closing the connection before your request can finish. Since this worked fine before and only affects this domain, it’s almost certainly tied to recent changes on their server that your outdated Request module can’t keep up with.

Key Reasons This Happens:

  • Request is unmaintained: The Request module has been deprecated since 2020 and hasn’t received updates for modern TLS/SSL security standards. It’s highly likely politi.dk upgraded their server to use newer TLS versions (like TLS 1.3) or stricter security rules that Request’s default setup doesn’t support.
  • Missing SNI support: Many modern servers require Server Name Indication (SNI) to route requests correctly. Older versions of Request might not send SNI headers by default, causing the server to drop the connection.
  • User-Agent blocking: Politi.dk could have updated their anti-scraping measures to block the generic User-Agent string that Request sends (something like request/2.88.2).

Fixes to Try:

1. Switch to a Maintained HTTP Library (Recommended)

Since Request is no longer supported, moving to a modern alternative like axios or node-fetch will resolve most compatibility issues long-term. Here’s how to rewrite your test with axios:

First install the library:

npm install axios

Then the test code:

const axios = require('axios');

axios.get('https://www.politi.dk/da/ompolitiet/jobipolitiet/ledige_stillinger/ledigestillinger', {
  headers: {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36'
  }
})
.then(response => {
  console.log('statusCode:', response.status);
  console.log('statusMessage:', response.statusText);
})
.catch(error => {
  console.error(error);
});

2. Force Modern TLS Settings in Request (If You Must Keep Using It)

If you can’t switch libraries right away, try manually configuring Request to use newer TLS protocols and enable SNI:

var request = require('request');

const options = {
  url: 'https://www.politi.dk/da/ompolitiet/jobipolitiet/ledige_stillinger/ledigestillinger',
  headers: {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36'
  },
  agentOptions: {
    secureProtocol: 'TLSv1_2_method', // Use TLS 1.2 (or TLSv1_3_method if your Node.js version supports it)
    servername: 'www.politi.dk' // Enforce SNI header
  }
};

request(options, function(err, res, body){
  if(err){
    console.log(err);
  } else {
    console.log('statusCode:', res.statusCode);
    console.log('statusMessage:', res.statusMessage);
  }
});

Note: Ensure your Node.js version supports TLS 1.2 or higher (Node.js 8+ should have this enabled by default).

3. Test with a Custom User-Agent

Sometimes just spoofing a browser’s User-Agent is enough to bypass basic anti-scraping blocks. Add the User-Agent header to your original Request code first—it’s quick to test and often works.

Why This Worked Before

Politi.dk almost certainly updated their server’s security configuration recently (e.g., disabling old TLS protocols like 1.0/1.1, adding stricter SNI requirements, or implementing User-Agent filtering). Since Request hasn’t been updated to handle these changes, your requests now get rejected.

内容的提问来源于stack exchange,提问作者RonRonDK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:41:23