简体   繁体   中英

How to get cookies in Java?

I'm implementing a project from ASP that cross to a Java program. In a Java class, what I want is to get the cookies that is set by the ASP program.

Is that possible?

If so, I have this code at hand but its not working....

class AfficheMonCookie extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException{
        String Valeur = "";
        Cookie[] cookies = request.getCookies();

        for(int i=0; i < cookies.length; i++) {
            Cookie MonCookie = cookies[i];
            if (MonCookie.getName().equals("FXA")) {
                Valeur = cookies[i].getValue();
            }
        }       
    }
}

Use a network monitor tool like fiddler to inspect the cookies set by the ASP site ( Set-Cookie response header) and to see if the cookies make it to the Servlet HTTP request ( Cookie request header).

Fiddler is a Web Debugging Proxy which logs all HTTP(S) traffic between your computer and [other machines]. Fiddler allows you to inspect traffic, set breakpoints, and "fiddle" [or just inspect] with incoming or outgoing data.

Remember that the cookies come from the client (eg web-browser) and are only sent for matching domains and paths -- it may be possible they were/are never sent to the Java server at all.

Happy coding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM