在自簽名伺服器上進行基本驗證的 Node.js HTTP 請求

在自簽名伺服器上進行基本驗證的 Node.js HTTP 請求

我使用以下程式碼只是為了從具有自簽名憑證的 Web 伺服器取得基本回應。需要基本身份驗證。

我正在嘗試下面的程式碼Node.js(使用http模組),但幾秒鐘後失敗。據我所知,它沒有從伺服器得到回應並且超時。

{[Error: socket hang up] code: 'ECONNRESET' }
Problem with request: undefined

Code:<BR>
const fs = require('fs');<BR>
const http = require('http');<BR>
const https = require('https');<BR>
var options = {<BR>
    method: "GET",<BR>
    hostname: "ServerName.com",<BR>
    port: 8445,<BR>
    rejectUnauthorized: 'false',<BR>
    cert: fs.readFileSync('Certname.cer'),<BR>
    ca: [ fs.readFileSync('Certname.cer') ],<BR>
    checkServerIdentity: () => { return null; },<BR>
    path: '/finesse/api/User/TEST/Dialogs'<BR>
    Authorization:'Basic',<BR>
    cacheControl: 'no-cache',<BR>
    acceptEncoding: "gzip, deflate",<BR>
    username: user,<BR>
    password: passw,<BR>
    Connection: 'keep-alive'<BR>
};<BR>

var req = http.request(options, function(res){<BR>
    var responseBody ="";<BR>
    console.log(`server status: ${res.statusCode}`);<BR>
    console.log("Responcer Headers: %j" , res.headers);<BR>
    res.setEnconding("UTF-8");<BR>
    res.once("data", (chunk)=>{ <BR>
        console.log(chunk);<BR>
    });<BR>
    res.on('data', function(chunk) { <BR>
        console.log(`---chunk-- ${chunk.length}`);<BR>
        responseBody +=chunk;<BR>
    });<BR>
    res.on("end", function() {<BR>
        fs.writeFile("userDialog.html", responseBody, function (error) {<BR>
            if (error) throw error; <BR>
        });<BR>
    });<BR>
});<BR>
req.on("error", function(err) {<BR>
    console.error(err);<BR>
    console.log(`Problem with request: ${err.mesage}`)<BR>
});<BR>

透過完成時相同的請求郵差返回正確的內容。我什至從 Postman 複製了請求代碼並嘗試了它,但結果相同。在Postman中,標題還有兩個選項:

"User-Agent": "PostmanRuntime/7.13.0", "Postman-Token": ....

我不知道我是否需要那些...有什麼想法嗎?

多謝!

相關內容