1

I am currently working on a mobile app that is going to communicate with a RESTfull server over the HTTP-protocol. For this I have made a working code and got the communication working.

For the server to identify users I wanted to use cookies (some kind of sessions cookie). So I have started out with the two following helper-methods to create a HttpClient and a context (that includes my cookie).

// Gets a standard client - set to HTTP1.1
private HttpClient getClient() {
    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    return new DefaultHttpClient(params);
}

// Gets the context with cookies to be used. This is to make each user unique
private HttpContext getHttpContext(String server) {
    CookieStore cookieStore = new BasicCookieStore();
    BasicClientCookie cookie = new BasicClientCookie("X-ANDROID-USER", "some name");
    cookie.setDomain(server);
    cookie.setPath("/");
    cookie.setVersion(1);

    cookieStore.addCookie(cookie);
    HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    return localContext;
}

I then connect to the server using the following (read function). That basically takes the Client and Conext generate by the last two helper methods and do a normal GET-request.
// Connects to the restfull server and returns the return value (get request)

    private String restfullRead(URI uri, String server)
    {
        // Sets up the different objects needed to read a HttpRequest
        HttpClient client = this.getClient();
        HttpGet request = new HttpGet();
        request.setURI(uri);
        HttpContext context = this.getHttpContext(server);

        // Connects to the server and reads response
        try {
          HttpResponse response = client.execute(request, context);
          reader.close();
        }
        catch (Exception e) {
        return "";
    }

When I run this to my server I get the following request.
GET /info/1 HTTP/1.1
Host: 192.168.0.191:8080
Connection: Keep-Alive

As you can see, this does not include any cookies. Thats why I wonder, why arent the cookie made in "getHttpContext" sent in the request?

Etse
  • 1,235
  • 1
  • 15
  • 28
  • Why dont you send the client app an unique token. This token identifies the user and will be send with every single request to the server? – Basic Coder Mar 20 '12 at 15:53
  • Maybe a bit unclear, the code was a bit simplefied - i try to sent a cookie with the name "X-ANDROID-USER" with the value "some name" so the server. When I get this working, i will change "some name" into a unique-value. – Etse Mar 20 '12 at 15:56

1 Answers1

0

This answer will help you:

How do I make an http request using cookies on Android?

// EDIT

1.) Your Android app sends it's unique NFC-Tag to the server.

2.) The server create a new unique token and builds a releation beween the token and the NFC-Tag

3.) Whenever your app is sending a request, send the unique NFC-Tag too.

4.) The server will check the NFC-Tag and searchs the token and now your device is identified

Sending custom header:

HttpGet get = new HttpGet(getURL);
get.addHeader("NFC", "YOUR NFC-TAG");
get.addHeader("Some more header", "more infos");
Community
  • 1
  • 1
Basic Coder
  • 10,882
  • 6
  • 42
  • 75
  • Hi. Thanks for trying to help. I have read through it and it is different from what I am trying to do. He is doing a post-request to the server and retrieves the cookies sent back. While I want to create a new cookie on my android app (with a unique identifier, which is read from an NFC-tag). I would like to send this tag as a cookie. Do I actually need to send a post-request to the server and make the server set the cookie? – Etse Mar 20 '12 at 16:33
  • Which is what I am tryong to do. I have even made the cookides and added into the HttpContext. And when I do the request I tell it to use this context. - Yet, the header is never actually sent. – Etse Mar 20 '12 at 16:50
  • You can send the NFC-Tag as GET Parameter or as Header. GET Parameter should be clear. I will post an example to send a header. I'm going to edit the post above – Basic Coder Mar 20 '12 at 16:52
  • Hey. Thanks for the help, This did not really fix the problem but it should work. Thank sfor the help atleast, I think I will just stick with sending them as header-fields :) – Etse Mar 20 '12 at 16:56