-2

I need to capture all the http/https requests that are going through the browsers of my system using JAVA servlets.

Can I achieve that?

halfer
  • 18,701
  • 13
  • 79
  • 158
Srinivas B
  • 1,721
  • 3
  • 14
  • 33
  • The very nature of HTTPS is to forbid "capturing" of the clear text request/response outside of the applications performing them. Now maybe you could code some kind of servlet based HTTP-proxy and create custom certificates and fool the browsers by installing them as trusted, but this is no light work, and having it work transparently under HTTPS is nearly (if not entirely) impossible. – GPI Jan 25 '17 at 12:11
  • Maybe you'll find jpcap usefull. The description says "jpcap is a set of Java classes which provide an interface and system for network packet capture". See https://sourceforge.net/projects/jpcap/ and https://javatutorial.net/capture-network-packages-java – Marco A. Hernandez Jan 25 '17 at 12:45
  • use Fiddler or WireShark – Krzysztof Cichocki Jan 25 '17 at 13:47
  • Is there any way to do with servlets for capturing specific requests ?? – Srinivas B Jan 25 '17 at 16:42
  • @Srinivas B yes, it is. Check my answer. – code_angel Jan 25 '17 at 18:40
  • As a high-rep user, we expect you to know that [ASAP begging](http://meta.stackoverflow.com/q/326569/472495) is not acceptable in questions. Please refrain from this, thanks. – halfer Jan 26 '17 at 21:22
  • http://stackoverflow.com/questions/6509628/how-to-get-http-response-code-using-selenium-webdriver-with-java# – Srinivas B Feb 06 '17 at 09:06

1 Answers1

0

going through the browsers of my system You can do this is with an implementation of ServletRequestListener::requestInitialized(ServletRequestEvent sre)

The Documentation say:

requestInitialized(ServletRequestEvent sre)
Receives notification that a ServletRequest is about to come into scope of the web application.

The class could look like this:

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Map.Entry;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;

@WebListener
public class RequestListener implements ServletRequestListener {
    public RequestListener() {}

    public void requestDestroyed(ServletRequestEvent sre)  {}

    public void requestInitialized(ServletRequestEvent sre)  {
        HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();

        System.out.println("Timestamp: " + new Timestamp(System.currentTimeMillis()));
        System.out.println("SessionId: " + request.getSession(false));
        System.out.println("RequestURL: " + request.getRequestURL());
        System.out.println("Method: " + request.getMethod());

        System.out.println("Parameters: ");
        for (Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
            System.out.println(entry.getKey() + " = " + Arrays.asList(entry.getValue()));
        }
    }
}

In the console you get something like this:

Timestamp: 2017-01-25 19:12:04.36
SessionId: null
RequestURL: https://localhost:8181/jee6/ResponseFilterTest/Fiz
Method: GET
Parameters:
p1 = [v1]
p2 = [v2]

Instead in the console you can store the data in a DB or write to a log.

If you need to differentiate between local and remote requests you can use request.getRemoteAddr(). For local requests it is 127.0.0.1

code_angel
  • 1,517
  • 1
  • 11
  • 20