如何用Java获取cookie?

时间:2022-06-17 05:09:50

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.

我正在从ASP实现一个项目,该项目与Java程序交叉。在Java类中,我想要的是获取ASP程序设置的cookie。

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();
            }
        }       
    }
}

1 个解决方案

#1


0  

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这样的网络监视工具来检查ASP站点设置的cookie(Set-Cookie响应头),并查看cookie是否使其成为Servlet HTTP请求(Cookie请求头)。

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.

Fiddler是一个Web调试代理,它记录您的计算机和[其他机器]之间的所有HTTP(S)流量。 Fiddler允许您检查流量,设置断点,并“输入”[或仅检查]传入或传出数据。

Remember that the cookies come from the client (e.g. 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.

请记住,cookie来自客户端(例如Web浏览器),并且只发送给匹配的域和路径 - 它们可能根本不会被发送到Java服务器。

Happy coding.

#1


0  

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这样的网络监视工具来检查ASP站点设置的cookie(Set-Cookie响应头),并查看cookie是否使其成为Servlet HTTP请求(Cookie请求头)。

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.

Fiddler是一个Web调试代理,它记录您的计算机和[其他机器]之间的所有HTTP(S)流量。 Fiddler允许您检查流量,设置断点,并“输入”[或仅检查]传入或传出数据。

Remember that the cookies come from the client (e.g. 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.

请记住,cookie来自客户端(例如Web浏览器),并且只发送给匹配的域和路径 - 它们可能根本不会被发送到Java服务器。

Happy coding.