Log all network interactions of Java application

前端 未结 5 719
陌清茗
陌清茗 2020-12-29 22:53

I have a monstrous Java app (a client of little-known application server GNUEnterprise) and its source, which I can compile back after making some changes to it. The app use

5条回答
  •  情深已故
    2020-12-29 23:13

    I just want to post a snippet of code that you can use at starting point. I do not have a"global" logger but with small changes to existing code you can reach your goal. I "log" on standard output, since your problem can be split into two pieces: getting the info from UrlConnection and store it for further reference.

    This code logs the request:

    public class URLConnectionReader {
    
      public static void main(String[] args) throws Exception {
      URL oracle = new URL("http://www.google.com/");
      URLConnection yc = oracle.openConnection();
    
      String headerName = null;
      for (int i = 1; (headerName = yc.getHeaderFieldKey(i)) != null; i++) {
        if (headerName.equals("Set-Cookie")) {
          String cookie = yc.getHeaderField(i);
          System.out.println("Cookie  ::  " + cookie);
        } else {
          System.out.println("Header: "+ yc.getHeaderField(i));
        }
      }
    
      BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
      String inputLine;
      while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);
      in.close();
    }
    

    }

    saving all this data to one or more files is not hard. If you believe that I'm on right way for you problem, I would be glad to edit the answer with other details as needed.

提交回复
热议问题