Log all network interactions of Java application

前端 未结 5 684
陌清茗
陌清茗 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.

    0 讨论(0)
  • 2020-12-29 23:15

    Can't you use a logging proxy server, e.g. http://www.charlesproxy.com/ or http://fiddler2.com/? Charles Proxy can also decode AMF requests, Fiddler is the champion for SSL interception.

    IIRC you can set system properties http.proxyHost and http.proxyPort to 127.0.0.1 and 8888, and java URLConnections will automatically connect via your logging proxy server, and you get a nice view of your HTTP Traffic.

    Or, if the intention is to be able to replay the communication, use JMeter HTTP Proxy, this records in a format that is suitable for replay. It has also built-in support for handling cookies.

    If the application uses nice web services, SoapUI also has a monitoring proxy that intercepts web service calls for which you have imported the WSDL.

    0 讨论(0)
  • 2020-12-29 23:18

    A quick way to log all SSL traffic is to startup java with:

    -Djavax.net.debug=all
    

    Or set it as a system property:

    System.setProperty("javax.net.debug","all");
    

    Brace yourself. Standard out gets NOISY if you do this!

    (*Note: only logs SSL traffic and SSL debug info (handshakes, etc). Regular sockets are not logged.)

    0 讨论(0)
  • 2020-12-29 23:24

    If you want to log network traffic & you have URLConnection objects then the problem is already solved!
    If you want to log at stream level, you need to write a little wrapper on top of your I/O streams & log all data before transfer them to lower network layers, otherwise you can use connection objects directly to get required info & log them.
    For managing cookies, you should use java.net.HttpCookie which is introduced in java 6. Only thing you have to do is to create an instance of CookieManager & set it as default cookie manager:

    CookieManager cookieManager = new CookieManager();
    CookieHandler.setDefault(cookieManager);
    

    & you can use it for managing connection cookies!

    0 讨论(0)
  • 2020-12-29 23:32

    I dug a little further, and apparently, HttpURLConnection comes with java.util.logging enabled by default.

    See related questions: Java: Display request of an HttpURLConnection before sending and How to enable wire logging for a java HttpURLConnection traffic?

    Java.util.logging was Sun's (failed) attempt at standardizing logging providers in Java 1.4. You can bridge it to SLF4J via the jul-to-slf4j module if you want to avoid tweaking system properties.

    But I would still prefer Fiddler or JMeter proxy though :-)

    0 讨论(0)
提交回复
热议问题