Node语言代码示例

发布时间:2021-12-08 16:15

nodejs http

  1. const http = require("http");
  2. const url = require("url");
  3. const targetURL = url.parse("https://myip.top");
  4. const proxyIp = "您的代理IP";
  5. const proxyPort = 端口号;
  6. const authKey = "请改成您的Key";
  7. const password = "请改成您的AuthPwd";
  8. const base64 = new Buffer.from(authKey + ":" + password).toString("base64");
  9. const options = {
  10. host: proxyIp,
  11. port: proxyPort,
  12. path: targetURL,
  13. method: "GET",
  14. headers: {
  15. "Host": urlParsed.hostname,
  16. "Proxy-Authorization": "Basic " + base64
  17. }
  18. };
  19. http.request(options, function (resp) {
  20. console.log("response status code: " + resp.statusCode);
  21. resp.pipe(process.stdout);
  22. }).on("error", function (err) {
  23. console.log("request failed: " + err);
  24. }).end();

nodejs request

  1. const request = require("request");
  2. const targetUrl = "https://myip.top";
  3. const proxyIp = "您的代理IP";
  4. const proxyPort = 端口号;
  5. const authKey = "请改成您的Key";
  6. const password = "请改成您的AuthPwd";
  7. const proxyUrl = "http://" + authKey + ":" + password + "@" + proxyIp + ":" + proxyPort;
  8. const req = request.defaults({'proxy': proxyUrl});
  9. const options = {
  10. url: targetUrl,
  11. headers: {}
  12. };
  13. req.get(options, function (err, resp, body) {
  14. if (err) {
  15. return console.log(err);
  16. }
  17. console.log("response status code: " + resp.statusCode);
  18. console.log("response body: " + body);
  19. }).on("error", function (err) {
  20. console.log("request failed: " + err);
  21. });

nodejs superagent

  1. const request = require("superagent");
  2. require("superagent-proxy")(request);
  3. const targetUrl = "https://myip.top";
  4. const proxyIp = "您的代理IP";
  5. const proxyPort = 端口号;
  6. const authKey = "请改成您的Key";
  7. const password = "请改成您的AuthPwd";
  8. const proxyUrl = "http://" + authKey + ":" + password + "@" + proxyIp + ":" + proxyPort;
  9. request.get(targetUrl).proxy(proxyUrl).end(function onResponse(err, resp) {
  10. if (err) {
  11. return console.log(err);
  12. }
  13. console.log("response status code: " + resp.statusCode);
  14. console.log("response body: " + resp.text);
  15. });

nodejs axios

  1. const axios = require("axios")
  2. const {HttpsProxyAgent} = require("https-proxy-agent")
  3. const targetUrl = "http://myip.top"
  4. const proxyIp = "您的代理IP"
  5. const proxyPort = 端口号
  6. const authKey = "请改成您的Key"
  7. const password = "请改成您的AuthPwd"
  8. const proxy = new HttpsProxyAgent(`http://${authKey}:${password}@${proxyIp}:${proxyPort}`)
  9. axios.get(targetUrl, {
  10. httpAgent: proxy,
  11. httpsAgent: proxy,
  12. }).then(function (response) {
  13. console.log("response body: " + response.data)
  14. }).catch(function (error) {
  15. console.log("request failed: " + error)
  16. }).finally(function () {
  17. console.log("request finished.")
  18. })
  19. // 如果目标站是HTTPS,则需要使用下面的代码进行代理请求
  20. let httpsProxyAgent = require('https-proxy-agent');
  21. var agent = new httpsProxyAgent(`http://${authKey}:${password}@${proxyIp}:${proxyPort}`);
  22. var config = {
  23. url: "https://myip.top",
  24. httpsAgent: agent
  25. }
  26. axios.request(config)
  27. .then(function(response) {
  28. console.log("response body: " + response.data)
  29. }).catch(function(error) {
  30. console.log("request failed: " + error)
  31. })
本文导读

客户热线:0771-7711440

微信客服
微信客服
微信二维码