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

使用Axios调用政务接口遇SSL协商错误及返回数据异常求助

解决Axios调用政务接口的SSL错误及响应异常问题

问题概述

  • 直接运行代码触发SSL错误:SSL routines:final_renegotiate:unsafe legacy renegotiation disabled
  • 添加secureOptions: crypto.constants.SSL_OP_LEGACY_SERVER_CONNECT后请求成功,但返回的HTML与手动访问页面提交租户编号后的正常响应不一致

问题原因

  1. SSL连接错误:目标服务器使用了旧版SSL协商协议,Node.js默认禁用不安全的旧版协商逻辑,必须开启legacy选项才能建立连接。
  2. 响应异常:目标站点基于ASP.NET WebForms构建,__VIEWSTATE、__EVENTVALIDATION等参数是动态生成的,每次请求页面都会更新。硬编码的旧参数会导致服务器识别请求无效,返回初始页面而非查询结果。

分步解决方案

1. 修复SSL连接问题

保留secureOptions配置以解决SSL协商错误,这是建立连接的基础:

httpsAgent: new https.Agent({
  rejectUnauthorized: false,
  keepAlive: true,
  secureOptions: crypto.constants.SSL_OP_LEGACY_SERVER_CONNECT,
}),

2. 动态获取有效WebForms参数

先发送GET请求获取初始页面,解析出最新的__VIEWSTATE、__VIEWSTATEGENERATOR、__EVENTVALIDATION参数,再用这些参数构造POST请求。

修改后的完整代码

import Axios from 'axios';
import https from 'https';
const crypto = await import('node:crypto');
import cheerio from 'cheerio'; // 先执行 npm install cheerio 安装依赖

const AxPortal = Axios.create({
  withCredentials: true,
  timeout: 100 * 1000,
  headers: {
    'Accept': '*/*',
    'Accept-Encoding': 'gzip, deflate, br',
  },
  httpsAgent: new https.Agent({
    rejectUnauthorized: false,
    keepAlive: true,
    secureOptions: crypto.constants.SSL_OP_LEGACY_SERVER_CONNECT,
  }),
});

try {
  // 第一步:GET请求获取初始页面,提取动态参数
  const getResp = await AxPortal.get('https://ahmedabadcity.gov.in/AMCWEBMODULE/PTAX/FrmTENE_PEC_Details.aspx');
  const $ = cheerio.load(getResp.data);
  
  // 提取WebForms必需的动态参数
  const viewState = $('#__VIEWSTATE').val();
  const viewStateGenerator = $('#__VIEWSTATEGENERATOR').val();
  const eventValidation = $('#__EVENTVALIDATION').val();

  // 第二步:构造POST请求,使用刚提取的有效参数
  let config = {
    method: 'post',
    maxBodyLength: Infinity,
    url: 'https://ahmedabadcity.gov.in/AMCWEBMODULE/PTAX/FrmTENE_PEC_Details.aspx',
    headers: {
      "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
      "accept-language": "en-GB,en;q=0.9",
      "cache-control": "no-cache",
      "content-type": "application/x-www-form-urlencoded",
      "pragma": "no-cache",
      "sec-ch-ua": "\"Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"115\", \"Chromium\";v=\"115\"",
      "sec-ch-ua-mobile": "?0",
      "sec-ch-ua-platform": "\"macOS\"",
      "sec-fetch-dest": "document",
      "sec-fetch-mode": "navigate",
      "sec-fetch-site": "same-origin",
      "sec-fetch-user": "?1",
      "upgrade-insecure-requests": "1",
      "Referer": "https://ahmedabadcity.gov.in/AMCWEBMODULE/PTAX/FrmTENE_PEC_Details.aspx",
      "Referrer-Policy": "strict-origin-when-cross-origin"
    },
    data: new URLSearchParams({
      '__EVENTTARGET': 'ctl00$ContentPlaceHolder1$btnSearch',
      '__EVENTARGUMENT': '',
      '__VIEWSTATE': viewState,
      '__VIEWSTATEGENERATOR': viewStateGenerator,
      '__EVENTVALIDATION': eventValidation,
      'ctl00$ContentPlaceHolder1$txtSEARCHNO': '01020711500001V'
    }).toString()
  };

  const resp = await AxPortal.request(config);
  console.log(resp.data);

} catch (error) {
  console.error(error);
}

额外注意事项

  • 依赖安装:必须安装cheerio用于HTML解析,执行npm install cheerio
  • Cookie管理:开启withCredentials: true后,Axios会自动维护会话Cookie,无需硬编码过期的Cookie值
  • 请求头优化:部分sec-*开头的请求头是浏览器自动生成的,若请求仍有问题可尝试移除非必要的请求头
  • 编码处理:使用URLSearchParams自动处理表单数据编码,避免手动编码出错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 02:11:01