为什么.net开发的webapp在android机上不能鎐ookie
发布网友
发布时间:2022-04-23 23:06
我来回答
共1个回答
热心网友
时间:2023-09-09 00:38
Android中在使用OkHttp这个库的时候,有时候需要持久化Cookie,那么怎么实现呢。OkHttp的内部源码过于复杂,不进行深究,这里只看其中的HttpEngineer里面的部分源码,在发起请求以及请求结束都会调用这个类的几个方法。我们先看networkRequest方法,在里面通过client.getCookieHandler()函数获得了CookieHandler对象,通过该对象拿到cookie并设置到请求头里,请求结束后取得响应后通过networkResponse.headers()函数将请求头获得传入receiveHeaders函数,并将取得的cookie存入getCookieHandler得到的一个CookieHandler对象中去
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
This client doesn't specify a default {@code Accept} header because it
* doesn't know what content types the application is interested in.
*/
private Request networkRequest(Request request) throws IOException {
Request.Builder result = request.newBuilder();
//此处省略n行代码......
CookieHandler cookieHandler = client.getCookieHandler();
if (cookieHandler != null) {
// Capture the request headers added so far so that they can be offered to the CookieHandler.
// This is mostly to stay close to the RI; it is unlikely any of the headers above would
// affect cookie choice besides Host.
Map<string, string="">> headers = OkHeaders.toMultimap(result.build().headers(), null);
Map<string, string="">> cookies = cookieHandler.get(request.uri(), headers);
// Add any new cookies to the request.
OkHeaders.addCookies(result, cookies);
}
//此处省略n行代码......
return result.build();
} data-snippet-id=ext.4df17b386793c68468195a690c917859 data-snippet-saved=false data-csrftoken=bFkHGE1O-cy7WY9D6w70F6qK9YUKZHLynI5g data-codota-status=done><code class="hljs" java="">/**
* Populates request with defaults and cookies.
*
* </code><p><code class="hljs" java="">This client doesn't specify a default {@code Accept} header because it
* doesn't know what content types the application is interested in.
*/
private Request networkRequest(Request request) throws IOException {
Request.Builder result = request.newBuilder();
//此处省略n行代码......
CookieHandler cookieHandler = client.getCookieHandler();
if (cookieHandler != null) {
// Capture the request headers added so far so that they can be offered to the CookieHandler.
// This is mostly to stay close to the RI; it is unlikely any of the headers above would
// affect cookie choice besides Host.
Map<string, string="">> headers = OkHeaders.toMultimap(result.build().headers(), null);
Map<string, string="">> cookies = cookieHandler.get(request.uri(), headers);
// Add any new cookies to the request.
OkHeaders.addCookies(result, cookies);
}
//此处省略n行代码......
return result.build();
}</string,></string,></code></p></string,></string,>
?
1
2
3
4
5
6
7
<code class="hljs" java="">public void readResponse() throws IOException {
//此处省略n行代码......
receiveHeaders(networkResponse.headers());
//此处省略n行代码......
}</code>
?
1
2
3
4
5
6
<code class="hljs" java="">public void receiveHeaders(Headers headers) throws IOException {
CookieHandler cookieHandler = client.getCookieHandler();
if (cookieHandler != null) {
cookieHandler.put(userRequest.uri(), OkHeaders.toMultimap(headers, null));
}
}</code>
而这个CookieHandler对象是OkHttpClient类中的一个属性,提供了getter和setter方法,默认的构造函数OkHttpClient client = new OkHttpClient();不会创建这个CookieHandler对象。假设我们传入了这个对象,那么OkHttp自然会进行cookie的自动管理了。
?
1
2
3
4
5
6
7
8
9
<code class="hljs" cs="">private CookieHandler cookieHandler;
public OkHttpClient setCookieHandler(CookieHandler cookieHandler) {
this.cookieHandler = cookieHandler;
return this;
}
public CookieHandler getCookieHandler() {
return cookieHandler;
}</code>
那么假设我们将CookieHandler对象传入
?
1
2
<code axapta="" class="hljs">OkHttpClient client = new OkHttpClient();
client.setCookieHandler(CookieHandler cookieHanlder);</code>
那么,现在关键是如何去实现这个CookieHandler 对象。CookieManager是CookieHandler 的一个子类,其构造函数 public CookieManager(CookieStore store, CookiePolicy cookiePolicy)需要传入两个参数,CookieStore 是一个接口,因此我们实现CookieStore接口中的抽象方法,即可实现这个CookieHandler 对象。参考android-async-http这个库,它具有cookie的自动管理功能,主要我们参考其中的两个类
PersistentCookieStore SerializableCookie
参考以上两个类并做适当修改,得到了如下两个类,他们的功能就是将cookie保持在SharedPreferences中。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<code avrasm="" class="hljs">package com.kltz88.okhttp.cookie;
/**
* User:lizhangqu(513163535@qq.com)
* Date:2015-07-13
* Time: 17:31
*/
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.HttpCookie;
public class SerializableHttpCookie implements Serializable {
private static final long serialVersionUID = 6374381323722046732L;
private transient final HttpCookie cookie;
private transient HttpCookie clientCookie;
public SerializableHttpCookie(HttpCookie cookie) {
this.cookie = cookie;
}
public HttpCookie getCookie() {
HttpCookie bestCookie = cookie;
if (clientCookie != null) {
bestCookie = clientCookie;
}
return bestCookie;
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.writeObject(cookie.getName());
out.writeObject(cookie.getValue());
out.writeObject(cookie.getComment());
out.writeObject(cookie.getCommentURL());
out.writeObject(cookie.getDomain());
out.writeLong(cookie.getMaxAge());
out.writeObject(cookie.getPath());
out.writeObject(cookie.getPortlist());
out.writeInt(cookie.getVersion());
out.writeBoolean(cookie.getSecure());
out.writeBoolean(cookie.getDiscard());
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
String name = (String) in.readObject();
String value = (String) in.readObject();
clientCookie = new HttpCookie(name, value);
clientCookie.setComment((String) in.readObject());
clientCookie.setCommentURL((String) in.readObject());
clientCookie.setDomain((String) in.readObject());
clientCookie.setMaxAge(in.readLong());
clientCookie.setPath((String) in.readObject());
clientCookie.setPortlist((String) in.readObject());
clientCookie.setVersion(in.readInt());
clientCookie.setSecure(in.readBoolean());
clientCookie.setDiscard(in.readBoolean());
}
}</code>
?