简体   繁体   English

HTTPclient和cookie的问题

[英]Problems with HTTPclient and cookies

I am currently working on a mobile app that is going to communicate with a RESTfull server over the HTTP-protocol. 我目前正在开发一个移动应用程序,该应用程序将通过HTTP协议与RESTfull服务器进行通信。 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). 为了让服务器识别用户,我想使用cookie(某种会话cookie)。 So I have started out with the two following helper-methods to create a HttpClient and a context (that includes my cookie). 因此,我从以下两个辅助方法开始,以创建HttpClient和上下文(包括我的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. 基本上,这需要使用Client和Conext的后两个辅助方法生成并执行正常的GET请求。
// Connects to the restfull server and returns the return value (get request) //连接到restfull服务器并返回返回值(获取请求)

 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. 如您所见,其中不包含任何cookie。 Thats why I wonder, why arent the cookie made in "getHttpContext" sent in the request? 那就是为什么我想知道,为什么不在请求中发送“ getHttpContext”中生成的cookie?

This answer will help you: 该答案将帮助您:

How do I make an http request using cookies on Android? 如何在Android上使用Cookie发出http请求?

// EDIT //编辑

1.) Your Android app sends it's unique NFC-Tag to the server. 1.)您的Android应用将其唯一的NFC标签发送到服务器。

2.) The server create a new unique token and builds a releation beween the token and the NFC-Tag 2.)服务器创建一个新的唯一令牌,并在令牌和NFC-Tag之间建立关系

3.) Whenever your app is sending a request, send the unique NFC-Tag too. 3.)每当您的应用发送请求时,也发送唯一的NFC标签。

4.) The server will check the NFC-Tag and searchs the token and now your device is identified 4.)服务器将检查NFC标签并搜索令牌,现在可以识别您的设备

Sending custom header: 发送自定义标头:

HttpGet get = new HttpGet(getURL);
get.addHeader("NFC", "YOUR NFC-TAG");
get.addHeader("Some more header", "more infos");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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