簡體   English   中英

NodeJs HTTP 請求不適用於代理 (407)

[英]NodeJs HTTP Request Not working with proxy (407)

多年來,我一直在使用 NPM Request package,盡管它已被棄用,但它始終可以完美地滿足我的需求,直到今天我才遇到任何問題......

我正在嘗試使用用戶執行常規 GET 請求:傳遞經過身份驗證的代理和一些額外的標頭 - 我過去做過一千次同樣的事情,但是這次 url 是HTTP而不是 HTTPS。

無論出於何種原因,因為鏈接是 HTTP,它擾亂了我的代理身份驗證,並且無論出於何種原因返回407的響應代碼,並且響應正文顯示錯誤,說明Cache Access Denied (ERR_CACHE_ACCESS_DENIED) 這就是我對做什么感到困惑的地方,因為我知道我的代理身份驗證不應該有任何問題,因為我多年來一直在做同樣的事情。

請求代碼:

const request = require('request').defaults({
    timeout: 30000,
    gzip: true,
    forever: true
});

cookieJar = request.jar();
proxyUrl = "http://proxyUsername:proxyPassword@proxyDomain:proxyPort";

request({
    method: "GET",
    url: "http://mylink.com",
    proxy: proxyUrl,
    jar: cookieJar,
    headers: {
      "Proxy-Authorization": new Buffer('proxyUsername:proxyPassword').toString('base64'),
      "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.9",
      "Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8",
      "Connection": "keep-alive",
      "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36",
    },
    followAllRedirects: true
}, (err, resp, body) => {

    if (err || resp.statusCode != 200) {
        if (err) {
            console.log(err);
        } else {
            console.log(resp.statusCode);
            console.log(resp.body);

        }
        return;
    }

    console.log(resp.body);
});

響應正文片段(狀態代碼 407):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<meta type="copyright" content="Copyright (C) 1996-2019 The Squid Software Foundation and contributors">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>ERROR: Cache Access Denied</title>
<style type="text/css"><!-- 

body
:lang(fa) { direction: rtl; font-size: 100%; font-family: Tahoma, Roya, sans-serif; float: right; }
:lang(he) { direction: rtl; }
 --></style>
</head><body id=ERR_CACHE_ACCESS_DENIED>
<div id="titles">
<h1>ERROR</h1>
<h2>Cache Access Denied.</h2>
</div>
<hr>
...
<p>Sorry, you are not currently allowed to request http://mylink.com from this cache until you have authenticated yourself.</p>
...

其他注意事項:

  • 我還在我的瀏覽器上連接到完全相同的代理並訪問了同一個站點,它工作得很好,所以這不是代理本身的問題。
  • 如果我從請求中刪除代理,它會完美運行,所以也許我為 HTTP 配置了錯誤的請求是我能想到的

現在正如我所說,這個確切的代碼可以完美地與任何 HTTPS 鏈接一起工作,所以這就是我難過的地方。 任何幫助,將不勝感激!

將 Proxy-Authorization header 更改為Proxy-Authenticate ,這似乎工作得很好。 我不知道為什么它需要 HTTP 但你是 go,關於此事的文檔不多......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM