如何在节点js / Java中捕获网络调用?

时间:2015-10-23 07:02:09

标签: java node.js http httprequest firebug

在Firefox的firebug插件中有一个名为“Net”的标签,它捕获我们从浏览器点击URL时所做的所有网络调用

我可以从代码请求URL,但我正在寻找捕获正在进行的网络调用的方法,获取在网络调用中传递的请求参数

可以使用NodeJS / Java完成吗?

enter image description here

问题更新:Java中是否可以实现相同的目标?

4 个答案:

答案 0 :(得分:2)

如果您正在使用request模块以及request-debug模块:

var request = require('request')
var debug = require('request-debug')
debug(request)

request('http://google.com')

以下是执行上述代码后我在控制台中看到的内容:

{ request: 
   { debugId: 1,
     uri: 'http://google.com/',
     method: 'GET',
     headers: { host: 'google.com' } } }
{ redirect: 
   { debugId: 1,
     statusCode: 302,
     headers: 
      { 'cache-control': 'private',
        'content-type': 'text/html; charset=UTF-8',
        location: 'http://www.google.bg/?gfe_rd=cr&ei=AgEqVt-BLqOz8we1v5P4Cw',
        'content-length': '258',
        date: 'Fri, 23 Oct 2015 09:42:26 GMT',
        server: 'GFE/2.0',
        connection: 'close' },
     uri: 'http://www.google.bg/?gfe_rd=cr&ei=AgEqVt-BLqOz8we1v5P4Cw' } }
{ request: 
   { debugId: 1,
     uri: 'http://www.google.bg/?gfe_rd=cr&ei=AgEqVt-BLqOz8we1v5P4Cw',
     method: 'GET',
     headers: { referer: 'http://google.com/', host: 'www.google.bg' } } }
{ response: 
   { debugId: 1,
     headers: 
      { date: 'Fri, 23 Oct 2015 09:42:26 GMT',
        expires: '-1',
        'cache-control': 'private, max-age=0',
        'content-type': 'text/html; charset=windows-1251',
        p3p: 'CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."',
        server: 'gws',
        'x-xss-protection': '1; mode=block',
        'x-frame-options': 'SAMEORIGIN',
        'set-cookie': [Object],
        'accept-ranges': 'none',
        vary: 'Accept-Encoding',
        connection: 'close' },
     statusCode: 200 } }

答案 1 :(得分:0)

我创建了这个使用http.IncomingMessage对象读取三个有趣属性(request.method,request.url和request.headers)的示例

 //Lets require/import the HTTP module
var http = require('http');
//Lets define a port we want to listen to
const PORT=8080; 
//We need a function which handles requests and console request info
function handleRequest(request, response){
    console.log("===================");
    console.log("request.url : "+request.url);
    console.log("===================");
    console.log("request.method : "+request.method);  
    console.log("===================");  
    console.log("request.headers : "+JSON.stringify(request.headers));  
    console.log("===================");
    console.log("request.httpVersion : "+request.httpVersion);  
    console.log("===================");
    response.end();
}
//Create a server
var server = http.createServer(handleRequest);
//Lets start our server
server.listen(PORT, function(){
    //Callback triggered when server is successfully listening. Hurray!
    console.log("Server listening on: http://localhost:%s", PORT);
});

答案 2 :(得分:0)

在节点中,我们有morgan来查看日志。虽然它可能无法完全满足您的要求。但它用于查看通过节点进行的所有调用。

答案 3 :(得分:0)

有一个基本的HTTP控制台代理服务器解决方案。 运行inetcpl.cpl并将代理设置为localhost:8080,然后在控制台上运行此代理服务器:

    var httpProxy = require("http-proxy");
var http = require("http");
var url = require("url");
var net = require('net');
var strftime = require('strftime');

var server = http.createServer(function (req, res) {
  var urlObj = url.parse(req.url);
  let nw = {
            start: strftime('%F %T.%L'),
            end:0,
            url: req.url,
            host: urlObj.protocol + "//" + urlObj.host,
            request:{
                     content:[],
                     headers:[]
                     },
            response:{
                     content:[],
                     headers:[]
                     }
            };
  console.log("HTTP: ", nw.host );
      
  var proxy = httpProxy.createProxyServer({});
  
  proxy.on("error", function (err, req, res) {
    console.log("proxy error", err);
    res.end();
  });

  proxy.web(req, res, { target: nw.host,
                        selfHandleResponse : true } );

  req.on('data', (chunk) => {
    ( nw.request.content ).push(chunk);
  }).on('end', () => {
    nw.request.content = Buffer.concat( nw.request.content ).toString(); 
    return;
  });
  
  proxy.on('proxyRes', function (proxyRes, req, res) {
      nw.response.headers = proxyRes.headers;
      proxyRes.on('data', function (chunk) {
          ( nw.response.content ).push(chunk);
      }).on('end', function () {
          nw.response.content = Buffer.concat( nw.response.content ).toString();
          //res.end( nw.response.content );
          res.end();
      });
  });
  
  res.on("finish", () => {
    nw.request.headers = req.headers;
    nw.end= strftime('%F %T.%L');
    console.log( nw );
    return;
  });
  
}).listen(8080); 

结果如下:

HTTP:  http://redirector.gvt1.com
{
  start: '2020-09-19 11:31:54.020',
  end: '2020-09-19 11:31:54.061',
  url: 'http://redirector.gvt1.com/edgedl/release2/chrome_component/AIKCnnAtp9v2eS4vn_zJZyA_2135/bbfjzj0vPWuXZOhpCzAp9Q',
  host: 'http://redirector.gvt1.com',
  request: {
    content: '',
    headers: {
      'proxy-connection': 'Keep-Alive',
      accept: '*/*',
      'accept-encoding': 'identity',
      range: 'bytes=0-249',
      'user-agent': 'Microsoft BITS/7.8',
      host: 'redirector.gvt1.com'
    }
  },
  response: {
    content: '<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">\n' +
      '<TITLE>302 Moved</TITLE></HEAD><BODY>\n' +
      '<H1>302 Moved</H1>\n' +
      'The document has moved\n' +
      '<A HREF="http://r5---sn-po8puxa-c0qs.gvt1.com/edgedl/release2/chrome_component/AIKCnnAtp9v2eS4vn_zJZyA_2135/bbfjzj0vPWuXZOhpCzAp9Q?cms_redirect=yes&mh=9R&mip=212.24.176.76&mm=28&mn=sn-po8puxa-c0qs&ms=nvh&mt=1600507629&mv=u&mvi=5&pl=19&shardbypass=yes">here</A>.\r\n' +
      '</BODY></HTML>\r\n',
    headers: {
      date: 'Sat, 19 Sep 2020 09:31:54 GMT',
      pragma: 'no-cache',
      expires: 'Fri, 01 Jan 1990 00:00:00 GMT',
      'cache-control': 'no-cache, must-revalidate',
      location: 'http://r5---sn-po8puxa-c0qs.gvt1.com/edgedl/release2/chrome_component/AIKCnnAtp9v2eS4vn_zJZyA_2135/bbfjzj0vPWuXZOhpCzAp9Q?cms_redirect=yes&mh=9R&mip=212.24.176.76&mm=28&mn=sn-po8puxa-c0qs&ms=nvh&mt=1600507629&mv=u&mvi=5&pl=19&shardbypass=yes',
      'content-type': 'text/html; charset=UTF-8',
      server: 'ClientMapServer',
      'content-length': '478',
      'x-xss-protection': '0',
      'x-frame-options': 'SAMEORIGIN',
      connection: 'close'
    }
  }
}

这显示了非常基本的信息:主机,URL,开始和结束时间,请求和响应头以及内容。