gpt4 book ai didi

android - 使用 Volley,如果响应具有多个 Cookie,我如何将 Session Cookie 与 StringRequest 一起使用?

转载 作者:行者123 更新时间:2023-11-29 01:35:07 24 4
gpt4 key购买 nike

我如何使用 StringRequest 发送 session cookie 以识别网络服务器的请求。如果有两个来自服务器的 session cookie?

这是我的代码,

[1] 发起新请求 (StringRequest) , 代码 fragment let,

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = super.getHeaders();
if (headers == null || headers.equals(Collections.emptyMap())) {
headers = new HashMap<String, String>();
}
try {
addSessionCookie(headers);
} catch (Exception e) {
e.printStackTrace();
}
return headers;
}

@Override
protected Response<String> parseNetworkResponse(
NetworkResponse response) {
// check session cookies :: custom method
try {
checkSessionCookie(response.headers);
} catch (Exception e) {
e.printStackTrace();
}

return super.parseNetworkResponse(response);
}

[2] 获取 session cookie

// check session cookies
public final void checkSessionCookie(Map<String, String> headers)
throws Exception {
if (headers.containsKey("Set-Cookie")
&& headers.get("Set-Cookie").startsWith("sessionid")) {
String mycookie = headers.get("Set-Cookie");
if (mycookie.length() > 0) {
String[] splitCookie = mycookie.split(";");
String[] splitSessionId = splitCookie[0].split("=");
mycookie = splitSessionId[1];

}
}
}

// add session Cookie
public final void addSessionCookie(Map<String, String> headers)
throws Exception {
if (mycookie.length() > 0) {
StringBuilder builder = new StringBuilder();
builder.append("sessionid");
builder.append("=");
builder.append(mycookie);
if (headers.containsKey("Cookie")) {
builder.append("; ");
builder.append(headers.get("Cookie"));
}
headers.put("Cookie", builder.toString());
}
}

但上面的代码不起作用,响应是“授权错误”。

如何从响应中获取 session cookie(可能不止一个)并在新请求中设置,以验证用户?

最佳答案

这应该有效:

/**
* Created by shaharb on 5/18/15.
*/
public class DStringRequest extends StringRequest {
public DStringRequest(int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
super(method, url, listener, errorListener);
}

public DStringRequest(String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
super(url, listener, errorListener);
}

/* (non-Javadoc)
* @see com.android.volley.Request#getHeaders()
*/
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = super.getHeaders();
if (headers == null || headers.equals(Collections.emptyMap())) {
headers = new HashMap<>();
}
// add the session cookie
// try to get the cookie from the shared prefs
String sessionId = Hawk.get("connect.sid", "");
if (sessionId.length() > 0) {
StringBuilder builder = new StringBuilder();
builder.append("connect.sid");
builder.append("=");
builder.append(sessionId);
if (headers.containsKey("Set-Cookie")) {
builder.append("; ");
builder.append(headers.get("Set-Cookie"));
}
headers.put("Set-Cookie", builder.toString());
}

return headers;
}

@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
// since we don't know which of the two underlying network vehicles
// will Volley use, we have to handle and store session cookies manually
if (headers.containsKey("Set-Cookie")
&& headers.get("Set-Cookie").startsWith("connect.sid")) {
String cookie = headers.get("Set-Cookie");
if (cookie.length() > 0) {
String[] splitCookie = cookie.split(";");
String[] splitSessionId = splitCookie[0].split("=");
cookie = splitSessionId[1];

// Store is somewhare, shared prefs is ok
Hawk.put("connect.sid", cookie);
}
}

return super.parseNetworkResponse(response);
}
}

关于android - 使用 Volley,如果响应具有多个 Cookie,我如何将 Session Cookie 与 StringRequest 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28778380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com