【问题标题】:How can I send request via proxy server in nodejs?如何通过 nodejs 中的代理服务器发送请求?
【发布时间】:2022-02-06 00:08:23
【问题描述】:

在java中,我可以通过代理服务器发送请求

HttpClient clinet = HttpClient.newBuilder()
        .proxy(ProxySelector.of(new InetSocketAddress("proxy.server", portNum)))
        .build();
HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create("https://server.I.want.to.send.request"))
        .POST(HttpRequest.BodyPublishers.noBody())
        .build();

HttpResponse<String> response =  clinet.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());   

我已经用上面的代码进行了测试,并检查它工作正常。

但如果我想在 nodejs (react) 中做同样的事情,它似乎不起作用。

const agent = httpsProxyAgent('proxy.server:portNum');

axios.request({
    method: 'POST',
    url: "https://server.I.want.to.send.request",
    data: {body},
    httpsAgent: agent
});

据我所知,上面的代码是我可以在 react 中尝试使用 JavaScript 的最佳代码。
我已经尝试过了,但它失败并出现超时错误(这意味着我猜请求不会通过代理服务器)

谁能给我一个建议来解决这个问题?

【问题讨论】:

    标签: node.js reactjs axios proxy


    【解决方案1】:

    我认为你可以参考github中的这个问题:

    https://github.com/axios/axios/issues/925#issuecomment-513028175

    如果这仍然无法解决您的问题。您可能会更改为使用另一个库,node-fetch。链接在这里:

    https://www.scrapingbee.com/blog/proxy-node-fetch/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-23
      • 1970-01-01
      • 1970-01-01
      • 2010-10-31
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      相关资源
      最近更新 更多