Solved

Interaction between Applet, Cookie e ASP.net doesn't works in Firefox

Posted on 2010-09-07
6
665 Views
Last Modified: 2012-05-10
Hi!

I have an Applet that gets the username through a cookie set by asp.net page (the page that contains the applet).

Java Code to get the cookie
------------------------------

    private void getUserNameFromCookie() {
        try {
            // get the cookies that are applicable for this applet's parent web page
            URL docBaseUrl = this.getDocumentBase();

            CookieHandler cookieHandler = CookieHandler.getDefault();
            java.util.Map<String, List<String>> headers =
                    cookieHandler.get(docBaseUrl.toURI(),
                    new HashMap<String, List<String>>());

            if (headers.isEmpty()) {
                System.out.println("No cookies found!");
            } else {
                getUserNameFromHeader(headers);
            }
        } catch (Exception e) {
            System.out.println("Unable to get cookie using CookieHandler");
            e.printStackTrace();
        }
    }

    private void getUserNameFromHeader(java.util.Map<String, List<String>> headers) {

        for (String key : headers.keySet()) {
            for (String value : headers.get(key)) {
                if (key.equals("Cookie") && value.startsWith("userName")) {
                    userName = value.split("=")[1];
                }
            }
        }
    }

Open in new window


ASP.net C# code to create the cookie (on Page_Load)
----------------------

            HttpCookie appCookie = new HttpCookie("userName");
            appCookie.Value = Session["user"].ToString();

            appCookie.Expires = DateTime.Now.AddDays(1);
            appCookie.HttpOnly = false;
            Response.Cookies.Add(appCookie);

Open in new window


In IE everything works nice, but in Firefox the operations that requires the username from cookie are not launched.

Any idea on how to resolve this?

Thanks in advance!
0
Comment
Question by:calypsoworld
  • 3
  • 3
6 Comments
 
LVL 14

Expert Comment

by:Daniel Junges
ID: 33624876
0
 
LVL 14

Expert Comment

by:Daniel Junges
ID: 33624908
0
 

Author Comment

by:calypsoworld
ID: 33631850
Thank you for reply.

Yes, cookies are enabled.
It's weird this works in IE and don't in Firefox....

Java console shows this information after applet's loading (and a lot of other things)

network: Connecting http://localhost:55000/page.aspx with cookie "__utma_a2a=6852338610.1305743161.1274186235.1274560427.1274878003.51; userName=testuser@mail.com"

Apparently there are two cookies in this page (I don't know where the first cookie come from).
Considering that are two cookies in page, am I getting it in the right way in Java?

Thank you.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:calypsoworld
ID: 33632030
I think that is the problem:

Information from Java console...

In IE
network: Connecting http://localhost:55000/page.aspx with cookie "userName=testuser@mail.com"

In Firefox
network: Connecting http://localhost:55000/page.aspx with cookie "__utma_a2a=6852338610.1305743161.1274186235.1274560427.1274878003.51; userName=testuser@mail.com"

Maybe I'm getting the wrong index in Java...
How should script the Java code to get the value in userName field, both in IE and Firefox?
Thanks in advance!
0
 

Accepted Solution

by:
calypsoworld earned 0 total points
ID: 33632651
Problem has been solved.

Thank you junges for your attention!

The problem is actually the what I said in previous post: Firefox contains an 'extra' cookie.
So, the method in Java to get the userName value, both to Firefox (2 cookies) or IE (1 cookie) is the same from the question, but with a simple adjustment.

    private void getUserNameFromHeader(java.util.Map<String, List<String>> headers) {

        for (String key : headers.keySet()) {
            for (String value : headers.get(key)) {
                if (key.equals("Cookie") ) {
                            userName = value.substring(value.indexOf("userName")+9);
                }
            }
        }
0
 
LVL 14

Expert Comment

by:Daniel Junges
ID: 33635103
very good.

regards junges
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now