×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    11

    Cookies in Java ME (Nokia Asha)

    I am implementing small Java ME app. This app gets some data from 3rd patty resource and needs to be authenticated before. I do first call for get cookies (it was easy), and the second call with this cookies for get data (I am stuck here). I googled a little how to do it, and found next solution - Deal with cookie with J2ME

    I have changed this code to next for my purpose:

    public void getData(String url,String cookie) {
    HttpConnection hpc = null;
    InputStream is = null;

    try {
    hpc = (HttpConnection) Connector.open(url);
    hpc.setRequestProperty("cookie", cookie);
    is = hpc.openInputStream();
    int length = (int) hpc.getLength(); // GETS -1
    byte[] response = new byte[length];
    is.read(response);
    String strResponse = new String(response);
    } catch (Exception e) {
    System.out.println(e.getMessage() + " " + e.toString());
    } finally {
    try {
    if (is != null)
    is.close();
    if (hpc != null)
    hpc.close();
    } catch (Exception e) {}
    }
    }

    This code doesn't work, and `(int) hpc.getLength();` gets -1. When I try to put this URL in the browser (I logged to the site before) its gets correct response. cookie has next pattern = "auth=xxxxxxxxxxxx".

    Let me show C# code that I am using for same purpose:

    private static async Task<HttpResponseMessage> GetData(string cookie)
    {
    var parameters = new List<KeyValuePair<string, string>>() //some parameters for call
    HttpContent httpContent = new FormUrlEncodedContent(parameters);
    var cookieContainer = new CookieContainer();
    cookieContainer.Add(new Uri(c_URL), new Cookie("auth", cookie));
    var handler = new HttpClientHandler
    {
    UseDefaultCredentials = false,
    UseCookies = true,
    CookieContainer = cookieContainer,
    };
    var client = new HttpClient(handler);
    return await client.PostAsync(c_URL, httpContent);
    }

    What is wrong in my java me code? Does anybody have canonical solution/example for this?
    Last edited by Hospodar; 2013-07-30 at 08:04.

  2. #2
    Registered User
    Join Date
    Dec 2012
    Location
    India
    Posts
    264

    Re: Cookies in Java ME (Nokia Asha)

    I think the response was gzip encoded. Have a look here :

    http://developer.nokia.com/Community...4-gzip-In-Http

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    11

    Re: Cookies in Java ME (Nokia Asha)

    Thanks! You are right. When I added header:
    hpc.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    hpc.setRequestProperty("Accept-Encoding", "gzip, deflate");
    hpc.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

    It started to work. But response looks strange:
    ??ÑÁNÃ0à;O±(²§M}A-?@
    .?PYS¨Ôe¥Í@\üìde??XÊo}Vâ]hk?­6ëµóA|µvÞz'Íà?wAúêmw4í0?ÐÆ?ÚMW=?òêz CÛUa:6Ö7¼T?<oF?nh6[_0?l4?äê&)?çó³?ÅÕúf¨ä(.? ªDÙ??§?ÊP+??(:?Á,Si¾ïA¥ã-jJÅÄ8ÊbBçL)gs.S.þG5ÌÀÆéX}CÁíÑ-þ?BDK`²?\¶?ó3I÷ô±e]°6¬c?q?Ó?¼?Y.¯??Y?%?ÏP1è?ìw;?È Ò??e
    |ôh0?
    How can I improve this? Set special encoding, or what?

  4. #4
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,567

    Re: Cookies in Java ME (Nokia Asha)

    This discussion puzzles me a little, as it seems like there is a question in #1, there is an unseen answer, there is a next question in #3, and there is an answer for #3 in #2.
    So while I do not know what the original issue has been, now you have a gziped/deflated response, and following the link in Arunkam's post you will find a tiny library linked in #4 (of that discussion).

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    11

    Re: Cookies in Java ME (Nokia Asha)

    You are right. Issue described in first topic is fixed. I will create new thread with new question.

Similar Threads

  1. Replies: 6
    Last Post: 2013-12-01, 16:48
  2. Map API + Asha and Java SDK 1.1
    By chriske86 in forum Maps API for Java ME
    Replies: 4
    Last Post: 2012-06-13, 08:39

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •