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

使用AXIOS携带pfx文件发送GET请求失败,浏览器访问正常

问题:Axios GET请求返回BIG-IP登出页面,浏览器访问正常

我使用Axios向指定API发送GET请求未获得正确响应,但通过浏览器访问该URL可正常返回预期结果<Authentication>AccessGranted</Authentication>。执行代码后返回状态码200,但响应内容为BIG-IP登出页面。

我的实现代码

const axios = require("axios");
const https = require("https");const fs = require("fs");
const pfxPath ="C:\Users\RYANHUYNHPC\Downloads\connection.pfx";
const pfxPassword = "connectPassword123";

console.log(pfxPath);

const httpsAgent = new https.Agent({

   pfx: fs.readFileSync(pfxPath),
   passphrase: pfxPassword,
   minVersion: "TLSv1.3", // Enforce TLS 1.3});

async function sendGetRequest() {
   const url ="https://api.ontest.uniquesystem.com/home.aspx?Page=Login&amp;Mode=Silent&amp;UserID=testuser01&amp;Password=testuser01pw";

try {
    const response = await axios.get(url, {
         httpsAgent,
         withCredentials: true, // Enable cookies});
    console.log("Headers:", response.headers);
    console.log("Status Code:", response.status);
    console.log("GET Response:", response.data);
} catch (error) {
    console.error("GET Error:",error.response ? error.response.data : error.message);}
}

(async function main() {
    console.log("Sending GET request...");
    await sendGetRequest();})();

执行后返回的响应信息

Sending GET request...
Headers: Object [AxiosHeaders] {
server: 'BigIP',
'content-type': 'text/html; charset=utf-8',
'accept-ranges': 'bytes',
connection: 'Keep-Alive',
date: 'Wed, 18 Dec 2024 19:19:44 GMT',
age: '33931',
'x-frame-options': 'DENY',
'set-cookie': [
'MRHSession=deleted;expires=Thu, 01-Jan-1970 00:00:01 GMT;path=/;secure',
'F5_ST=deleted;expires=Thu, 01-Jan-1970 00:00:01 GMT;path=/;secure',
'MRHSHint=deleted;expires=Thu, 01-Jan-1970 00:00:01 GMT;path=/;secure',
'F5_HT_shrinked=deleted;expires=Thu, 01-Jan-1970 00:00:01 GMT;path=/;secure',
'F5_fullWT=deleted;expires=Thu, 01-Jan-1970 00:00:01 GMT;path=/;secure',
'MRHSequence=deleted;expires=Thu, 01-Jan-1970 00:00:01 GMT;path=/;secure',
'visid_incap_2868884=zVDyne5jTzeUBRbsKtQmNtIfY2cAAAAAQUIPAAAAAAAfAwDyQ+HhOMmdWiIESTHj; expires=Thu, 18 Dec 2025 07:06:01 GMT; HttpOnly; path=/; Domain=.ontest.excel',
'incap_ses_305_2868884=nQ6/edAB5RlZIUbfHpQ7BNIfY2cAAAAAOQL+uEKkhQ7w9l+2VjAHRA==; path=/; Domain=.ontest.excelleris.com'
],
pragma: 'no-cache',
'cache-control': 'no-cache, must-revalidate',
'x-cdn': 'Imperva',
'transfer-encoding': 'chunked',
'x-iinfo': '14-100186966-100186968 NNYN CT(5 4 0) RT(1734549458766 19) q(0 0 0 -1) r(0 0) U12'
}
Status Code: 200
GET Response: <html>

BIG-IP logout page

php-template (auto)

<script language="javascript">
  function InsertActivexControl(clsid, params)
  {
      var container = document.getElementById("logoutActivexContainer");

      if (navigator.appName == "Microsoft Internet Explorer") {
          var paramsCode = "";
          for (var item in params) {
              paramsCode += "<param name='" + item + "' value='" + params[item] + "'>";
          }

          container.innerHTML =
              "<center><object classId=CLSID:" + clsid + " border=0 width=1 height=1>" +
              paramsCode + "</object></center>";
      }
      else {
          var pluginHost = new UPluginHost("", container);
          var control = {"clsid": clsid, "width": 1, "height": 1};
          pluginHost.hostControl(control, params);
      }
  }

  function OnLoad()
  {
    var isBrowserControl = false;
    try {
        isBrowserControl = ("undefined" != typeof(window.external))
            && ("unknown" != typeof(window.external))
            && ("undefined" != typeof(window.external.WebLogonNotifyUser))
            && ("unknown" != typeof(window.external.WebLogonNotifyUser));
    }
    catch(e) {}

    var display_session = get_cookie("LastMRH_Session");
    if(null != display_session) {
      document.getElementById("sessionDIV").innerHTML = '<BR>The session reference number: &nbsp;' + display_session + '<BR><BR>';
      document.getElementById("sessionDIV").style.visibility = "visible";
    }

    if(isBrowserControl) {
        window.external.WebLogonNotifyUser();
    }
    if(!isBrowserControl) {
        document.getElementById("newSessionDIV").style.visibility = "visible";
    }

    try
    {
        if (get_cookie("F5_PWS") == "1")
        {
            document.cookie = "F5_PWS=0; path=/; expires=Fri, 01-Jan-1970 00:00:01 GMT";
            var pwsClassId = "7E73BE8F-FD87-44EC-8E22-023D5FF960FF";
            InsertActivexControl(pwsClassId, {"command": "exit"} );
        }
    }catch (e) {};
  }
</script>

try{

dart (auto)

if(window.external && window.external.WebLogonOnAuthFailure) {
    window.external.WebLogonOnAuthFailure();
}

}catch(e){};try {window.external.OnHostClose();} catch(e) { }</script><meta http-equiv="X-UA-Compatible" content="IE=Edge"><meta name="viewport" id="viewport" content="initial-scale=1.0" /><meta name="robots" context="noindex,nofollow">

//orientation toolbox function setViewport(){ document.getElementById("viewport").setAttribute('content', Math.abs(window.orientation) == 90 ? 'width=device-height, initial-scale=1' : 'width=device-width, initi } setViewport(); window.onorientationchange = function() { setViewport(); }

JavaScript is not enabled. Please enable JavaScript in your browser or contact your system administrator for assistance. lished. BIG-IP can not find session information in the request. This can happen because your browser restarted after an add-on was installed. If this occurred, click the link below to continue. This can also happen because cookies are disabled in your browser. If so, enable cookies in your browser and start a new session. Thank you for using BIG-IP. To open a new session, please click here. This product is licensed from F5 Networks. © 1999-2019 F5 Networks. All rights reserved.

解决建议

1. 修正基础配置问题

  • 路径转义:Windows路径中的反斜杠需要转义,否则会被解析为特殊字符:
    const pfxPath = "C:\\Users\\RYANHUYNHPC\\Downloads\\connection.pfx";
    
  • URL参数修正:原URL中的&amp;是HTML转义字符,应直接使用&分隔参数:
    const url = "https://api.ontest.uniquesystem.com/home.aspx?Page=Login&Mode=Silent&UserID=testuser01&Password=testuser01pw";
    

2. 模拟浏览器请求头

BIG-IP可能会校验请求头的合法性,添加浏览器风格的请求头:

headers: {
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  'Accept-Language': 'en-US,en;q=0.5'
}

3. 调整TLS版本

强制TLSv1.3可能与BIG-IP不兼容,尝试改为TLSv1.2或去掉版本限制:

const httpsAgent = new https.Agent({
  pfx: fs.readFileSync(pfxPath),
  passphrase: pfxPassword,
  minVersion: "TLSv1.2" // 或直接删除该行,让Node自动协商
});

4. 会话Cookie处理

浏览器访问时会自动保留会话Cookie,而Axios是全新请求。可以先发送一个预热请求获取初始Cookie,再发送登录请求:

// 先发送预热请求获取Cookie
await axios.get("https://api.ontest.uniquesystem.com", { httpsAgent, withCredentials: true });
// 再发送登录请求
const response = await axios.get(url, { httpsAgent, withCredentials: true, headers: {...} });

修改后的完整代码

const axios = require("axios");
const https = require("https");
const fs = require("fs");
const pfxPath = "C:\\Users\\RYANHUYNHPC\\Downloads\\connection.pfx";
const pfxPassword = "connectPassword123";

console.log(pfxPath);

const httpsAgent = new https.Agent({
  pfx: fs.readFileSync(pfxPath),
  passphrase: pfxPassword,
  minVersion: "TLSv1.2"
});

async function sendGetRequest() {
  const url = "https://api.ontest.uniquesystem.com/home.aspx?Page=Login&Mode=Silent&UserID=testuser01&Password=testuser01pw";

  try {
    // 预热请求获取初始Cookie
    await axios.get("https://api.ontest.uniquesystem.com", { httpsAgent, withCredentials: true });
    
    const response = await axios.get(url, {
      httpsAgent,
      withCredentials: true,
      headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Language': 'en-US,en;q=0.5'
      }
    });
    console.log("Headers:", response.headers);
    console.log("Status Code:", response.status);
    console.log("GET Response:", response.data);
  } catch (error) {
    console.error("GET Error:", error.response ? error.response.data : error.message);
  }
}

(async function main() {
  console.log("Sending GET request...");
  await sendGetRequest();
})();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 03:15:59