axios 配置 proxy 请求失败

2023-07-19 23:09:07 +08:00
 monster1priest

我想用 axios 通过远程代理去做一个请求,但是一直返回错误。
我换用 HttpsAgent 就可以,请问这是为啥?

import axios from "axios";

axios.get('https://www.google.com', {
    proxy: {
        protocol: 'https',
        host: 'xxx.yyy.com',
        port: '443',
        auth: {
            username: '123456',
            password: '123456'
        }
    },
}).then(r => {
    console.log(r.data)
}).catch(e => {
    console.error(JSON.stringify(e))
})

{
    "message":"read ECONNRESET",
    "name":"Error",
    "stack":"Error: read ECONNRESET\n    at Function.AxiosError.from (file:///*/axios-test/node_modules/axios/lib/core/AxiosError.js:89:14)\n    at RedirectableRequest.handleRequestError (file:///*/axios-test/node_modules/axios/lib/adapters/http.js:591:25)\n    at RedirectableRequest.emit (node:events:527:28)\n    at ClientRequest.eventHandlers.<computed> (*/axios-test/node_modules/follow-redirects/index.js:14:24)\n    at ClientRequest.emit (node:events:527:28)\n    at TLSSocket.socketErrorListener (node:_http_client:454:9)\n    at TLSSocket.emit (node:events:527:28)\n    at emitErrorNT (node:internal/streams/destroy:157:8)\n    at emitErrorCloseNT (node:internal/streams/destroy:122:3)\n    at processTicksAndRejections (node:internal/process/task_queues:83:21)",
    "config":{
        "transitional":{
            "silentJSONParsing":true,
            "forcedJSONParsing":true,
            "clarifyTimeoutError":false
        },
        "adapter":[
            "xhr",
            "http"
        ],
        "transformRequest":[
            null
        ],
        "transformResponse":[
            null
        ],
        "timeout":0,
        "xsrfCookieName":"XSRF-TOKEN",
        "xsrfHeaderName":"X-XSRF-TOKEN",
        "maxContentLength":-1,
        "maxBodyLength":-1,
        "env":{
            "Blob":null
        },
        "headers":{
            "Accept":"application/json, text/plain, */*",
            "User-Agent":"axios/1.4.0",
            "Accept-Encoding":"gzip, compress, deflate, br"
        },
        "proxy":{
            "protocol":"https",
            "host":"xxx.yyy.com",
            "port":"443",
            "auth":"123456:123456"
        },
        "method":"get",
        "url":"https://www.google.com"
    },
    "code":"ECONNRESET",
    "status":null
}
896 次点击
所在节点    问与答
3 条回复
naminokoe
2023-07-20 05:57:36 +08:00
还是润了舒服,不用配置这些乱七八糟的东西,什么都可以直连
coderdusk
2023-07-20 08:35:01 +08:00
刚搜了一下,port 好像传的数字类型而不是字符串吧
monster1priest
2023-07-20 14:18:37 +08:00
@coderdusk 试过了,和这个没什么关系

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://tanronggui.xyz/t/958134

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX